build(aio): fail doc-gen if a code-example is badly formatted.

This will catch the problem that was missed in
https://github.com/angular/angular/pull/19845#issuecomment-338626662
This commit is contained in:
Peter Bacon Darwin
2017-10-23 17:58:49 +01:00
committed by Victor Berchet
parent 5b16ce9302
commit 0355142737
2 changed files with 48 additions and 12 deletions

View File

@ -12,16 +12,43 @@ module.exports = function renderExamples(getExampleRegion) {
docs.forEach(doc => {
if (doc.renderedContent) {
// We match either `code-example` or `code-pane` elements that have a path attribute
doc.renderedContent = doc.renderedContent.replace(/<(code-example|code-pane)([^>]*)>[^<]*<\/\1>/g, (original, element, attributes) => {
const attrMap = parseAttributes(attributes);
if (attrMap.path) {
// We found a path attribute so look up the example and rebuild the HTML
const exampleContent = getExampleRegion(doc, attrMap.path, attrMap.region);
return `<${element}${renderAttributes(attrMap)}>\n${exampleContent}\n</${element}>`;
}
// No path attribute so just ignore this one
return original;
});
doc.renderedContent = doc.renderedContent.replace(
/<(code-example|code-pane)([^>]*)>[^<]*<\/([^>]+)>/g,
(original, openingTag, attributes, closingTag) => {
const attrMap = parseAttributes(attributes);
if (attrMap.path) {
if (closingTag !== openingTag) {
// The markdown renderer will wrap what it thinks is a paragraph in `<p>` and `</p>` tags.
// If you do not leave a blank line between a paragraph of text and a `<code-example>` then
// the markdown renderer may add a paragraph marker "in-between" the opening and closing
// tags of the code-example. For example:
//
// ```
// Some paragraph
// <code-example path="...">
//
// </code-example>
// ```
//
// will be rendered as:
//
// ```
// <p>Some paragraph
// <code-example path="...">
// </p>
// </code-example>
// ```
throw new Error(
'Badly formed example: ' + original + ' - closing tag does not match opening tag.\n' +
' - Perhaps you forgot to put a blank line before the example?');
}
// We found a path attribute so look up the example and rebuild the HTML
const exampleContent = getExampleRegion(doc, attrMap.path, attrMap.region);
return `<${openingTag}${renderAttributes(attrMap)}>\n${exampleContent}\n</${openingTag}>`;
}
// No path attribute so just ignore this one
return original;
});
}
});
}