From 03b05115920ba848cf124188db8e7edde64957bb Mon Sep 17 00:00:00 2001 From: Ingwannu Date: Sat, 8 Aug 2026 07:06:57 +0000 Subject: [PATCH] fix(ci): preserve code while normalizing issue media --- .github/scripts/issue-quality-core.cjs | 81 ++++++++++++++++++-------- .github/scripts/issue-quality.test.cjs | 33 +++++++++++ 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/.github/scripts/issue-quality-core.cjs b/.github/scripts/issue-quality-core.cjs index 817624f46f..66adc90493 100644 --- a/.github/scripts/issue-quality-core.cjs +++ b/.github/scripts/issue-quality-core.cjs @@ -66,45 +66,78 @@ function isPlaceholderOnlyValue(raw) { */ function stripMediaTokens(text) { if (typeof text !== "string") return ""; - // Indented code lines render as literal code in GitHub Markdown. Protect - // them first so neither the HTML nor the Markdown media stripper can - // remove example syntax; restore the lines afterwards. + // Fenced and indented code render literally in GitHub Markdown. Protect + // them first so neither media stripper can remove example syntax. The + // protector deliberately leaves indented children of an unindented HTML + // media block visible: those lines are HTML children, not Markdown code. const protectedText = protectIndentedCodeLines(text); const markdownStripped = stripMarkdownImages(stripHtmlMedia(protectedText.text)); const referenceStripped = stripReferenceImages(markdownStripped); - return restoreIndentedCodeLines(referenceStripped, protectedText.lines); + return restoreIndentedCodeLines(referenceStripped, protectedText); } /** - * Replace every indented code line (4+ leading spaces or a tab) with a - * placeholder of equal length so media stripping cannot touch it. Returns the - * masked text plus the original lines for restoration. + * Replace fenced code and indented code outside HTML media blocks with opaque + * tokens. Restoration is token-based rather than line-position-based because + * stripping a multiline media block may collapse or remove lines. */ function protectIndentedCodeLines(text) { const lines = []; + let markerPrefix = "\u0000OCX_ISSUE_CODE_"; + while (text.includes(markerPrefix)) markerPrefix += "_"; + let mediaDepth = 0; + let fence = null; + + const mask = (line) => { + const index = lines.push(line) - 1; + return `${markerPrefix}${index}\u0000`; + }; + const masked = text.split("\n").map((line) => { - if (/^(?: {4,}|\t)/.test(line)) { - lines.push(line); - return "\u0000" + line.replace(/[^\n]/g, " ").slice(1); + if (fence) { + const closing = new RegExp(`^ {0,3}${fence.char}{${fence.length},}[ \\t]*$`); + if (closing.test(line)) fence = null; + return mask(line); + } + + const fenceStart = line.match(/^ {0,3}(`{3,}|~{3,})/); + if (fenceStart) { + fence = { char: fenceStart[1][0], length: fenceStart[1].length }; + return mask(line); + } + + // Four-space/tab lines inside an active unindented HTML media block are + // child markup or fallback text. Treating them as code would keep an + // otherwise media-only /