Skip to content

Commit a360bdc

Browse files
committed
update example comment api in md to html renderer
1 parent 658896f commit a360bdc

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {basename, extname} from 'path';
55
const whitespaceRegex = /\W+/g;
66

77
/** Regular expression that matches example comments. */
8-
const exampleCommentRegex = /<!--\W*example\(([^)]+)\)\W*-->/g;
8+
const exampleCommentRegex = /<!--\s*example\(\{(.*\:.*)+\}\)\s*-->/g;
99

1010
/**
1111
* Custom renderer for marked that will be used to transform markdown files to HTML
@@ -49,15 +49,31 @@ export class DocsMarkdownRenderer extends Renderer {
4949
* Method that will be called whenever inline HTML is processed by marked. In that case,
5050
* we can easily transform the example comments into real HTML elements. For example:
5151
*
52-
* `<!-- example(name) -->` turns into `<div material-docs-example="name"></div>`
52+
* `<!-- example(
53+
* {
54+
* "example": "exampleName",
55+
* "file": "example-html.html",
56+
* "lines": "[5, 10]",
57+
* "expanded": "true"
58+
* }
59+
* ) -->`
60+
* turns into
61+
* `<div material-docs-example="exampleName"
62+
* file="example-html.html"
63+
* lines="[5, 10]"
64+
* expanded="true"></div>`
5365
*/
5466
html(html: string) {
55-
html = html.replace(exampleCommentRegex, (_match: string, name: string) =>
56-
`<div material-docs-example="${name}"></div>`
57-
);
67+
html = html.replace(exampleCommentRegex, (_match: string, content: string) =>
68+
{
69+
const {example, file, lines, expanded} = JSON.parse(content);
70+
return `<div material-docs-example="${example}" file="${file}" lines="${lines}" exapnded="${!!expanded}"></div>`;
71+
}
5872

59-
return super.html(html);
60-
}
73+
);
74+
75+
return super.html(html);
76+
}
6177

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

0 commit comments

Comments
 (0)