Skip to content

Commit

Permalink
feat: add heading links to PostDetails page (#232)
Browse files Browse the repository at this point in the history
- implement easy links to headings (h2-h6) on post details
- the links are aria-hidden and only show on hover
  • Loading branch information
benjaminrae authored Jan 15, 2024
1 parent 742314e commit 742baff
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/layouts/PostDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ const layoutProps = {
</style>

<script is:inline>
/** Attaches links to headings in the document,
* allowing sharing of sections easily */
function addHeadingLinks() {
let headings = Array.from(document.querySelectorAll("h2, h3, h4, h5, h6"));
for (let heading of headings) {
heading.classList.add("group")
let link = document.createElement("a");
link.innerText = "#";
link.className = "heading-link hidden group-hover:inline-block ml-2";
link.href = "#" + heading.id;
link.ariaHidden = true;
heading.appendChild(link);
}
}
addHeadingLinks();

/** Attaches copy buttons to code blocks in the document,
* allowing users to copy code easily. */
function attachCopyButtons() {
Expand Down

0 comments on commit 742baff

Please sign in to comment.