Skip to content

Commit dfa3f6e

Browse files
committed
support for old and new api before docs are updated
1 parent bf95db8 commit dfa3f6e

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

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

Lines changed: 24 additions & 13 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 = /<!--\s*example\(\{(.*\:.*)+\}\)\s*-->/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
@@ -47,29 +47,40 @@ export class DocsMarkdownRenderer extends Renderer {
4747

4848
/**
4949
* Method that will be called whenever inline HTML is processed by marked. In that case,
50-
* we can easily transform the example comments into real HTML elements. For example:
51-
*
50+
* we can easily transform the example comments into real HTML elements.
51+
* For example:
52+
* (New API)
5253
* `<!-- example(
5354
* {
5455
* "example": "exampleName",
5556
* "file": "example-html.html",
56-
* "lines": "[5, 10]",
57-
* "expanded": "true"
57+
* "lines": [5, 10],
58+
* "expanded": true
5859
* }
5960
* ) -->`
6061
* turns into
6162
* `<div material-docs-example="exampleName"
6263
* file="example-html.html"
63-
* lines="[5, 10]"
64-
* expanded="true"></div>`
64+
* [lines]="[5, 10]"
65+
* [expanded]="true"></div>`
66+
*
67+
* (old API)
68+
* `<!-- example(name) -->`
69+
* turns into
70+
* `<div material-docs-example="name"></div>`
6571
*/
6672
html(html: string) {
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}" expanded="${!!expanded}"></div>`;
71-
}
72-
73+
html = html.replace(exampleCommentRegex, (_match: string, content: string) => {
74+
if (content.startsWith('{')) {
75+
const {example, file, lines, expanded} = JSON.parse(content);
76+
return `<div material-docs-example="${example}"
77+
file="${file}"
78+
[lines]="${lines}"
79+
[expanded]="${!!expanded}"></div>`;
80+
} else {
81+
return `<div material-docs-example="${content}"></div>`;
82+
}
83+
}
7384
);
7485

7586
return super.html(html);

0 commit comments

Comments
 (0)