2017-04-26 12:58:57 -04:00
|
|
|
const { parseAttributes, renderAttributes } = require('../../helpers/utils');
|
2017-03-25 17:07:34 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the renderedContent looking for code examples that have a path (and optionally a region) attribute.
|
|
|
|
* When they are found replace their content with the appropriate doc-region parsed previously from an example file.
|
|
|
|
*/
|
|
|
|
module.exports = function renderExamples(getExampleRegion) {
|
|
|
|
return {
|
|
|
|
$runAfter: ['docs-rendered'],
|
|
|
|
$runBefore: ['writing-files'],
|
|
|
|
$process: function(docs) {
|
|
|
|
docs.forEach(doc => {
|
|
|
|
if (doc.renderedContent) {
|
|
|
|
// We match either `code-example` or `code-pane` elements that have a path attribute
|
2017-03-27 11:37:44 -04:00
|
|
|
doc.renderedContent = doc.renderedContent.replace(/<(code-example|code-pane)([^>]*)>[^<]*<\/\1>/g, (original, element, attributes) => {
|
2017-03-25 17:07:34 -04:00
|
|
|
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);
|
2017-04-26 12:58:57 -04:00
|
|
|
return `<${element}${renderAttributes(attrMap)}>\n${exampleContent}\n</${element}>`;
|
2017-03-25 17:07:34 -04:00
|
|
|
}
|
|
|
|
// No path attribute so just ignore this one
|
|
|
|
return original;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|