1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 11:01:17 -04:00

use much, much simpler logic for adding anchors to h[2-4] elements

This commit is contained in:
2021-12-01 15:54:15 -05:00
parent 6f6de426da
commit f7632e0071
3 changed files with 21 additions and 11 deletions

View File

@@ -1,7 +1,23 @@
import SimpleAnchor from "simple-anchor"; // Heavily inspired by AnchorJS: https://github.com/bryanbraun/anchorjs
const anchors = new SimpleAnchor({ // loop through each h2, h3, h4 in this page's content area
icon: null, document.querySelectorAll(["div#content h2", "div#content h3", "div#content h4"]).forEach((h) => {
// don't add to elements without a pre-existing ID (e.g. `<h2 id="...">`)
if (!h.hasAttribute("id")) {
return;
}
// build the anchor link (the "#" icon is added via CSS)
const anchor = document.createElement("a");
anchor.className = "anchorjs-link";
anchor.href = `#${h.getAttribute("id")}`;
// if this is a touchscreen, always show the "#" icon instead waiting for hover
// NOTE: this is notoriously unreliable; see https://github.com/Modernizr/Modernizr/pull/2432
if ("ontouchstart" in window) {
anchor.style.opacity = "1";
}
// add anchor link to the right of the heading
h.appendChild(anchor);
}); });
anchors.add("div#content h2, div#content h3, div#content h4");

View File

@@ -43,7 +43,6 @@
"preact": "^10.6.2", "preact": "^10.6.2",
"query-string": "^7.0.1", "query-string": "^7.0.1",
"rss-parser": "^3.12.0", "rss-parser": "^3.12.0",
"simple-anchor": "^1.0.3",
"trim-newlines": "^4.0.2", "trim-newlines": "^4.0.2",
"twemoji": "^13.1.0", "twemoji": "^13.1.0",
"twemoji-emojis": "^14.1.0", "twemoji-emojis": "^14.1.0",

View File

@@ -8746,11 +8746,6 @@ signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"
integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ== integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ==
simple-anchor@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/simple-anchor/-/simple-anchor-1.0.3.tgz#9e23e3c53f6cd913e576e44a2b1cb666c4e553d1"
integrity sha512-g2CrKyqfxRzEpDuuLWsGaDrN/WlU+oft1xGkiGTUB73SiG4MmqW0YMBibQ32pcu4jpL4fnEotggpMpa+ddVM3g==
simple-git-hooks@^2.7.0: simple-git-hooks@^2.7.0:
version "2.7.0" version "2.7.0"
resolved "https://registry.yarnpkg.com/simple-git-hooks/-/simple-git-hooks-2.7.0.tgz#121a5c3023663b8abcc5648c8bfe8619dc263705" resolved "https://registry.yarnpkg.com/simple-git-hooks/-/simple-git-hooks-2.7.0.tgz#121a5c3023663b8abcc5648c8bfe8619dc263705"