diff options
| author | eltrac <hi@eltr.ac> | 2026-02-10 13:40:03 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-02-10 13:40:03 +0800 |
| commit | 1b844f1230a03983bf027a321eb77bb908f469ea (patch) | |
| tree | 8b30244506bb0a3ddb1a62376555a6b26ff24ae4 /layouts/_partials/utils | |
| parent | 81b562797338f1e6eb15835deacc652c049ac49e (diff) | |
refactor: use custom render hook
- remove inject-footnotes.js, use a partial that receives `.Content` and
does the injection statically
- use a `utils/render` partial, as custom "render hook"
- reuse `content` partial for all pages
Diffstat (limited to 'layouts/_partials/utils')
| -rw-r--r-- | layouts/_partials/utils/inject-footnotes.html | 44 | ||||
| -rw-r--r-- | layouts/_partials/utils/render.html | 1 |
2 files changed, 45 insertions, 0 deletions
diff --git a/layouts/_partials/utils/inject-footnotes.html b/layouts/_partials/utils/inject-footnotes.html new file mode 100644 index 00000000..57cc1e00 --- /dev/null +++ b/layouts/_partials/utils/inject-footnotes.html @@ -0,0 +1,44 @@ +{{ $content := . }} + +{{ $footnotesPattern := `(?s)<div class="footnotes"[^>]*>.*?</div>` }} +{{ $footnotesMatch := findRE $footnotesPattern $content 1 }} + +{{ if $footnotesMatch }} + {{ $footnotesSection := index $footnotesMatch 0 }} + + {{ $liPattern := `(?s)<li id="fn:(\d+)">(.*?)</li>` }} + {{ $footnoteItems := findRESubmatch $liPattern $footnotesSection }} + + {{ $footnoteMap := dict }} + {{ range $footnoteItems }} + {{ $id := index . 1 }} + {{ $contentRaw := index . 2 }} + {{ $cleanContent := replaceRE `(?s)\s*<a href="#fnref:\d+"[^>]*>.*?</a>\s*$` "" $contentRaw }} + {{ $footnoteMap = merge $footnoteMap (dict $id $cleanContent) }} + {{ end }} + + {{ $contentWithoutFootnotes := replaceRE $footnotesPattern "" $content }} + + {{ $refPattern := `<sup id="fnref:(\d+)".*?</sup>` }} + {{ $refs := findRESubmatch $refPattern $contentWithoutFootnotes }} + + {{ $result := $contentWithoutFootnotes }} + {{ $count := len $refs }} + + {{ range seq (sub $count 1) -1 0 }} + {{ $ref := index $refs . }} + {{ $refFull := index $ref 0 }} + {{ $refId := index $ref 1 }} + {{ $noteContent := index $footnoteMap $refId }} + + {{ if $noteContent }} + {{ $inlineFootnote := printf `<div class="inline-footnote text-sm text-secondary flex gap-2 ml-4 mt-2 mb-2" id="fn:%s"><span>%s.</span><div>%s</div></div>` $refId $refId $noteContent }} + {{ $replacement := printf `%s%s` $refFull $inlineFootnote }} + {{ $result = replace $result $refFull $replacement 1 }} + {{ end }} + {{ end }} + + {{ $content = $result }} +{{ end }} + +{{ $content | safeHTML }} diff --git a/layouts/_partials/utils/render.html b/layouts/_partials/utils/render.html new file mode 100644 index 00000000..bdc4bf2f --- /dev/null +++ b/layouts/_partials/utils/render.html @@ -0,0 +1 @@ +{{ partial "utils/inject-footnotes" . }} |
