Skip to content

Commit ae88649

Browse files
committed
fix(issue-quality): strip indented HTML media blocks
1 parent 2468502 commit ae88649

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎.github/scripts/issue-quality.cjs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,29 @@ function stripMediaTokens(text) {
7676
}
7777

7878
/**
79-
* Replace every indented code line (4+ leading spaces or a tab) with a
80-
* placeholder of equal length so media stripping cannot touch it. Returns the
81-
* masked text plus the original lines for restoration.
79+
* Replace indented code lines (4+ leading spaces or a tab) with placeholders
80+
* so media stripping cannot touch them. Indented children of an unindented
81+
* HTML media block remain visible because their indentation is just HTML
82+
* formatting. Returns the masked text plus the original lines for restoration.
8283
*/
8384
function protectIndentedCodeLines(text) {
8485
const lines = [];
86+
let mediaBlockDepth = 0;
8587
const masked = text.split("\n").map((line) => {
86-
if (/^(?: {4,}|\t)/.test(line)) {
88+
const isIndented = /^(?: {4,}|\t)/.test(line);
89+
if (isIndented && mediaBlockDepth === 0) {
8790
lines.push(line);
8891
return "\u0000" + line.replace(/[^\n]/g, " ").slice(1);
8992
}
93+
94+
// Indentation inside a real HTML media block is formatting, not a
95+
// Markdown indented-code block. Leave child tags visible to the HTML
96+
// stripper. An indented opening tag at depth zero remains protected as
97+
// literal Markdown code and therefore must not start a block here.
98+
for (const tag of line.matchAll(/<(\/)?(picture|video|audio)\b[^>]*>/gi)) {
99+
if (tag[1]) mediaBlockDepth = Math.max(0, mediaBlockDepth - 1);
100+
else if (!/\/\s*>$/.test(tag[0])) mediaBlockDepth += 1;
101+
}
90102
lines.push(null);
91103
return line;
92104
});

‎.github/scripts/issue-quality.test.cjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ describe("validateIssue - feature", () => {
396396
false,
397397
);
398398
assert.equal(isMediaOnly('<picture><source srcset="x.webp"><img src="x.png"></picture>'), true);
399+
assert.equal(
400+
isMediaOnly('<picture>\n <source srcset="x.webp">\n <img src="x.png">\n</picture>'),
401+
true,
402+
);
403+
assert.equal(isMediaOnly('<video controls>\n <source src="clip.mp4">\n</video>'), true);
399404
assert.equal(isMediaOnly('<video src="clip.mp4"></video>'), true);
400405
assert.equal(isMediaOnly('<img src="x.png" />\nCaption text'), false);
401406
assert.equal(isMediaOnly("Some real description."), false);

0 commit comments

Comments
 (0)