Skip to content

Commit c771a01

Browse files
authored
build: prevent markdown styling from bleeding into examples (#31358)
Currently we style the content in the docs by setting a `docs-markdown` class around it which then targets element tags that can be produced by the markdown renderer like `p` and `h1`. The problem with this is that some components use these elements for their own styling, e.g. the main `mat-card` example looks off because its `p` tag styling is overridden by `docs-markdown`. These changes update the docs generation script to exclude the component styles from the markdown styles.
1 parent f521a58 commit c771a01

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import {basename, extname} from 'path';
44
/** Regular expression that matches example comments. */
55
const exampleCommentRegex = /<!--\s*example\(\s*([^)]+)\)\s*-->/g;
66

7+
/** Marker inserted before the start of an example. */
8+
const exampleStartMarker = '<!-- ___exampleStart___ -->';
9+
10+
/** Marker inserted after the end of an example. */
11+
const exampleEndMarker = '<!-- ___exampleEnd___ -->';
12+
713
/**
814
* Custom renderer for marked that will be used to transform markdown files to HTML
915
* files that can be used in the Angular Material docs.
@@ -78,19 +84,23 @@ export class DocsMarkdownRenderer extends Renderer {
7884
*/
7985
html(html: string) {
8086
html = html.replace(exampleCommentRegex, (_match: string, content: string) => {
87+
let replacement: string;
88+
8189
// using [\s\S]* because .* does not match line breaks
8290
if (content.match(/\{[\s\S]*\}/g)) {
8391
const {example, file, region} = JSON.parse(content.trim()) as {
8492
example: string;
8593
file: string;
8694
region: string;
8795
};
88-
return `<div material-docs-example="${example}"
96+
replacement = `<div material-docs-example="${example}"
8997
${file ? `file="${file}"` : ''}
9098
${region ? `region="${region}"` : ''}></div>`;
9199
} else {
92-
return `<div material-docs-example="${content}"></div>`;
100+
replacement = `<div material-docs-example="${content}"></div>`;
93101
}
102+
103+
return `${exampleStartMarker}${replacement}${exampleEndMarker}`;
94104
});
95105

96106
return super.html(html);
@@ -120,6 +130,16 @@ export class DocsMarkdownRenderer extends Renderer {
120130
this._slugger.seen = {};
121131
this._referencedFragments.clear();
122132

123-
return `<div class="docs-markdown">${output}</div>`;
133+
const markdownOpen = '<div class="docs-markdown">';
134+
135+
// The markdown styling can interfere with the example's styles, because we don't use
136+
// encapsulation. We work around it by replacing the opening marker, left by the previous
137+
// step, with a closing tag, and replacing the closing marker with an opening tag.
138+
// The result is that we exclude the example code from the markdown styling.
139+
output = output
140+
.replace(new RegExp(exampleStartMarker, 'g'), '</div>')
141+
.replace(new RegExp(exampleEndMarker, 'g'), markdownOpen);
142+
143+
return `${markdownOpen}${output}</div>`;
124144
}
125145
}

0 commit comments

Comments
 (0)