2017-04-16 17:13:09 -04:00
|
|
|
module.exports = function convertToJsonProcessor(log, createDocMessage) {
|
2017-02-21 11:57:19 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
$runAfter: ['checkUnbalancedBackTicks'],
|
|
|
|
$runBefore: ['writeFilesProcessor'],
|
|
|
|
docTypes: [],
|
|
|
|
$process: function(docs) {
|
2017-04-01 14:34:10 -04:00
|
|
|
const docTypes = this.docTypes;
|
2017-02-21 11:57:19 -05:00
|
|
|
docs.forEach((doc) => {
|
|
|
|
if (docTypes.indexOf(doc.docType) !== -1) {
|
2017-04-16 17:13:09 -04:00
|
|
|
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);
|
2017-02-21 11:57:19 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2017-04-16 17:13:09 -04:00
|
|
|
|
2017-04-01 14:34:10 -04:00
|
|
|
};
|