summaryrefslogtreecommitdiffstats
path: root/services/wiki
diff options
context:
space:
mode:
authorOto Šťáva <oto.stava@gmail.com>2024-11-23 14:37:16 +0100
committerOto Šťáva <oto.stava@gmail.com>2024-11-24 15:55:34 +0100
commitfc31fa0eeb925dca634b2ffb64344fb58e4530fe (patch)
tree9a6a408ae3d331414c13ed5916c39b72ad238c8f /services/wiki
parentMerge pull request 'Update module github.com/stretchr/testify to v1.10.0 (for... (diff)
downloadforgejo-fc31fa0eeb925dca634b2ffb64344fb58e4530fe.tar.xz
forgejo-fc31fa0eeb925dca634b2ffb64344fb58e4530fe.zip
Show page titles in wiki search results (#6048)
Replace wiki page filenames with page titles in the search results, fixing the problem with them showing unreadable URI-encoded names.
Diffstat (limited to 'services/wiki')
-rw-r--r--services/wiki/wiki.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go
index e1b37d1e7f..63196aa862 100644
--- a/services/wiki/wiki.go
+++ b/services/wiki/wiki.go
@@ -408,18 +408,42 @@ func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error {
return nil
}
-func SearchWikiContents(ctx context.Context, repo *repo_model.Repository, keyword string) ([]*git.GrepResult, error) {
+type SearchContentsResult struct {
+ *git.GrepResult
+ Title string
+}
+
+func SearchWikiContents(ctx context.Context, repo *repo_model.Repository, keyword string) ([]SearchContentsResult, error) {
gitRepo, err := git.OpenRepository(ctx, repo.WikiPath())
if err != nil {
return nil, err
}
defer gitRepo.Close()
- return git.GrepSearch(ctx, gitRepo, keyword, git.GrepOptions{
+ grepRes, err := git.GrepSearch(ctx, gitRepo, keyword, git.GrepOptions{
ContextLineNumber: 0,
Mode: git.FixedAnyGrepMode,
RefName: repo.GetWikiBranchName(),
MaxResultLimit: 10,
MatchesPerFile: 3,
})
+ if err != nil {
+ return nil, err
+ }
+
+ res := make([]SearchContentsResult, 0, len(grepRes))
+ for _, entry := range grepRes {
+ wp, err := GitPathToWebPath(entry.Filename)
+ if err != nil {
+ return nil, err
+ }
+ _, title := WebPathToUserTitle(wp)
+
+ res = append(res, SearchContentsResult{
+ GrepResult: entry,
+ Title: title,
+ })
+ }
+
+ return res, nil
}