build(aio): use the captured h1 as the title if necessary

If the doc does not already have a `title` tag
then use the one captured from the renderedContent in
the final JSON output, instead.
This commit is contained in:
Peter Bacon Darwin 2017-05-30 22:24:44 +03:00 committed by Pete Bacon Darwin
parent 4d5fa5c855
commit 89f317915d
2 changed files with 9 additions and 9 deletions

View File

@ -12,17 +12,13 @@ module.exports = function convertToJsonProcessor(log, createDocMessage) {
let title = doc.title;
// We do allow an empty `title` but resort to `name` if it is not even defined
// We do allow an empty `title` but if it is `undefined` we resort to `vFile.title` and then `name`
if (title === undefined) {
title = doc.name;
title = (doc.vFile && doc.vFile.title);
}
// 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];
}
title = doc.name;
}
// If there is still no title then log a warning

View File

@ -50,8 +50,12 @@ describe('convertToJson processor', () => {
expect(log.warn).not.toHaveBeenCalled();
});
it('should get the title from the first `h1` if no title nor name is specified', () => {
const docs = [{ docType: 'test-doc', renderedContent: '<div><h1 class="title">Some title</h1><article><h1>Article 1</h1></article></div>' }];
it('should get the title from the title extracted from the h1 in the rendered content if no title property is specified', () => {
const docs = [{
docType: 'test-doc',
vFile: { title: 'Some title' },
renderedContent: '<div><h1 class="title">Some title</h1><article><h1>Article 1</h1></article></div>'
}];
processor.$process(docs);
expect(JSON.parse(docs[0].renderedContent).contents).toEqual('<div><h1 class="title">Some title</h1><article><h1>Article 1</h1></article></div>');
expect(JSON.parse(docs[0].renderedContent).title).toEqual('Some title');