Skip to content
Myr

custom.js

This file

Path

docs/js/custom.js

146 lines · 5.6 KiB · JavaScript

Last changed by

🔨 Handle external links `target=_blank` and CSS automatically in JS and CSS (#1622)Sebastián Ramírezbefore adoption3d834e2

History of this file

Take it

The walk stopped after 200 commits: lines older than d73e61b point at it.

⬆ Bump ruff from 0.14.8 to 0.14.10 (#1449)dependabot[bot]before adoptiond73e61b

lines 1–83why, one step back
1function setupTermynal() {
2 document.querySelectorAll(".use-termynal").forEach(node => {3 node.style.display = "block";4 new Termynal(node, {5 lineDelay: 5006 });7 });8 const progressLiteralStart = "---> 100%";9 const promptLiteralStart = "$ ";10 const customPromptLiteralStart = "# ";11 const termynalActivateClass = "termy";12 let termynals = [];1314 function createTermynals() {15 document16 .querySelectorAll(`.${termynalActivateClass} .highlight code`)17 .forEach(node => {18 const text = node.textContent;19 const lines = text.split("\n");20 const useLines = [];21 let buffer = [];22 function saveBuffer() {23 if (buffer.length) {24 let isBlankSpace = true;25 buffer.forEach(line => {26 if (line) {27 isBlankSpace = false;28 }29 });30 dataValue = {};31 if (isBlankSpace) {32 dataValue["delay"] = 0;33 }34 if (buffer[buffer.length - 1] === "") {35 // A last single <br> won't have effect36 // so put an additional one37 buffer.push("");38 }39 const bufferValue = buffer.join("<br>");40 dataValue["value"] = bufferValue;41 useLines.push(dataValue);42 buffer = [];43 }44 }45 for (let line of lines) {46 if (line === progressLiteralStart) {47 saveBuffer();48 useLines.push({49 type: "progress"50 });51 } else if (line.startsWith(promptLiteralStart)) {52 saveBuffer();53 const value = line.replace(promptLiteralStart, "").trimEnd();54 useLines.push({55 type: "input",56 value: value57 });58 } else if (line.startsWith("// ")) {59 saveBuffer();60 const value = "💬 " + line.replace("// ", "").trimEnd();61 useLines.push({62 value: value,63 class: "termynal-comment",64 delay: 065 });66 } else if (line.startsWith(customPromptLiteralStart)) {67 saveBuffer();68 const promptStart = line.indexOf(promptLiteralStart);69 if (promptStart === -1) {70 console.error("Custom prompt found but no end delimiter", line)71 }72 const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "")73 let value = line.slice(promptStart + promptLiteralStart.length);74 useLines.push({75 type: "input",76 value: value,77 prompt: prompt78 });79 } else {80 buffer.push(line);81 }82 }83 saveBuffer();

🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743

lines 84–85why, one step back
84 const inputCommands = useLines.filter(line => line.type === "input").map(line => line.value).join("\n");
85 node.textContent = inputCommands;

⬆ Bump ruff from 0.14.8 to 0.14.10 (#1449)dependabot[bot]before adoptiond73e61b

line 86why, one step back
86 const div = document.createElement("div");

🐛 Fix copy button in `custom.js` (#1488)Alejandrabefore adoption30b0743

lines 87–88why, one step back
87 node.style.display = "none";
88 node.after(div);

⬆ Bump ruff from 0.14.8 to 0.14.10 (#1449)dependabot[bot]before adoptiond73e61b

lines 89–111why, one step back
89 const termynal = new Termynal(div, {
90 lineData: useLines,91 noInit: true,92 lineDelay: 50093 });94 termynals.push(termynal);95 });96 }9798 function loadVisibleTermynals() {99 termynals = termynals.filter(termynal => {100 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {101 termynal.init();102 return false;103 }104 return true;105 });106 }107 window.addEventListener("scroll", loadVisibleTermynals);108 createTermynals();109 loadVisibleTermynals();110}111

🔨 Handle external links `target=_blank` and CSS automatically in JS and CSS (#1622)Sebastián Ramírezbefore adoption3d834e2

lines 112–138why, one step back
112function openLinksInNewTab() {
113 const siteUrl = document.querySelector("link[rel='canonical']")?.href114 || window.location.origin;115 const siteOrigin = new URL(siteUrl).origin;116 document.querySelectorAll(".md-content a[href]").forEach(a => {117 if (a.getAttribute("target") === "_self") return;118 const href = a.getAttribute("href");119 if (!href) return;120 try {121 const url = new URL(href, window.location.href);122 // Skip same-page anchor links (only the hash differs)123 if (url.origin === window.location.origin124 && url.pathname === window.location.pathname125 && url.search === window.location.search) return;126 if (!a.hasAttribute("target")) {127 a.setAttribute("target", "_blank");128 a.setAttribute("rel", "noopener");129 }130 if (url.origin !== siteOrigin) {131 a.dataset.externalLink = "";132 } else {133 a.dataset.internalLink = "";134 }135 } catch (_) {}136 });137}138

⬆ Bump ruff from 0.14.8 to 0.14.10 (#1449)dependabot[bot]before adoptiond73e61b

line 139why, one step back
139async function main() {

🔨 Handle external links `target=_blank` and CSS automatically in JS and CSS (#1622)Sebastián Ramírezbefore adoption3d834e2

lines 140–141why, one step back
140 setupTermynal();
141 openLinksInNewTab();

⬆ Bump ruff from 0.14.8 to 0.14.10 (#1449)dependabot[bot]before adoptiond73e61b

lines 142–146why, one step back
142}
143144document$.subscribe(() => {145 main()146})