Skip to content
Myr

custom.js

This file

Path

docs/js/custom.js

169 lines · 6.5 KiB · JavaScript

Last changed by

💥 Update metavar printing (#1863)Sofie Van Landeghembefore adoption42196d4

History of this file

Take it

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

💥 Update metavar printing (#1863)Sofie Van Landeghembefore adoption42196d4

lines 1–14why, one step back
1function escapePlainCliLine(line) {
2 return line3 .replace(/&/g, "&amp;")4 .replace(/</g, "&lt;")5 .replace(/>/g, "&gt;");6}78function prepareTermynalLines(lines, isRichHtml) {9 if (isRichHtml) {10 return lines.join("<br>");11 }12 return lines.map(escapePlainCliLine).join("<br>");13}14

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 15–33why, one step back
15function setupTermynal() {
16 document.querySelectorAll(".use-termynal").forEach(node => {17 node.style.display = "block";18 new Termynal(node, {19 lineDelay: 50020 });21 });22 const progressLiteralStart = "---> 100%";23 const promptLiteralStart = "$ ";24 const customPromptLiteralStart = "# ";25 const termynalActivateClass = "termy";26 let termynals = [];2728 function createTermynals() {29 document30 .querySelectorAll(`.${termynalActivateClass} .highlight code`)31 .forEach(node => {32 const text = node.textContent;33 const lines = text.split("\n");

💥 Update metavar printing (#1863)Sofie Van Landeghembefore adoption42196d4

lines 34–38why, one step back
34 // If it is a type in brackets like <str> or <str int boolean>, it won't have
35 // HTML attributes (=) or a closing tag, while Rich HTML tags do.36 const hasHtmlAttribute = /<[A-Za-z][^>]*=/.test(text);37 const hasClosingTag = /<\/[a-zA-Z0-9]/.test(text);38 const isRichHtml = hasHtmlAttribute || hasClosingTag;

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 39–57why, one step back
39 const useLines = [];
40 let buffer = [];41 function saveBuffer() {42 if (buffer.length) {43 let isBlankSpace = true;44 buffer.forEach(line => {45 if (line) {46 isBlankSpace = false;47 }48 });49 dataValue = {};50 if (isBlankSpace) {51 dataValue["delay"] = 0;52 }53 if (buffer[buffer.length - 1] === "") {54 // A last single <br> won't have effect55 // so put an additional one56 buffer.push("");57 }

💥 Update metavar printing (#1863)Sofie Van Landeghembefore adoption42196d4

line 58why, one step back
58 const bufferValue = prepareTermynalLines(buffer, isRichHtml);

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 59–78why, one step back
59 dataValue["value"] = bufferValue;
60 useLines.push(dataValue);61 buffer = [];62 }63 }64 for (let line of lines) {65 if (line === progressLiteralStart) {66 saveBuffer();67 useLines.push({68 type: "progress"69 });70 } else if (line.startsWith(promptLiteralStart)) {71 saveBuffer();72 const value = line.replace(promptLiteralStart, "").trimEnd();73 useLines.push({74 type: "input",75 value: value76 });77 } else if (line.startsWith("// ")) {78 saveBuffer();

💥 Update metavar printing (#1863)Sofie Van Landeghembefore adoption42196d4

lines 79–83why, one step back
79 let comment = line.replace("// ", "").trimEnd();
80 if (!isRichHtml) {81 comment = escapePlainCliLine(comment);82 }83 const value = "💬 " + comment;

📝 Update link syntax to minimal Markdown (#1623)Sebastián Ramírezbefore adoption3780a29

lines 84–169why, one step back
84 useLines.push({
85 value: value,86 class: "termynal-comment",87 delay: 088 });89 } else if (line.startsWith(customPromptLiteralStart)) {90 saveBuffer();91 const promptStart = line.indexOf(promptLiteralStart);92 if (promptStart === -1) {93 console.error("Custom prompt found but no end delimiter", line)94 }95 const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "")96 let value = line.slice(promptStart + promptLiteralStart.length);97 useLines.push({98 type: "input",99 value: value,100 prompt: prompt101 });102 } else {103 buffer.push(line);104 }105 }106 saveBuffer();107 const inputCommands = useLines.filter(line => line.type === "input").map(line => line.value).join("\n");108 node.textContent = inputCommands;109 const div = document.createElement("div");110 node.style.display = "none";111 node.after(div);112 const termynal = new Termynal(div, {113 lineData: useLines,114 noInit: true,115 lineDelay: 500116 });117 termynals.push(termynal);118 });119 }120121 function loadVisibleTermynals() {122 termynals = termynals.filter(termynal => {123 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {124 termynal.init();125 return false;126 }127 return true;128 });129 }130 window.addEventListener("scroll", loadVisibleTermynals);131 createTermynals();132 loadVisibleTermynals();133}134135function openLinksInNewTab() {136 const siteUrl = document.querySelector("link[rel='canonical']")?.href137 || window.location.origin;138 const siteOrigin = new URL(siteUrl).origin;139 document.querySelectorAll(".md-content a[href]").forEach(a => {140 if (a.getAttribute("target") === "_self") return;141 const href = a.getAttribute("href");142 if (!href) return;143 try {144 const url = new URL(href, window.location.href);145 // Skip same-page anchor links (only the hash differs)146 if (url.origin === window.location.origin147 && url.pathname === window.location.pathname148 && url.search === window.location.search) return;149 if (!a.hasAttribute("target")) {150 a.setAttribute("target", "_blank");151 a.setAttribute("rel", "noopener");152 }153 if (url.origin !== siteOrigin) {154 a.dataset.externalLink = "";155 } else {156 a.dataset.internalLink = "";157 }158 } catch (_) {}159 });160}161162async function main() {163 setupTermynal();164 openLinksInNewTab();165}166167document$.subscribe(() => {168 main()169})