-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Update docs Termynal scripts to not include line nums for local dev (…
- Loading branch information
Showing
2 changed files
with
109 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,113 @@ | ||
document.querySelectorAll(".use-termynal").forEach(node => { | ||
node.style.display = "block"; | ||
new Termynal(node, { | ||
lineDelay: 500 | ||
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 = []; | ||
const progressLiteralStart = "---> 100%"; | ||
const promptLiteralStart = "$ "; | ||
const customPromptLiteralStart = "# "; | ||
const termynalActivateClass = "termy"; | ||
let termynals = []; | ||
|
||
function createTermynals() { | ||
document | ||
.querySelectorAll(`.${termynalActivateClass} .highlight`) | ||
.forEach(node => { | ||
const text = node.textContent; | ||
const lines = text.split("\n"); | ||
const useLines = []; | ||
let buffer = []; | ||
function saveBuffer() { | ||
if (buffer.length) { | ||
let isBlankSpace = true; | ||
buffer.forEach(line => { | ||
if (line) { | ||
isBlankSpace = false; | ||
function createTermynals() { | ||
document | ||
.querySelectorAll(`.${termynalActivateClass} .highlight code`) | ||
.forEach(node => { | ||
const text = node.textContent; | ||
const lines = text.split("\n"); | ||
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; | ||
} | ||
}); | ||
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(""); | ||
if (buffer[buffer.length - 1] === "") { | ||
// A last single <br> won't have effect | ||
// so put an additional one | ||
buffer.push(""); | ||
} | ||
const bufferValue = buffer.join("<br>"); | ||
dataValue["value"] = bufferValue; | ||
useLines.push(dataValue); | ||
buffer = []; | ||
} | ||
const bufferValue = buffer.join("<br>"); | ||
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(); | ||
const value = "💬 " + line.replace("// ", "").trimEnd(); | ||
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) | ||
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(); | ||
const value = "💬 " + line.replace("// ", "").trimEnd(); | ||
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); | ||
} | ||
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 div = document.createElement("div"); | ||
node.replaceWith(div); | ||
const termynal = new Termynal(div, { | ||
lineData: useLines, | ||
noInit: true, | ||
lineDelay: 500 | ||
saveBuffer(); | ||
const div = document.createElement("div"); | ||
node.replaceWith(div); | ||
const termynal = new Termynal(div, { | ||
lineData: useLines, | ||
noInit: true, | ||
lineDelay: 500 | ||
}); | ||
termynals.push(termynal); | ||
}); | ||
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 loadVisibleTermynals() { | ||
termynals = termynals.filter(termynal => { | ||
if (termynal.container.getBoundingClientRect().top - innerHeight <= 0) { | ||
termynal.init(); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
async function main() { | ||
setupTermynal() | ||
} | ||
window.addEventListener("scroll", loadVisibleTermynals); | ||
createTermynals(); | ||
loadVisibleTermynals(); | ||
|
||
main() |