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;
|
|
|
|
|
2017-05-30 15:24:44 -04:00
|
|
|
// We do allow an empty `title` but if it is `undefined` we resort to `vFile.title` and then `name`
|
2017-04-19 04:56:39 -04:00
|
|
|
if (title === undefined) {
|
2017-05-30 15:24:44 -04:00
|
|
|
title = (doc.vFile && doc.vFile.title);
|
2017-04-19 04:56:39 -04:00
|
|
|
}
|
|
|
|
|
2017-04-16 17:13:09 -04:00
|
|
|
if (title === undefined) {
|
2017-05-30 15:24:44 -04:00
|
|
|
title = doc.name;
|
2017-04-16 17:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|