Add margin notes: Tufte-style side notes with dashed inline fallback

This commit is contained in:
2026-11-15 10:04:32 +08:00
parent 88c8c36d02
commit cfe85f9b16
6 changed files with 188 additions and 8 deletions
+91
View File
@@ -223,3 +223,94 @@ html.dark #post-list .post-card:hover {
#post-list .blog-post-item { #post-list .blog-post-item {
margin-bottom: 1rem; margin-bottom: 1rem;
} }
/* ===== 注释(边注):宽屏显示在两侧留白,窄屏虚线框内嵌 ===== */
.post-note-mark {
font-size: 0.7em;
line-height: 0;
vertical-align: super;
margin-left: 0.1em;
color: #2563eb;
font-weight: 600;
}
.dark .post-note-mark {
color: #60a5fa;
}
/* 窄屏 / 无 JS:虚线框内嵌在正文中 */
.post-note {
display: block;
margin: 0.5rem 0;
padding: 0.5rem 0.75rem;
border: 1px dashed #cbd5e1;
border-radius: 0.375rem;
font-size: 0.85rem;
line-height: 1.65;
color: #6b7280;
}
.dark .post-note {
border-color: #404040;
color: #9ca3af;
}
/* 宽屏:由 notes.js 接管,放进正文两侧的留白 */
@media (min-width: 80rem) {
/* 去掉 TOC / 侧栏占位后,把正文栏收回原来的阅读宽度,两侧留给注释 */
main.notes-page {
max-width: 52rem;
margin-left: auto;
margin-right: auto;
}
html.js .post-note {
display: none;
}
html.js main.notes-page.notes-active {
position: relative;
}
html.js main.notes-page.notes-active .post-note.placed {
display: block;
position: absolute;
width: 12rem;
margin: 0;
padding: 0 0.75rem;
border: 0 solid #e5e7eb;
border-radius: 0;
background: none;
font-size: 0.8125rem;
line-height: 1.65;
color: #6b7280;
}
html.dark.js main.notes-page.notes-active .post-note.placed {
color: #9ca3af;
border-color: #333;
}
/* 注释靠在正文哪一侧,就在朝向正文的那边加一条细线 */
html.js main.notes-page.notes-active .post-note.placed[data-side="right"] {
left: calc(100% + 1rem);
right: auto;
border-left-width: 2px;
}
html.js main.notes-page.notes-active .post-note.placed[data-side="left"] {
right: calc(100% + 1rem);
left: auto;
border-right-width: 2px;
}
}
@media print {
.post-note {
display: block !important;
position: static !important;
width: auto !important;
border: 1px dashed #999 !important;
padding: 0.5rem 0.75rem !important;
}
}
+5 -5
View File
@@ -74,7 +74,7 @@ pokemon(wartortle).
pokemon(blastoise). pokemon(blastoise).
``` ```
在 Prolog 中,我们声明"谓词"(Predicate)。谓词定义关系:`bulbasaur` 是一个 `pokemon`,`charmander` 是一个 `pokemon`,以此类推。我们称这个谓词为 `pokemon/1`,因为它有一个参数。 在 Prolog 中,我们声明"谓词"(Predicate)。谓词定义关系:`bulbasaur` 是一个 `pokemon`,`charmander` 是一个 `pokemon`,以此类推。我们称这个谓词为 `pokemon/1`,因为它有一个参数。{{< note >}}斜杠后面的数字是**元数**(arity),表示参数个数。`pokemon/1` 和 `pokemon/2` 在 Prolog 里是两个完全不同的谓词,可以同时存在。{{< /note >}}
这些事实被加载到一个交互式提示符——"顶层"(top-level)中。你输入一个语句到提示符,Prolog 试图找出所有使该语句为真的方式: 这些事实被加载到一个交互式提示符——"顶层"(top-level)中。你输入一个语句到提示符,Prolog 试图找出所有使该语句为真的方式:
@@ -113,7 +113,7 @@ type(blastoise, water).
false. false.
``` ```
Prolog 中首字母大写的名字是**变量**。Prolog 尝试将谓词与变量的所有可能匹配进行"合一": Prolog 中首字母大写的名字是**变量**。{{< note >}}变量也可以写成 `_` 开头,单独的 `_` 是匿名变量,每个 `_` 都互不相同,用来占位而不关心取值。{{< /note >}}Prolog 尝试将谓词与变量的所有可能匹配进行"合一":
``` ```
?- type(squirtle, Type). ?- type(squirtle, Type).
@@ -128,7 +128,7 @@ Prolog 中首字母大写的名字是**变量**。Prolog 尝试将谓词与变
; Type = poison. ; Type = poison.
``` ```
分号 `;` 表示"或"。任何参数都可以是变量,这意味着我们可以从任意方向提问。所有草属性宝可梦有哪些?只需把第一个参数设为变量: 分号 `;` 表示"或"。{{< note >}}顶层不会一次性给出所有答案:按回车只得到第一个解,输入 `;`(或空格)才会回溯继续找下一个。{{< /note >}}任何参数都可以是变量,这意味着我们可以从任意方向提问。所有草属性宝可梦有哪些?只需把第一个参数设为变量:
``` ```
?- type(Pokemon, grass). ?- type(Pokemon, grass).
@@ -168,7 +168,7 @@ Iron Bundle 是一只水/冰属性宝可梦,特攻很高。具体多高?
; ... ; ...
``` ```
Freeze-Dry 是一个特别好的特殊招式。找出所有特攻大于 120、会 Freeze-Dry 的冰属性宝可梦: Freeze-Dry 是一个特别好的特殊招式。找出所有特攻大于 120、会 Freeze-Dry 的冰属性宝可梦:{{< note >}}`#>` 是约束求解库里的比较,作用在变量上;普通 `>` 只能用于已实例化的数值,在变量上会报错。{{< /note >}}
``` ```
?- pokemon_spa(Pokemon, SpA), SpA #> 120, learns(Pokemon, freezedry), type(Pokemon, ice). ?- pokemon_spa(Pokemon, SpA), SpA #> 120, learns(Pokemon, freezedry), type(Pokemon, ice).
@@ -178,7 +178,7 @@ Freeze-Dry 是一个特别好的特殊招式。找出所有特攻大于 120、
; Pokemon = ironbundle, SpA = 124. ; Pokemon = ironbundle, SpA = 124.
``` ```
最后一个概念:**规则**(Rules)。规则有头部和主体,如果主体为真,则规则合一: 最后一个概念:**规则**(Rules)。规则有头部和主体,如果主体为真,则规则合一:{{< note >}}`:-` 读作"如果",逗号是"并且",分号是"或者"。规则的顺序会影响求解效率和回溯顺序。{{< /note >}}
```prolog ```prolog
damaging_move(Move) :- damaging_move(Move) :-
+5
View File
@@ -1,3 +1,8 @@
<script>
// 让 CSS 知道 JS 可用:宽屏边注需要 JS 定位
document.documentElement.classList.add("js");
</script>
<script defer src="/js/notes.js"></script>
<!-- Expand sidebar tree by default --> <!-- Expand sidebar tree by default -->
<script> <script>
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
+6 -3
View File
@@ -1,13 +1,16 @@
{{ define "main" }} {{ define "main" }}
<div class="hx:mx-auto hx:flex hextra-max-page-width"> {{- /* 有边注的页面:宽屏两侧需要留白,因此去掉 TOC 与侧栏占位 */ -}}
{{- $hasNotes := .HasShortcode "note" }}
<div class="hx:mx-auto hx:flex hextra-max-page-width{{ if $hasNotes }} has-notes{{ end }}">
{{ partial "sidebar.html" (dict "context" . "disableSidebar" true {{ partial "sidebar.html" (dict "context" . "disableSidebar" true
"displayPlaceholder" true) }} {{ partial "toc.html" . }} "displayPlaceholder" (not $hasNotes)) }} {{ if not $hasNotes }}{{ partial
"toc.html" . }}{{ end }}
<article <article
class="hx:w-full hx:break-words hx:flex hx:min-h-[calc(100vh-var(--navbar-height))] hx:min-w-0 hx:justify-center hx:pb-8 hx:pr-[calc(env(safe-area-inset-right)-1.5rem)]" class="hx:w-full hx:break-words hx:flex hx:min-h-[calc(100vh-var(--navbar-height))] hx:min-w-0 hx:justify-center hx:pb-8 hx:pr-[calc(env(safe-area-inset-right)-1.5rem)]"
> >
<main <main
id="content" id="content"
class="hx:w-full hx:min-w-0 hextra-max-content-width hx:px-6 hx:pt-0 hx:md:px-12" class="hx:w-full hx:min-w-0 hextra-max-content-width hx:px-6 hx:pt-0 hx:md:px-12{{ if $hasNotes }} notes-page{{ end }}"
> >
{{ partial "breadcrumb.html" (dict "page" . "enable" true) }} {{ if {{ partial "breadcrumb.html" (dict "page" . "enable" true) }} {{ if
.Title }} .Title }}
+9
View File
@@ -0,0 +1,9 @@
{{- /*
边注 / 注释。
宽屏:显示在正文两侧的空白处(由 static/js/notes.js 定位)。
窄屏:以虚线框内嵌在文章中。
用法:{{< note >}}注释内容{{< /note >}}
*/ -}}
{{- $n := add (.Page.Store.Get "noteCount" | default 0) 1 -}}
{{- .Page.Store.Set "noteCount" $n -}}
<sup class="post-note-mark">{{ $n }}</sup><span class="post-note" data-note="{{ $n }}">{{- .Inner | .Page.RenderString (dict "display" "inline") -}}</span>
+72
View File
@@ -0,0 +1,72 @@
// 边注定位:宽屏时把 .post-note 放到正文左右两侧的空白处,
// 窄屏时交还给 CSS(虚线框内嵌)。
(function () {
var WIDE = 1280; // 与 CSS 的 @media (min-width: 80rem) 保持一致
var MIN_GAP = 12; // 同一侧两条注释之间的最小垂直间距
function layout() {
var main = document.querySelector("main.notes-page");
if (!main) return;
var notes = Array.prototype.slice.call(
main.querySelectorAll(".post-note"),
);
if (!notes.length) return;
if (window.innerWidth < WIDE) {
main.classList.remove("notes-active");
notes.forEach(function (note) {
note.classList.remove("placed");
note.removeAttribute("data-side");
note.style.top = "";
});
return;
}
main.classList.add("notes-active");
var mainTop = main.getBoundingClientRect().top;
var sideBottom = { left: -Infinity, right: -Infinity };
notes.forEach(function (note, i) {
var mark = note.previousElementSibling;
if (!mark || !mark.classList.contains("post-note-mark")) {
mark = note;
}
var side = i % 2 === 0 ? "right" : "left";
note.setAttribute("data-side", side);
note.classList.add("placed");
var anchorTop = mark.getBoundingClientRect().top - mainTop;
var height = note.offsetHeight;
var top = Math.max(0, anchorTop - 6);
if (top < sideBottom[side] + MIN_GAP) {
top = sideBottom[side] + MIN_GAP;
}
note.style.top = top + "px";
sideBottom[side] = top + height;
});
}
var timer;
function schedule() {
clearTimeout(timer);
timer = setTimeout(layout, 100);
}
document.addEventListener("DOMContentLoaded", layout);
window.addEventListener("load", layout);
window.addEventListener("resize", schedule);
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(layout);
}
if (window.ResizeObserver) {
document.addEventListener("DOMContentLoaded", function () {
var content = document.querySelector("main.notes-page .content");
if (content) new ResizeObserver(schedule).observe(content);
});
}
})();