Skip to content

Commit b72b980

Browse files
committed
[docs-infra] Fail on invalid codeblock tab titles
1 parent 94815ac commit b72b980

2 files changed

Lines changed: 150 additions & 7 deletions

File tree

‎packages-internal/markdown/parseMarkdown.mjs‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,58 @@ function getDescription(markdown) {
205205
return matches[1].trim().replace(/`/g, '');
206206
}
207207

208+
/**
209+
* Parse the tab title from a `<codeblock>` fence header.
210+
* Accepts a single unquoted word (`npm`) or a quoted multi-word title (`"Claude Code"`).
211+
* @param {string} language
212+
* @param {string} meta - Remainder of the fence line after the language.
213+
* @returns {string}
214+
*/
215+
function parseCodeblockTabTitle(language, meta) {
216+
const trimmed = meta.trim();
217+
const fencePreview = `\`\`\`${language}${meta}`;
218+
219+
if (!trimmed) {
220+
throw new Error(
221+
[
222+
'docs-infra: Missing codeblock tab title.',
223+
`The fence ${fencePreview} is missing a tab title.`,
224+
'Add a single-word title (e.g. ```bash npm) or a quoted multi-word title (e.g. ```bash "Claude Code").',
225+
'',
226+
].join('\n'),
227+
);
228+
}
229+
230+
const quotedMatch = trimmed.match(/^(["'])([\s\S]*?)\1$/);
231+
if (quotedMatch) {
232+
const title = quotedMatch[2];
233+
if (title.length === 0) {
234+
throw new Error(
235+
[
236+
'docs-infra: Empty codeblock tab title.',
237+
`The fence ${fencePreview} has an empty quoted tab title.`,
238+
'Provide a non-empty title (e.g. ```bash "Claude Code").',
239+
'',
240+
].join('\n'),
241+
);
242+
}
243+
return title;
244+
}
245+
246+
if (/^\S+$/.test(trimmed)) {
247+
return trimmed;
248+
}
249+
250+
throw new Error(
251+
[
252+
'docs-infra: Invalid codeblock tab title.',
253+
`The fence ${fencePreview} has a tab title that could not be parsed.`,
254+
'Use a single word (e.g. ```bash Claude) or quote multi-word titles (e.g. ```bash "Claude Code").',
255+
'',
256+
].join('\n'),
257+
);
258+
}
259+
208260
function getCodeblock(content) {
209261
if (!content.startsWith('<codeblock')) {
210262
return undefined;
@@ -214,13 +266,13 @@ function getCodeblock(content) {
214266
const storageKey = content.match(
215267
/^<codeblock (?!<codeblock )[^>]*storageKey=["|'](?!storageKey=["|'])(\S*)["|'].*>/m,
216268
)?.[1];
217-
const blocks = [...content.matchAll(/^```(\S*) (\S*)\n(.*?)\n```/gmsu)].map(
218-
([, language, tab, code]) => ({ language, tab, code }),
219-
);
220-
221-
const blocksData = blocks.filter(
222-
(block) => block.tab !== undefined && !emptyRegExp.test(block.code),
223-
);
269+
const blocksData = [...content.matchAll(/^```(\S*)([^\n]*)\n(.*?)\n```/gmsu)]
270+
.map(([, language, meta, code]) => ({
271+
language,
272+
tab: parseCodeblockTabTitle(language, meta),
273+
code,
274+
}))
275+
.filter((block) => !emptyRegExp.test(block.code));
224276

225277
return {
226278
type: 'codeblock',

‎packages-internal/markdown/parseMarkdown.test.mjs‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,97 @@ title: "Our docs just got a major upgrade—here's what that means for you"
243243
],
244244
});
245245
});
246+
247+
it('should accept quoted multi-word tab titles', () => {
248+
const codeblock = getCodeblock(
249+
[
250+
'<codeblock storageKey="coding-agent">',
251+
'',
252+
'```bash "Claude Code"',
253+
'claude plugin marketplace add mui/material-ui',
254+
'```',
255+
'',
256+
"```bash 'OpenAI Codex'",
257+
'codex plugin add material-ui@material-ui',
258+
'```',
259+
'',
260+
'</codeblock>',
261+
].join('\n'),
262+
);
263+
expect(codeblock).to.deep.equal({
264+
type: 'codeblock',
265+
storageKey: 'coding-agent',
266+
data: [
267+
{
268+
language: 'bash',
269+
tab: 'Claude Code',
270+
code: 'claude plugin marketplace add mui/material-ui',
271+
},
272+
{
273+
language: 'bash',
274+
tab: 'OpenAI Codex',
275+
code: 'codex plugin add material-ui@material-ui',
276+
},
277+
],
278+
});
279+
});
280+
281+
it('should throw on unquoted multi-word tab titles', () => {
282+
expect(() => {
283+
getCodeblock(
284+
[
285+
'<codeblock storageKey="coding-agent">',
286+
'',
287+
'```bash Claude Code',
288+
'claude plugin marketplace add mui/material-ui',
289+
'```',
290+
'',
291+
'</codeblock>',
292+
].join('\n'),
293+
);
294+
}).to.throw(`docs-infra: Invalid codeblock tab title.
295+
The fence \`\`\`bash Claude Code has a tab title that could not be parsed.
296+
Use a single word (e.g. \`\`\`bash Claude) or quote multi-word titles (e.g. \`\`\`bash "Claude Code").
297+
`);
298+
});
299+
300+
it('should throw when the tab title is missing', () => {
301+
expect(() => {
302+
getCodeblock(
303+
[
304+
'<codeblock storageKey="package-manager">',
305+
'',
306+
'```bash',
307+
'npm install @mui/material',
308+
'```',
309+
'',
310+
'</codeblock>',
311+
].join('\n'),
312+
);
313+
}).to.throw(`docs-infra: Missing codeblock tab title.
314+
The fence \`\`\`bash is missing a tab title.
315+
Add a single-word title (e.g. \`\`\`bash npm) or a quoted multi-word title (e.g. \`\`\`bash "Claude Code").
316+
`);
317+
});
318+
319+
it('should throw on empty quoted tab titles', () => {
320+
expect(() => {
321+
getCodeblock(
322+
[
323+
'<codeblock storageKey="package-manager">',
324+
'',
325+
'```bash ""',
326+
'npm install @mui/material',
327+
'```',
328+
'',
329+
'</codeblock>',
330+
].join('\n'),
331+
);
332+
}).to.throw(`docs-infra: Empty codeblock tab title.
333+
The fence \`\`\`bash "" has an empty quoted tab title.
334+
Provide a non-empty title (e.g. \`\`\`bash "Claude Code").
335+
`);
336+
});
246337
});
247338

248339
describe('renderMarkdown', () => {

0 commit comments

Comments
 (0)