Skip to content

Commit bfaa272

Browse files
authored
docs(markdown-to-html): use regex to match new API (#19417)
* fix(markdown-to-html): use regex to match new API * fix test to reflect change Co-authored-by: Annie Wang <annieyw@google.com>
1 parent a72883f commit bfaa272

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

tools/markdown-to-html/docs-marked-renderer.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ describe('DocsMarkdownRenderer', () => {
4949
{
5050
"example": "exampleName",
5151
"file": "example-html.html",
52-
"region": "some-region",
52+
"region": "some-region"
5353
}
5454
) -->`);
55-
56-
// TODO(annieyw): I think DocsMarkedRenderer#html needs to be fixed for the new API?
5755
expectEqualIgnoreLeadingWhitespace(result, `<div material-docs-example="
5856
{
5957
"example": "exampleName",

tools/markdown-to-html/docs-marked-renderer.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class DocsMarkdownRenderer extends Renderer {
6464
* {
6565
* "example": "exampleName",
6666
* "file": "example-html.html",
67-
* "region": "some-region",
67+
* "region": "some-region"
6868
* }
6969
* ) -->`
7070
* turns into
@@ -78,20 +78,21 @@ export class DocsMarkdownRenderer extends Renderer {
7878
* `<div material-docs-example="name"></div>`
7979
*/
8080
html(html: string) {
81-
html = html.replace(exampleCommentRegex, (_match: string, content: string) => {
82-
if (content.startsWith('{')) {
83-
const {example, file, region} = JSON.parse(content);
84-
return `<div material-docs-example="${example}"
85-
file="${file}"
86-
region="${region}"></div>`;
87-
} else {
88-
return `<div material-docs-example="${content}"></div>`;
89-
}
90-
}
91-
);
92-
93-
return super.html(html);
94-
}
81+
html = html.replace(exampleCommentRegex, (_match: string, content: string) => {
82+
// using [\s\S]* because .* does not match line breaks
83+
if (content.match(/\{[\s\S]*\}/g)) {
84+
const {example, file, region} = JSON.parse(content);
85+
return `<div material-docs-example="${example}"
86+
file="${file}"
87+
region="${region}"></div>`;
88+
} else {
89+
return `<div material-docs-example="${content}"></div>`;
90+
}
91+
}
92+
);
93+
94+
return super.html(html);
95+
}
9596

9697
/**
9798
* Method that will be called after a markdown file has been transformed to HTML. This method

0 commit comments

Comments
 (0)