From 89f317915de66d715f612786a32c7068929983cb Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Tue, 30 May 2017 22:24:44 +0300 Subject: [PATCH] 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. --- .../angular-base-package/processors/convertToJson.js | 10 +++------- .../processors/convertToJson.spec.js | 8 ++++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/aio/tools/transforms/angular-base-package/processors/convertToJson.js b/aio/tools/transforms/angular-base-package/processors/convertToJson.js index 1506306065..932f6e69d2 100644 --- a/aio/tools/transforms/angular-base-package/processors/convertToJson.js +++ b/aio/tools/transforms/angular-base-package/processors/convertToJson.js @@ -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>/.exec(contents); - if (match) { - title = match[1]; - } + title = doc.name; } // If there is still no title then log a warning diff --git a/aio/tools/transforms/angular-base-package/processors/convertToJson.spec.js b/aio/tools/transforms/angular-base-package/processors/convertToJson.spec.js index 043267ae10..355097cda0 100644 --- a/aio/tools/transforms/angular-base-package/processors/convertToJson.spec.js +++ b/aio/tools/transforms/angular-base-package/processors/convertToJson.spec.js @@ -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: '

Some title

Article 1

' }]; + 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: '

Some title

Article 1

' + }]; processor.$process(docs); expect(JSON.parse(docs[0].renderedContent).contents).toEqual('

Some title

Article 1

'); expect(JSON.parse(docs[0].renderedContent).title).toEqual('Some title');