Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Update application.js to make heading links accessible #373

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,39 @@ const addHeadingLinks = () => {
};

const headings = document.querySelectorAll(
"main h1, main h2, main h3, main h4, main h5, main h6"
"main h2, main h3, main h4, main h5, main h6"
);
for (const heading of headings) {
if (!heading.id) {
heading.id = slugify(heading.innerText);
}

heading.innerHTML = `${heading.innerText} <a href="#${heading.id}" aria-hidden="true" tabindex="-1" class="usa-link heading-link--symbol">#</a>`;
heading.innerHTML = `${heading.innerText} <a href="#${heading.id}" aria-label="Permanent link to ${heading.innerText}" class="usa-link heading-permalink heading-link--symbol"><svg class="usa-icon" aria-hidden="true" focusable="false" role="img">
<use xlink:href="#svg-link"></use>
</svg></a>`;
}
};

const markHeadingLink = () => {
const externalLinkIcon = document.createElement("img");
externalLinkIcon.setAttribute(
"src",
`{{ site.baseurl }}/assets/uswds/img/usa-icons/link.svg`
);
externalLinkIcon.setAttribute("alt", "(external link)");
externalLinkIcon.setAttribute("style", "width: 1rem;");

Array.from(document.querySelectorAll("main a[href]"))
.filter((a) => {
const href = a.getAttribute("href");
return (
!href.startsWith(window.location.origin) &&
!/^[/#]/.test(a.getAttribute("href"))
);
})
.forEach((a) => a.appendChild(externalLinkIcon.cloneNode()));
};

const markExternalLinks = () => {
const externalLinkIcon = document.createElement("img");
externalLinkIcon.setAttribute(
Expand Down