Skip to content

Commit d044765

Browse files
authored
Merge pull request #600 from raifdmueller/fix/599-legacy-hash-links
fix: legacy hash links in doc content are dead on pre-rendered pages
2 parents dc1772c + a5b49d3 commit d044765

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

scripts/render-contracts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function renderContract(contract) {
4444
const anchors = contract.anchors
4545
.map(
4646
(id) =>
47-
`<a href="#/anchor/${escapeHtml(id)}" class="inline-block rounded-full bg-blue-100 dark:bg-blue-900/30 px-2 py-0.5 text-xs text-blue-700 dark:text-blue-300">${escapeHtml(id)}</a>`
47+
`<a href="/Semantic-Anchors/anchor/${escapeHtml(id)}" class="inline-block rounded-full bg-blue-100 dark:bg-blue-900/30 px-2 py-0.5 text-xs text-blue-700 dark:text-blue-300">${escapeHtml(id)}</a>`
4848
)
4949
.join(' ')
5050

scripts/render-docs.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,23 @@ function extractToc(html) {
7777
* sidecar `<basename>.toc.html` file so doc-page.js can render it in its
7878
* own sidebar slot.
7979
*/
80+
/**
81+
* Rewrite legacy hash-router links (href="#/anchor/x", href="#/about", …)
82+
* that authors used in the .adoc sources to real clean URLs, so the
83+
* pre-rendered pages are navigable without JavaScript and crawlable (#599).
84+
* In-page TOC/section links (href="#section-id", no slash) stay untouched.
85+
* With JS the router intercepts the clean URLs for SPA navigation.
86+
*/
87+
function rewriteLegacyHashLinks(html) {
88+
return html.replace(/href="#\//g, 'href="/Semantic-Anchors/')
89+
}
90+
8091
function renderFile(srcPath, destPath, quiet = false) {
8192
if (!fs.existsSync(srcPath)) return
8293
try {
8394
fs.mkdirSync(path.dirname(destPath), { recursive: true })
8495
const html = String(asciidoctor.convertFile(srcPath, { ...OPTS, to_file: false }))
85-
const { toc, body } = extractToc(html)
96+
const { toc, body } = extractToc(rewriteLegacyHashLinks(html))
8697
fs.writeFileSync(destPath, body, 'utf-8')
8798
if (!quiet) console.log(`Rendered: ${path.relative(ROOT, destPath)}`)
8899
const tocPath = destPath.replace(/\.html$/, '.toc.html')

0 commit comments

Comments
 (0)