From 88753d6a5a0a6ca82fd87ec9121a2d16eeaae56d Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Thu, 20 Nov 2025 21:55:57 +0000 Subject: [PATCH] refactor(docs-infra): improve code efficiency for adding line numbers Only run the code for adding line numbers if line numbers are required, otherwise skip the code block --- .../extensions/docs-code/format/highlight.mts | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format/highlight.mts b/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format/highlight.mts index d710d5adbd8..0255ef1d43b 100644 --- a/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format/highlight.mts +++ b/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/format/highlight.mts @@ -30,35 +30,25 @@ export function highlightCode(highlighter: HighlighterGeneric, token: token.code = codeToHtml(highlighter, token.code, {language, highlight}); } - const dom = new JSDOM(token.code); - const document = dom.window.document; - const lines = document.body.querySelectorAll('.line'); + if (token.linenums) { + const dom = new JSDOM(token.code); + const document = dom.window.document; + const lines = document.body.querySelectorAll('.line'); - const linesCount = lines.length; - if (linesCount === 0) { - return; - } - - let lineIndex = 0; - let resultFileLineIndex = 1; - - do { - const currentline = lines[lineIndex]; - - if (!!token.linenums) { - const lineNumberEl = JSDOM.fragment( - ``, - ).firstElementChild!; - lineNumberEl.textContent = `${resultFileLineIndex}`; - - currentline.parentElement!.insertBefore(lineNumberEl, currentline); - resultFileLineIndex++; + const linesCount = lines.length; + if (linesCount === 0) { + return; } - lineIndex++; - } while (lineIndex < linesCount); + lines.forEach((lineEl, idx) => { + const lineNumberEl = JSDOM.fragment( + `${idx + 1}`, + ).firstElementChild!; + lineEl.parentElement!.insertBefore(lineNumberEl, lineEl); + }); - token.code = document.body.innerHTML; + token.code = document.body.innerHTML; + } } function guessLanguageFromPath(path: string | undefined): string {