1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-10-29 02:15:49 -04:00

clean up some client JS

This commit is contained in:
2021-10-31 12:48:50 -04:00
parent b894f96bbe
commit 257dff937c
4 changed files with 18 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import SimpleAnchor from "simple-anchor";
const anchors = new SimpleAnchor({
icon: "",
icon: null,
});
anchors.add("div#content h2, div#content h3, div#content h4");

View File

@@ -5,8 +5,8 @@ import trimNewlines from "trim-newlines";
const defaultTerm = "Copy";
const successTerm = "Copied!";
// immediately give up if not supported
if (ClipboardJS.isSupported()) {
// immediately give up if not supported or if there are no code blocks
if (ClipboardJS.isSupported() && document.querySelector("div.highlight")) {
// loop through each code fence on page (if any)
document.querySelectorAll("div.highlight").forEach((highlightDiv) => {
const wrapperDiv = document.createElement("div");
@@ -22,10 +22,10 @@ if (ClipboardJS.isSupported()) {
wrapperDiv.insertBefore(button, wrapperDiv.firstChild);
});
// now that all the buttons are in place, bind them to the copy action
new ClipboardJS("button.copy-button", {
// actual code element will (should) have class "language-*", even if plaintext
text: (trigger) =>
// eslint-disable-next-line quotes
// actual code element will have class "language-*", even if plaintext
trimNewlines(trigger.parentElement.querySelector('code[class^="language-"]').innerText),
}).on("success", (e) => {
// show a subtle indication of success

View File

@@ -8,18 +8,18 @@ const HITS_ENDPOINT = "/api/hits/";
// don't continue if there isn't a span#meta-hits element on this page
const wrapper = document.getElementById("meta-hits");
// use <link rel="canonical"> to deduce a consistent identifier for this page
const canonical = canonicalUrl({
normalize: true,
normalizeOptions: {
removeTrailingSlash: true,
removeQueryParameters: true,
stripHash: true,
},
});
// page must have both span#meta-hits and canonical URL to enter
if (wrapper && canonical) {
if (wrapper) {
// use <link rel="canonical"> to deduce a consistent identifier for this page
const canonical = canonicalUrl({
normalize: true,
normalizeOptions: {
removeTrailingSlash: true,
removeQueryParameters: true,
stripHash: true,
},
});
// javascript is enabled so show the loading indicator
wrapper.style.display = "inline-flex";

View File

@@ -17,7 +17,8 @@ initDarkMode({
// make toggle visible now that we know JS is enabled
t.style.display = "block";
// HACK: re-enable theme transitions after a very short delay, otherwise there's a weird race condition (2/2)
// HACK: re-enable theme transitions after a very short delay, otherwise
// there's a weird race condition (2/2)
setTimeout(() => {
document.head.removeChild(disableTransitionCSSHack);
}, 500);