From 1b844f1230a03983bf027a321eb77bb908f469ea Mon Sep 17 00:00:00 2001
From: eltrac
Date: Tue, 10 Feb 2026 13:40:03 +0800
Subject: 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
---
assets/js/inject-footnotes.js | 36 ----------------------
layouts/_partials/content.html | 9 ++++++
layouts/_partials/scripts.html | 1 -
layouts/_partials/template/post-page.html | 9 +-----
layouts/_partials/utils/inject-footnotes.html | 44 +++++++++++++++++++++++++++
layouts/_partials/utils/render.html | 1 +
layouts/fictions/page.html | 12 ++------
layouts/library/page.html | 6 +---
8 files changed, 59 insertions(+), 59 deletions(-)
delete mode 100644 assets/js/inject-footnotes.js
create mode 100644 layouts/_partials/content.html
create mode 100644 layouts/_partials/utils/inject-footnotes.html
create mode 100644 layouts/_partials/utils/render.html
diff --git a/assets/js/inject-footnotes.js b/assets/js/inject-footnotes.js
deleted file mode 100644
index 20086ea6..00000000
--- a/assets/js/inject-footnotes.js
+++ /dev/null
@@ -1,36 +0,0 @@
-document.addEventListener("DOMContentLoaded", () => {
- const refs = Array.from(document.querySelectorAll("sup[id^='fnref']"));
- // reverse traversal, to keep injected footnotes in order
- for (let i = refs.length - 1; i >= 0; i--) {
- const ref = refs[i];
-
- const noteNumber = i + 1;
- const noteId = ref.id.replace("fnref", "fn");
- const note = document.getElementById(noteId);
-
- if (note) {
- // construct note element
- const inline = document.createElement("div");
- inline.className = "text-sm text-secondary flex gap-2 ml-4";
-
- const footnoteNumber = document.createElement('span')
- footnoteNumber.textContent = noteNumber;
- const footnoteContent = document.createElement('div');
- footnoteContent.innerHTML = note.innerHTML;
-
- inline.appendChild(footnoteNumber);
- inline.appendChild(footnoteContent);
- inline.id = noteId;
-
- // insert adjacent to paragraph
- // if it's inside a table, then insert outside the table
- if (ref.closest('table')) {
- ref.closest('table').insertAdjacentElement("afterend", inline);
- } else {
- ref.parentNode.insertAdjacentElement("afterend", inline);
- }
- }
- }
- // remove original footnotes
- document.getElementsByClassName("footnotes")[0]?.remove()
-});
diff --git a/layouts/_partials/content.html b/layouts/_partials/content.html
new file mode 100644
index 00000000..e1baae34
--- /dev/null
+++ b/layouts/_partials/content.html
@@ -0,0 +1,9 @@
+
+ {{ partial "posts/toc" . }}
+
+ {{ partial "utils/render" .Content }}
+
+ {{ partial "posts/endnotes" . }}
+
+
diff --git a/layouts/_partials/scripts.html b/layouts/_partials/scripts.html
index 718d7477..6ad03632 100644
--- a/layouts/_partials/scripts.html
+++ b/layouts/_partials/scripts.html
@@ -10,7 +10,6 @@
{{ if not .IsHome }}
{{ $articleJS := slice
(resources.Get "js/toc.js")
- (resources.Get "js/inject-footnotes.js")
(resources.Get "js/back2top.js")
(resources.Get "js/fediform.js")
}}
diff --git a/layouts/_partials/template/post-page.html b/layouts/_partials/template/post-page.html
index b05b6b41..d25b50fe 100644
--- a/layouts/_partials/template/post-page.html
+++ b/layouts/_partials/template/post-page.html
@@ -35,14 +35,7 @@
-
-
- {{ partial "posts/toc" . }}
- {{ .Content }}
- {{ partial "posts/endnotes" . }}
-
-
+ {{ partial "content" . }}
{{ partial "posts/action-menu.html" . }}
{{ partial "components/back-to-top.html" . }}
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)]*>.*?` }}
+{{ $footnotesMatch := findRE $footnotesPattern $content 1 }}
+
+{{ if $footnotesMatch }}
+ {{ $footnotesSection := index $footnotesMatch 0 }}
+
+ {{ $liPattern := `(?s)
于 {{ partial "components/date" . }} 读完
-- cgit v1.2.3