Peter Bacon Darwin ecd0348d96 build(aio): extract the title from the content if necessary
Documents can specify their title via the `title` or `name` jsdoc tags.

This change adds that, if neither are provided, the first `<h1>` element
is removed from the `renderedContent` and used for the title.

If there is still no title then it is set to the empty string and a warning
is logged.
2017-04-18 15:20:04 -07:00

35 lines
1.0 KiB
JavaScript

module.exports = function convertToJsonProcessor(log, createDocMessage) {
return {
$runAfter: ['checkUnbalancedBackTicks'],
$runBefore: ['writeFilesProcessor'],
docTypes: [],
$process: function(docs) {
const docTypes = this.docTypes;
docs.forEach((doc) => {
if (docTypes.indexOf(doc.docType) !== -1) {
let title = doc.title || doc.name;
let contents = doc.renderedContent || '';
// If there is no title then try to extract it from the first h1 in the renderedContent
if (title === undefined) {
const match = /<h1[^>]*>(.+?)<\/h1>/.exec(contents);
if (match) {
title = match[1];
}
}
// If there is still no title then log a warning
if (title === undefined) {
title = '';
log.warn(createDocMessage('Title property expected', doc));
}
doc.renderedContent = JSON.stringify({ title, contents }, null, 2);
}
});
}
};
};