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 contents = doc.renderedContent || '';
|
|
|
|
|
2017-04-19 04:56:39 -04:00
|
|
|
let title = doc.title;
|
|
|
|
|
|
|
|
// We do allow an empty `title` but resort to `name` if it is not even defined
|
|
|
|
if (title === undefined) {
|
|
|
|
title = doc.name;
|
|
|
|
}
|
|
|
|
|
2017-04-16 17:13:09 -04:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2017-04-20 07:55:34 -04:00
|
|
|
doc.renderedContent = JSON.stringify({ id: doc.path, 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
|
|
|
};
|