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:
parent
4d5fa5c855
commit
89f317915d
|
@ -12,17 +12,13 @@ module.exports = function convertToJsonProcessor(log, createDocMessage) {
|
||||||
|
|
||||||
let title = doc.title;
|
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) {
|
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) {
|
if (title === undefined) {
|
||||||
const match = /<h1[^>]*>(.+?)<\/h1>/.exec(contents);
|
title = doc.name;
|
||||||
if (match) {
|
|
||||||
title = match[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is still no title then log a warning
|
// If there is still no title then log a warning
|
||||||
|
|
|
@ -50,8 +50,12 @@ describe('convertToJson processor', () => {
|
||||||
expect(log.warn).not.toHaveBeenCalled();
|
expect(log.warn).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get the title from the first `h1` if no title nor name is specified', () => {
|
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', renderedContent: '<div><h1 class="title">Some title</h1><article><h1>Article 1</h1></article></div>' }];
|
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);
|
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).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');
|
expect(JSON.parse(docs[0].renderedContent).title).toEqual('Some title');
|
||||||
|
|
Loading…
Reference in New Issue