Skip to content

Commit

Permalink
fix: show ancestors of headings
Browse files Browse the repository at this point in the history
Closes #249
  • Loading branch information
razonyang committed Sep 6, 2024
1 parent 58d0fba commit 3b391cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
17 changes: 15 additions & 2 deletions assets/search/js/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,15 @@ export default class Renderer {
continue
}

headings += `<a title="${heading.title} - ${result.item.title}" href="${result.item.url}#${heading.anchor}" class="search-result search-result-heading">
let ancestors = this.headingAncestors(result.item.headings, heading.pid)
ancestors = ancestors.concat(result.item.title)
const subtitle = ancestors.join(' · ')

headings += `<a title="${heading.title} - ${subtitle}" href="${result.item.url}#${heading.anchor}" class="search-result search-result-heading">
<div class="search-result-icon search-result-heading-icon">${params.icons['heading']}</div>
<div class="search-result-content">
<div class="search-result-title">${this.highlight(heading.title, [matches[j]])}</div>
<div class="search-result-desc">${result.item.title}</div>
<div class="search-result-desc">${subtitle}</div>
</div>
</a>`
break // avoid match same heading multiple times.
Expand All @@ -358,4 +362,13 @@ export default class Renderer {

return headings
}

headingAncestors(headings, pid): Array<string> {
let v :Array<string> = []
if (pid >= 0) {
v.push(headings[pid].title)
v = v.concat(this.headingAncestors(headings, headings[pid].pid))
}
return v
}
}
7 changes: 1 addition & 6 deletions layouts/partials/search/functions/parse-headings.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{{- $headings := slice }}
{{- with .Fragments }}
{{- range .HeadingsMap }}
{{- $headings = $headings | append (dict
"anchor" .ID
"title" (.Title | plainify))
}}
{{- end }}
{{- $headings = partial "search/functions/walk-headings" (dict "Headings" .Headings "PID" -1) }}
{{- end }}
{{- return $headings -}}
16 changes: 16 additions & 0 deletions layouts/partials/search/functions/walk-headings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- $v := slice }}
{{- $pid := .PID }}
{{- range .Headings }}
{{- if ne .Title "" }}
{{- $v = $v | append (dict
"pid" $pid
"anchor" .ID
"title" (.Title | plainify))
}}
{{- end }}
{{- $v = $v | append (partial "search/functions/walk-headings" (dict
"Headings" .Headings
"PID" (add $pid (len $v))))
}}
{{- end }}
{{- return $v }}

0 comments on commit 3b391cd

Please sign in to comment.