custom.js
This file
Path
docs/js/custom.js
105 lines · 3.8 KiB · JavaScript
Last changed by
:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4
History of this fileTake it
:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 1–2why, one step back1document.querySelectorAll(".use-termynal").forEach(node => {2 node.style.display = "block";:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 3–5why, one step back3 new Termynal(node, {4 lineDelay: 5005 });:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 6–8why, one step back6});7const progressLiteralStart = "---> 100%";8const promptLiteralStart = "$ ";:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4
line 9why, one step back9const customPromptLiteralStart = "# ";:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 10why, one step back10const termynalActivateClass = "termy";:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 11why, one step back11let termynals = [];:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 12why, one step back12:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 13why, one step back13function createTermynals() {:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 14–16why, one step back14 document15 .querySelectorAll(`.${termynalActivateClass} .codehilite`)16 .forEach(node => {:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 17–26why, one step back17 const text = node.textContent;18 const lines = text.split("\n");19 const useLines = [];20 let buffer = [];21 function saveBuffer() {22 if (buffer.length) {23 let isBlankSpace = true;24 buffer.forEach(line => {25 if (line) {26 isBlankSpace = false;:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 27why, one step back27 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 28–31why, one step back28 });29 dataValue = {};30 if (isBlankSpace) {31 dataValue["delay"] = 0;:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 32why, one step back32 }:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4
lines 33–34why, one step back33 if (buffer[buffer.length - 1] === "") {34 // A last single <br> won't have effect:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 35–36why, one step back35 // so put an additional one36 buffer.push("");:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 37why, one step back37 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 38–41why, one step back38 const bufferValue = buffer.join("<br>");39 dataValue["value"] = bufferValue;40 useLines.push(dataValue);41 buffer = [];:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 42–43why, one step back42 }43 }:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 44–58why, one step back44 for (let line of lines) {45 if (line === progressLiteralStart) {46 saveBuffer();47 useLines.push({48 type: "progress"49 });50 } else if (line.startsWith(promptLiteralStart)) {51 saveBuffer();52 const value = line.replace(promptLiteralStart, "").trimEnd();53 useLines.push({54 type: "input",55 value: value56 });57 } else if (line.startsWith("// ")) {58 saveBuffer();:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4
line 59why, one step back59 const value = "💬 " + line.replace("// ", "").trimEnd();:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 60–63why, one step back60 useLines.push({61 value: value,62 class: "termynal-comment",63 delay: 0:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4
lines 64–76why, one step back64 });65 } else if (line.startsWith(customPromptLiteralStart)) {66 saveBuffer();67 const promptStart = line.indexOf(promptLiteralStart);68 if (promptStart === -1) {69 console.error("Custom prompt found but no end delimiter", line)70 }71 const prompt = line.slice(0, promptStart).replace(customPromptLiteralStart, "")72 let value = line.slice(promptStart + promptLiteralStart.length);73 useLines.push({74 type: "input",75 value: value,76 prompt: prompt:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 77–90why, one step back77 });78 } else {79 buffer.push(line);80 }81 }82 saveBuffer();83 const div = document.createElement("div");84 node.replaceWith(div);85 const termynal = new Termynal(div, {86 lineData: useLines,87 noInit: true,88 lineDelay: 50089 });90 termynals.push(termynal);:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
lines 91–92why, one step back91 });92}:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
lines 93–102why, one step back9394function loadVisibleTermynals() {95 termynals = termynals.filter(termynal => {96 if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) {97 termynal.init();98 return false;99 }100 return true;101 });102}:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 103why, one step back103window.addEventListener("scroll", loadVisibleTermynals);:sparkles: Fix Termynal size, update first steps, add fast buttonSebastián Ramírezbefore adoption234e50a
line 104why, one step back104createTermynals();:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6
line 105why, one step back105loadVisibleTermynals();