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.
This commit is contained in:
Oto Šťáva 2024-11-23 14:37:16 +01:00
parent 19bde653c1
commit fc31fa0eeb
No known key found for this signature in database
GPG key ID: 32B22D20C9B4E680
4 changed files with 41 additions and 6 deletions

View file

@ -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
}