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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170function escapePlainCliLine(line) { return line .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">");}
function prepareTermynalLines(lines, isRichHtml) { if (isRichHtml) { return lines.join("<br>"); } return lines.map(escapePlainCliLine).join("<br>");}
function setupTermynal() { document.querySelectorAll(".use-termynal").forEach(node => { node.style.display = "block"; new Termynal(node, { lineDelay: 500 }); }); const progressLiteralStart = "---> 100%"; const promptLiteralStart = "$ "; const customPromptLiteralStart = "# "; const termynalActivateClass = "termy"; let termynals = [];
function createTermynals() { document .querySelectorAll(`.${termynalActivateClass} .highlight code`) .forEach(node => { const text = node.textContent; const lines = text.split("\n"); // If it is a type in brackets like <str> or <str int boolean>, it won't have // HTML attributes (=) or a closing tag, while Rich HTML tags do. const hasHtmlAttribute = /<[A-Za-z][^>]*=/.test(text); const hasClosingTag = /<\/[a-zA-Z0-9]/.test(text); const isRichHtml = hasHtmlAttribute || hasClosingTag; const useLines = []; let buffer = []; function saveBuffer() { if (buffer.length) { let isBlankSpace = true; buffer.forEach(line => { if (line) { isBlankSpace = false; } }); dataValue = {}; if (isBlankSpace) { dataValue["delay"] = 0; } if (buffer[buffer.length - 1] === "") { // A last single <br> won't have effect // so put an additional one buffer.push(""); } const bufferValue = prepareTermynalLines(buffer, isRichHtml); dataValue["value"] = bufferValue; useLines.push(dataValue); buffer = []; } } for (let line of lines) { if (line === progressLiteralStart) { saveBuffer(); useLines.push({ type: "progress" }); } else if (line.startsWith(promptLiteralStart)) { saveBuffer(); const value = line.replace(promptLiteralStart, "").trimEnd(); useLines.push({ type: "input", value: value }); } else if (line.startsWith("// ")) { saveBuffer(); let comment = line.replace("// ", "").trimEnd(); if (!isRichHtml) { comment = escapePlainCliLine(comment); } const value = "💬 " + comment; useLines.push({ value: value, class: "termynal-comment", delay: 0 }); } else if (line.startsWith(customPromptLiteralStart)) { saveBuffer(); const promptStart = line.indexOf(promptLiteralStart); if (promptStart === -1) { console.error("Custom prompt found but no end delimiter", line) } const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "") let value = line.slice(promptStart + promptLiteralStart.length); useLines.push({ type: "input", value: value, prompt: prompt }); } else { buffer.push(line); } } saveBuffer(); const inputCommands = useLines.filter(line => line.type === "input").map(line => line.value).join("\n"); node.textContent = inputCommands; const div = document.createElement("div"); node.style.display = "none"; node.after(div); const termynal = new Termynal(div, { lineData: useLines, noInit: true, lineDelay: 500 }); termynals.push(termynal); }); }
function loadVisibleTermynals() { termynals = termynals.filter(termynal => { if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) { termynal.init(); return false; } return true; }); } window.addEventListener("scroll", loadVisibleTermynals); createTermynals(); loadVisibleTermynals();}
function openLinksInNewTab() { const siteUrl = document.querySelector("link[rel='canonical']")?.href || window.location.origin; const siteOrigin = new URL(siteUrl).origin; document.querySelectorAll(".md-content a[href]").forEach(a => { if (a.getAttribute("target") === "_self") return; const href = a.getAttribute("href"); if (!href) return; try { const url = new URL(href, window.location.href); // Skip same-page anchor links (only the hash differs) if (url.origin === window.location.origin && url.pathname === window.location.pathname && url.search === window.location.search) return; if (!a.hasAttribute("target")) { a.setAttribute("target", "_blank"); a.setAttribute("rel", "noopener"); } if (url.origin !== siteOrigin) { a.dataset.externalLink = ""; } else { a.dataset.internalLink = ""; } } catch (_) {} });}
async function main() { setupTermynal(); openLinksInNewTab();}
document$.subscribe(() => { main()})