From 51098c4f8698b87305de258472fcd0a9cb3d34b0 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 19 Apr 2017 09:56:39 +0100 Subject: [PATCH] fix(aio): allow empty titles for contents pages Now if you specifically provide an empty `@title` tag for a contents file, the doc-gen will not complain and the browser will just display a reasonable default. --- aio/content/file-not-found.md | 2 +- aio/src/app/app.component.spec.ts | 14 ++++++++++++++ aio/src/app/app.component.ts | 6 +++++- .../processors/convertToJson.js | 8 +++++++- .../processors/convertToJson.spec.js | 16 +++++++++++++++- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/aio/content/file-not-found.md b/aio/content/file-not-found.md index 719a135b6c..7cd8a516d5 100644 --- a/aio/content/file-not-found.md +++ b/aio/content/file-not-found.md @@ -1,3 +1,3 @@ -@title Document not found +@title @description

Document not found

\ No newline at end of file diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index e0b4d11d2a..a95fa12916 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -272,6 +272,14 @@ describe('AppComponent', () => { fixture.detectChanges(); expect(titleService.setTitle).toHaveBeenCalledWith('Angular - Pipes'); }); + + it('should update the document title, with a default value if the document has no title', () => { + const titleService = TestBed.get(Title); + spyOn(titleService, 'setTitle'); + locationService.go('file-not-found'); + fixture.detectChanges(); + expect(titleService.setTitle).toHaveBeenCalledWith('Angular'); + }); }); describe('autoScrolling', () => { @@ -424,12 +432,18 @@ class TestHttp { "contents": "

Test Doc

" }; + fileNotFoundDoc = { + "title": "", + "contents": "Page not found" + }; + // get = jasmine.createSpy('get').and.callFake((url: string) => { ... }); get(url: string) { const json = /navigation.json/.test(url) ? this.navJson : /api/.test(url) ? this.apiDoc : /pipes/.test(url) ? this.pipesDoc : + /file-not-found/.test(url) ? this.fileNotFoundDoc : this.testDoc; return of({ json: () => json }); } diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index 4ae7718f21..7ccb07de7d 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -150,6 +150,10 @@ export class AppComponent implements OnInit { } setDocumentTitle(title) { - this.titleService.setTitle(`Angular - ${title}`); + if (title.trim()) { + this.titleService.setTitle(`Angular - ${title}`); + } else { + this.titleService.setTitle('Angular'); + } } } diff --git a/aio/tools/transforms/angular.io-package/processors/convertToJson.js b/aio/tools/transforms/angular.io-package/processors/convertToJson.js index 374e1eb176..230f524a3c 100644 --- a/aio/tools/transforms/angular.io-package/processors/convertToJson.js +++ b/aio/tools/transforms/angular.io-package/processors/convertToJson.js @@ -8,9 +8,15 @@ module.exports = function convertToJsonProcessor(log, createDocMessage) { const docTypes = this.docTypes; docs.forEach((doc) => { if (docTypes.indexOf(doc.docType) !== -1) { - let title = doc.title || doc.name; let contents = doc.renderedContent || ''; + 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; + } + // 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); diff --git a/aio/tools/transforms/angular.io-package/processors/convertToJson.spec.js b/aio/tools/transforms/angular.io-package/processors/convertToJson.spec.js index 534f3506ff..17a073b004 100644 --- a/aio/tools/transforms/angular.io-package/processors/convertToJson.spec.js +++ b/aio/tools/transforms/angular.io-package/processors/convertToJson.spec.js @@ -4,7 +4,7 @@ var Dgeni = require('dgeni'); describe('convertToJson processor', () => { var dgeni, injector, processor, log; - beforeEach(function() { + beforeAll(function() { dgeni = new Dgeni([testPackage('angular.io-package')]); injector = dgeni.configureInjector(); processor = injector.get('convertToJsonProcessor'); @@ -34,6 +34,20 @@ describe('convertToJson processor', () => { expect(JSON.parse(docs[0].renderedContent).title).toEqual('The Name'); }); + it('should accept an empty title', () => { + const docs = [{ docType: 'test-doc', title: '' }]; + processor.$process(docs); + expect(JSON.parse(docs[0].renderedContent).title).toEqual(''); + expect(log.warn).not.toHaveBeenCalled(); + }); + + it('should accept an empty name if title is not provided', () => { + const docs = [{ docType: 'test-doc', name: '' }]; + processor.$process(docs); + expect(JSON.parse(docs[0].renderedContent).title).toEqual(''); + 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

' }]; processor.$process(docs);