Files
timi-web/src/utils/PrismCodeBlock.ts
T
2026-08-21 01:11:23 +08:00

124 lines
3.3 KiB
TypeScript

export function enhancePrismCodeBlock(root: ParentNode) {
const codeList = Array.from(root.querySelectorAll("pre > code[class*='language-'], pre[class*='language-'] > code"));
for (const code of codeList) {
if (!(code instanceof HTMLElement)) {
continue;
}
const pre = code.parentElement;
if (!(pre instanceof HTMLElement)) {
continue;
}
syncPreLanguageClass(pre, code);
resetCodeBlock(code);
normalizeCodeContent(code);
const maxHeight = resolveCodeBlockMaxHeight(pre);
if (maxHeight && maxHeight !== "auto") {
pre.style.maxHeight = maxHeight;
} else {
pre.style.removeProperty("max-height");
}
const lineCount = buildLineCount(code.textContent || "");
const rows = buildLineNumberRows(lineCount);
const codes = document.createElement("span");
codes.className = "codes";
codes.innerHTML = code.innerHTML;
pre.classList.add("has-line-numbers");
code.replaceChildren(rows, codes);
bindCodeBlockToggle(pre, code, lineCount, maxHeight);
}
}
function syncPreLanguageClass(pre: HTMLElement, code: HTMLElement) {
const classList = Array.from(code.classList).filter((item) => item.startsWith("language-"));
if (!classList.length) {
return;
}
for (const className of classList) {
if (!pre.classList.contains(className)) {
pre.classList.add(className);
}
}
}
function resetCodeBlock(code: HTMLElement) {
code.parentElement?.classList.remove("has-line-numbers");
const codes = code.querySelector(":scope > .codes");
if (codes instanceof HTMLElement) {
code.innerHTML = codes.innerHTML;
}
}
function normalizeCodeContent(code: HTMLElement) {
const firstNode = code.firstChild;
if (firstNode instanceof Text && firstNode.data.startsWith("\n")) {
firstNode.data = firstNode.data.slice(1);
if (!firstNode.data) {
firstNode.remove();
}
}
const lastNode = code.lastChild;
if (lastNode instanceof Text && lastNode.data.endsWith("\n")) {
lastNode.data = lastNode.data.replace(/\n+$/, "");
if (!lastNode.data) {
lastNode.remove();
}
}
}
function resolveCodeBlockMaxHeight(pre: HTMLElement) {
const container = pre.closest("[data-max-height]");
if (!(container instanceof HTMLElement)) {
return "";
}
return container.dataset.maxHeight || "";
}
function buildLineCount(text: string) {
if (!text) {
return 1;
}
return Math.max(text.split("\n").length, 1);
}
function buildLineNumberRows(lineCount: number) {
const rows = document.createElement("span");
rows.className = "line-numbers-rows diselect";
for (let i = 0; i < lineCount; i += 1) {
const row = document.createElement("span");
row.textContent = `${i + 1}`;
rows.appendChild(row);
}
return rows;
}
function bindCodeBlockToggle(pre: HTMLElement, code: HTMLElement, lineCount: number, maxHeight: string) {
pre.classList.remove("expand");
pre.dataset.lineCount = `${lineCount}`;
pre.ondblclick = null;
if (!maxHeight || maxHeight === "auto" || lineCount < 18) {
return;
}
pre.ondblclick = () => {
const isExpand = pre.classList.contains("expand");
if (isExpand) {
pre.style.maxHeight = maxHeight;
pre.classList.remove("expand");
} else {
const lineHeight = Number.parseFloat(getComputedStyle(code).lineHeight || "22") || 22;
pre.style.maxHeight = `${Math.ceil(lineCount * lineHeight + 2)}px`;
pre.classList.add("expand");
}
window.getSelection()?.removeAllRanges();
};
}