Skip to content
22 changes: 22 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,27 @@ h2 a {
font-size: 16px;
}

/* h2 -> h3 header links */
.heading-link::after {
content: '#';
}

.heading-link {
padding-inline: 0.3em;
font-size: inherit;
text-decoration: none;
color: inherit;
opacity: 0;
transition: opacity 0.2s ease-in-out;
}

h2:hover .heading-link,
h3:hover .heading-link,
:target .heading-link,
.heading-link:is(:hover, :focus, :focus-visible) {
opacity: 1;
}

/* search-bar desktop re-sizing */
@media all and (min-width: 950px) {
.algolia-autocomplete {
Expand Down Expand Up @@ -1736,3 +1757,4 @@ blockquote {
border-bottom: 1px solid var(--border);
}
}

11 changes: 11 additions & 0 deletions js/copycode.js
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
Expand All @@ -13,6 +14,15 @@ codeBlocks.forEach((block) => {
});
});

// add heading links
for (const heading of headers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (const heading of headers) {
for (const heading of headers) {
if (h.querySelector('.heading-link')) continue;

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want thinking of implementing lazy attach links using IntersectionObserver or MutationObserver. WDYT?

Is there a reason why this is living inside the copycode.js script file?

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, {
Expand Down Expand Up @@ -57,3 +67,4 @@ function updateButtonState(button, statusClass, ariaLabel) {

button.dataset.timerId = timer;
}