Skip to content

Commit 2d7e047

Browse files
authored
feat: skipTo — after the table of contents (#289)
feat: `skipTo` — after the table of contents
2 parents 9821714 + 0fd046c commit 2d7e047

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/assets/js/blog.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1+
const checkTableOfContents = () => {
2+
const toc = document.getElementById("main").querySelector(".table-of-contents")
3+
const skipToMain = document.querySelector('a[href="#main"]')
4+
const navSkip = document.querySelector(".nav-skip")
5+
if (!toc) return
6+
7+
let nextCandidate = toc?.nextElementSibling
8+
nextCandidate.id = "pass-toc"
9+
10+
const skipLink = createSkipLink(nextCandidate.id)
11+
navSkip.insertBefore(skipLink, skipToMain.nextSibling)
12+
}
13+
14+
const createSkipLink = (id, text = "Skip pass the Table of Contents") => {
15+
const skipLink = document.createElement('a')
16+
skipLink.className = 'nav-skipto'
17+
skipLink.href = `#${id}`
18+
skipLink.innerText = text
19+
return skipLink
20+
}
21+
22+
checkTableOfContents()
123
Array.from(document.links).filter(link => link.hostname != window.location.hostname)
2-
.forEach(link => link.target = '_blank')
24+
.forEach(link => link.target = '_blank')

0 commit comments

Comments
 (0)