mirror of
https://github.com/angular/angular.git
synced 2026-11-22 22:55:25 -05:00
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"}`.
This commit is contained in:
@@ -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}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user