Skip to content

Commit 1f0af9f

Browse files
committed
Make old paragraph bookmarks forward to the 3rd edition
1 parent 1562661 commit 1f0af9f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/client/ejs.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,17 @@ function debounce(fn, delay = 50) {
339339
}
340340
}
341341

342-
if (window.page && /^chapter|hints$/.test(window.page.type)) chapterInteraction()
342+
if (window.page && /^chapter|hints$/.test(window.page.type)) {
343+
chapterInteraction()
344+
// 3rd-edition-style anchor
345+
let {hash} = document.location
346+
if (/^#[phic]_./.test(hash)) {
347+
let exists = document.getElementById(hash.replace(/_/, "-"))
348+
if (exists) {
349+
document.location.hash = hash.replace(/_/, "-")
350+
} else {
351+
let chapter = /\/[^\/]+\.html/.exec(document.location)
352+
if (chapter) document.location = `https://eloquentjavascript.net/3rd_edition${chapter[0]}${hash}`
353+
}
354+
}
355+
}

src/transform.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ export function transformTokens(tokens, options) {
112112
i += 2
113113
} else {
114114
if (type == "paragraph_open")
115-
tok.hashID = "p_" + hash(startAndEnd(childrenText(tokens[i + 1])))
115+
tok.hashID = "p-" + hash(startAndEnd(childrenText(tokens[i + 1])))
116116
else if (type == "heading_open")
117-
tok.hashID = (tok.tag == "h2" ? "h_" : "i_") + hash(childrenText(tokens[i + 1]))
117+
tok.hashID = (tok.tag == "h2" ? "h-" : "i-") + hash(childrenText(tokens[i + 1]))
118118
else if (type == "fence")
119-
tok.hashID = "c_" + hash(tok.content)
119+
tok.hashID = "c-" + hash(tok.content)
120120

121121
if (tok.children) tok.children = transformInline(tok.children, options, tokens[i - 1].type)
122122

0 commit comments

Comments
 (0)