From 7bbe2636dc9e987ea3ffe599c493deeaf2da0c3a Mon Sep 17 00:00:00 2001 From: Kam Date: Tue, 15 Sep 2026 23:36:56 +0300 Subject: [PATCH] fix(docs-infra): reject malformed code block metadata The code block tokenizer silently ignored metadata it didn't recognize, and `expandRangeStringValues` swallowed highlight parse errors, so a typo like `header="foo.ts"` or a missing `highlight:` key rendered the block without its header or highlighting and nothing flagged it. The tokenizer now throws when the metadata contains anything it doesn't parse or when `highlight` isn't a valid range. The highlight rule also stops at its closing bracket, so `highlight` no longer swallows keys that follow it, like `{highlight: [9], header: "hero.service.ts"}`. --- .../extensions/docs-code/docs-code-block.mts | 24 ++++++++++++++++++- .../docs-code-block/docs-code-block.spec.mts | 22 +++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/docs-code-block.mts b/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/docs-code-block.mts index 46260f19ec3..e1a334ebea5 100644 --- a/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/docs-code-block.mts +++ b/adev/shared-docs/pipeline/shared/marked/extensions/docs-code/docs-code-block.mts @@ -8,6 +8,7 @@ import {TokenizerThis, RendererThis} from 'marked'; import {CodeToken, formatCode} from './format/index.mjs'; +import {expandRangeStringValues} from './format/range.mjs'; import {AdevDocsRenderer} from '../../renderer.mjs'; export interface DocsCodeBlock extends CodeToken { @@ -39,12 +40,21 @@ export const docsCodeBlockExtension = { const metadataStr = match[2].trim(); const headerRule = /header\s*:\s*(['"`])(.*?)\1/; // The 2nd capture matters here - const highlightRule = /highlight\s*:\s*(.*)([^,])/; + const highlightRule = /highlight\s*:\s*(\[(?:[^[\]]|\[[^[\]]*\])*\])/; const hideCopyRule = /hideCopy/; const hideDollarRule = /hideDollar/; const preferRule = /\b(prefer|avoid)\b/; const linenumsRule = /linenums/; + validateMetadata(metadataStr, highlightRule.exec(metadataStr)?.[1], [ + headerRule, + highlightRule, + hideCopyRule, + hideDollarRule, + preferRule, + linenumsRule, + ]); + const token: DocsCodeBlock = { raw: match[0], type: 'docs-code-block', @@ -68,3 +78,15 @@ export const docsCodeBlockExtension = { return formatCode(token, (this.parser.renderer as AdevDocsRenderer).context); }, }; + +function validateMetadata(metadataStr: string, highlight: string | undefined, rules: RegExp[]) { + const unrecognized = rules + .reduce((rest, rule) => rest.replace(new RegExp(rule, 'g'), ''), metadataStr) + .replace(/[{},\s]/g, ''); + const invalidHighlight = + highlight !== undefined && expandRangeStringValues(highlight).length === 0; + + if (unrecognized || invalidHighlight) { + throw new Error(`Invalid code block metadata: ${metadataStr}`); + } +} diff --git a/adev/shared-docs/pipeline/shared/marked/test/docs-code-block/docs-code-block.spec.mts b/adev/shared-docs/pipeline/shared/marked/test/docs-code-block/docs-code-block.spec.mts index 7419bc000d7..2f61604d947 100644 --- a/adev/shared-docs/pipeline/shared/marked/test/docs-code-block/docs-code-block.spec.mts +++ b/adev/shared-docs/pipeline/shared/marked/test/docs-code-block/docs-code-block.spec.mts @@ -48,4 +48,26 @@ describe('markdown to html', () => { const codeBlock = markdownDocument.querySelectorAll('.docs-code')[3]; expect(codeBlock.getAttribute('hideDollar')).toBe('true'); }); + + it('should parse highlight regardless of its position in the metadata', () => { + const fragment = JSDOM.fragment( + parseMarkdown('```ts {highlight: [2], header: "foo.ts"}\na\nb\n```', rendererContext), + ); + expect(fragment.querySelector('.docs-code')?.getAttribute('header')).toBe('foo.ts'); + expect(fragment.querySelectorAll('.line.highlighted').length).toBe(1); + }); + + for (const metadata of [ + '{header="foo.ts"}', + '{header: foo.ts}', + '{[[1],[2]]}', + '{highlight="[2]"}', + '{highlight: [1,]}', + ]) { + it(`should throw on invalid metadata ${metadata}`, () => { + expect(() => + parseMarkdown('```ts ' + metadata + '\ncode\n```', rendererContext), + ).toThrowError(/Invalid code block metadata/); + }); + } });