import { docReady } from "./utils.js"; // Borrowed from https://github.com/gohugoio/gohugoioTheme/blob/2e7250ca437d4666329d3ca96708dd3a4ff59818/assets/js/anchorforid.js function anchorForId(id) { const anchor = document.createElement("a"); anchor.className = "header-link"; anchor.title = "Link to this section"; anchor.href = "#" + id; // Icon from https://useiconic.com/open#icons anchor.innerHTML = ''; return anchor; } function anchorizeHeadings() { // If we've found more than 1 article, then abort. It probably means I've // messed something up if this is the case, but I don't have enough // confidence in the way I've set everything up to _not_ do this safety // check. const articles = document.querySelectorAll('article#main'); if (articles.length != 1) { return; } // Keep this list of header classes in sync with style.css const headers = articles[0].querySelectorAll('h2, h3, h4'); Array.prototype.forEach.call(headers, function (el, i) { var link = anchorForId(el.id); el.appendChild(link); }); } export default function anchorizeOnReady() { docReady(anchorizeHeadings); }