-
-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swap to use IdAttributePlugin instead of markdown-it-anchor
- Loading branch information
Showing
5 changed files
with
74 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Thank you to https://github.com/daviddarnes/heading-anchors | ||
|
||
class HeadingAnchors extends HTMLElement { | ||
static register(tagName) { | ||
if ("customElements" in window) { | ||
customElements.define(tagName || "heading-anchors", HeadingAnchors); | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
this.headings.forEach((heading) => { | ||
if(!heading.querySelector("a.direct-link") || heading.hasAttribute("data-heading-anchors-optout")) { | ||
heading.append(this.anchor(heading)); | ||
} | ||
}); | ||
} | ||
|
||
anchor(heading) { | ||
// TODO this would be good use case for shadow dom | ||
let anchor = document.createElement("a"); | ||
anchor.setAttribute("data-pagefind-ignore", ""); | ||
anchor.href = `#${heading.id}`; | ||
anchor.classList.add("heading-anchor"); | ||
anchor.innerHTML = `<span class="visually-hidden">Jump to heading</span><span aria-hidden="true">#</span>`; | ||
return anchor; | ||
} | ||
|
||
get headings() { | ||
return this.querySelectorAll(this.selector.split(",").map(entry => `${entry.trim()}[id]`)); | ||
} | ||
|
||
get selector() { | ||
return this.getAttribute("selector") || "h1,h2,h3,h4" | ||
} | ||
} | ||
|
||
HeadingAnchors.register(); |