-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: generate dynamic docs header links #2055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
0006a2e
d6bbad9
7a564ea
8fd9b24
9317fd8
77deb08
65b920f
5ebee47
7498eb3
3e31697
4e8a859
2d3f972
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||
const codeBlocks = document.querySelectorAll("pre:has(code)"); | ||||||||
const headers = document.querySelectorAll('main :is(h2,h3)[id]'); | ||||||||
|
||||||||
codeBlocks.forEach((block) => { | ||||||||
// Only add button if browser supports Clipboard API | ||||||||
|
@@ -13,6 +14,15 @@ codeBlocks.forEach((block) => { | |||||||
}); | ||||||||
}); | ||||||||
|
||||||||
// add heading links | ||||||||
for (const heading of headers) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: A little paranoid but this will guard against the script running twice somehow by accident or hot reloading or something, preventing us from adding the elements twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want thinking of implementing lazy attach links using
Copy code and header link share same logic and needs asynchronous implementation. |
||||||||
const linkIcon = document.createElement('a'); | ||||||||
linkIcon.setAttribute('href', `#${heading.id}`); | ||||||||
linkIcon.classList.add('heading-link'); | ||||||||
linkIcon.setAttribute("aria-label", `to section ${heading.textContent.trim()}`); | ||||||||
heading.appendChild(linkIcon); | ||||||||
} | ||||||||
|
||||||||
function createCopyButton() { | ||||||||
const button = document.createElement("button"); | ||||||||
setButtonAttributes(button, { | ||||||||
|
@@ -57,3 +67,4 @@ function updateButtonState(button, statusClass, ariaLabel) { | |||||||
|
||||||||
button.dataset.timerId = timer; | ||||||||
} | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.