Skip to content

Commit 9d2f82b

Browse files
committed
build: prevent markdown styling from bleeding into examples
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 d52ede5 commit 9d2f82b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 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.
@@ -85,11 +91,11 @@ export class DocsMarkdownRenderer extends Renderer {
8591
file: string;
8692
region: string;
8793
};
88-
return `<div material-docs-example="${example}"
94+
return `${exampleStartMarker}<div material-docs-example="${example}"
8995
${file ? `file="${file}"` : ''}
90-
${region ? `region="${region}"` : ''}></div>`;
96+
${region ? `region="${region}"` : ''}></div>${exampleEndMarker}`;
9197
} else {
92-
return `<div material-docs-example="${content}"></div>`;
98+
return `${exampleStartMarker}<div material-docs-example="${content}"></div>${exampleEndMarker}`;
9399
}
94100
});
95101

@@ -120,6 +126,16 @@ export class DocsMarkdownRenderer extends Renderer {
120126
this._slugger.seen = {};
121127
this._referencedFragments.clear();
122128

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

0 commit comments

Comments
 (0)