From a64af40c0bafdf05e12c2d9f6e90068c4a4820d9 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sun, 21 Jan 2018 17:24:05 +0000 Subject: [PATCH] build(aio): generate sitemap from the generated pages (#21689) Closes #21684 PR Close #21689 --- aio/src/extra-files/stable/robots.txt | 1 + .../transforms/angular-base-package/index.js | 1 + .../processors/createSitemap.js | 15 ++++++ .../processors/createSitemap.spec.js | 51 +++++++++++++++++++ .../transforms/templates/sitemap.template.xml | 7 +++ 5 files changed, 75 insertions(+) create mode 100644 aio/tools/transforms/angular-base-package/processors/createSitemap.js create mode 100644 aio/tools/transforms/angular-base-package/processors/createSitemap.spec.js create mode 100644 aio/tools/transforms/templates/sitemap.template.xml diff --git a/aio/src/extra-files/stable/robots.txt b/aio/src/extra-files/stable/robots.txt index 7dbb786bc6..7ff3ad772b 100644 --- a/aio/src/extra-files/stable/robots.txt +++ b/aio/src/extra-files/stable/robots.txt @@ -1,3 +1,4 @@ # Allow all URLs (see http://www.robotstxt.org/robotstxt.html) User-agent: * Disallow: +Sitemap: https://angular.io/generated/sitemap.xml diff --git a/aio/tools/transforms/angular-base-package/index.js b/aio/tools/transforms/angular-base-package/index.js index b94b79d100..f76b80762a 100644 --- a/aio/tools/transforms/angular-base-package/index.js +++ b/aio/tools/transforms/angular-base-package/index.js @@ -25,6 +25,7 @@ module.exports = new Package('angular-base', [ // Register the processors .processor(require('./processors/generateKeywords')) .processor(require('./processors/createOverviewDump')) + .processor(require('./processors/createSitemap')) .processor(require('./processors/checkUnbalancedBackTicks')) .processor(require('./processors/convertToJson')) .processor(require('./processors/fixInternalDocumentLinks')) diff --git a/aio/tools/transforms/angular-base-package/processors/createSitemap.js b/aio/tools/transforms/angular-base-package/processors/createSitemap.js new file mode 100644 index 0000000000..b9d4b89b51 --- /dev/null +++ b/aio/tools/transforms/angular-base-package/processors/createSitemap.js @@ -0,0 +1,15 @@ +module.exports = function createSitemap() { + return { + $runAfter: ['paths-computed'], + $runBefore: ['rendering-docs'], + $process(docs) { + docs.push({ + id: 'sitemap.xml', + path: 'sitemap.xml', + outputPath: '../sitemap.xml', + template: 'sitemap.template.xml', + urls: docs.filter(doc => doc.outputPath).map(doc => doc.path) + }); + } + }; +}; diff --git a/aio/tools/transforms/angular-base-package/processors/createSitemap.spec.js b/aio/tools/transforms/angular-base-package/processors/createSitemap.spec.js new file mode 100644 index 0000000000..c56d81f965 --- /dev/null +++ b/aio/tools/transforms/angular-base-package/processors/createSitemap.spec.js @@ -0,0 +1,51 @@ +var testPackage = require('../../helpers/test-package'); +var Dgeni = require('dgeni'); + +describe('createSitemap processor', () => { + var injector, processor; + + beforeEach(() => { + const dgeni = new Dgeni([testPackage('angular-base-package')]); + + injector = dgeni.configureInjector(); + processor = injector.get('createSitemap'); + }); + + it('should be available from the injector', () => { + expect(processor).toBeDefined(); + }); + + it('should run after "paths-computed"', () => { + expect(processor.$runAfter).toEqual(['paths-computed']); + }); + + it('should run before "rendering-docs"', () => { + expect(processor.$runBefore).toEqual(['rendering-docs']); + }); + + describe('$process', () => { + describe('should add a sitemap doc', () => { + + it('with the correct id, path, outputPath and template properties', () => { + const docs = []; + processor.$process(docs); + expect(docs.pop()).toEqual(jasmine.objectContaining({ + id: 'sitemap.xml', + path: 'sitemap.xml', + outputPath: '../sitemap.xml', + template: 'sitemap.template.xml' + })); + }); + + it('with an array of urls for each doc that has an outputPath', () => { + const docs = [ + { path: 'abc', outputPath: 'abc' }, + { path: 'cde' }, + { path: 'fgh', outputPath: 'fgh' }, + ]; + processor.$process(docs); + expect(docs.pop().urls).toEqual(['abc', 'fgh']); + }); + }); + }); +}); \ No newline at end of file diff --git a/aio/tools/transforms/templates/sitemap.template.xml b/aio/tools/transforms/templates/sitemap.template.xml new file mode 100644 index 0000000000..009dd5d66d --- /dev/null +++ b/aio/tools/transforms/templates/sitemap.template.xml @@ -0,0 +1,7 @@ + + + {%- for url in doc.urls %} + + https://angular.io/{$ url $} + {% endfor %} + \ No newline at end of file