build(aio): blacklist unwanted URLs from the generated sitemap.xml (#22061)

Closes #22017

PR Close #22061
This commit is contained in:
Pete Bacon Darwin 2018-02-07 10:46:37 +00:00 committed by Miško Hevery
parent 545fdf10e2
commit 71ea931df5
2 changed files with 52 additions and 1 deletions

View File

@ -1,5 +1,15 @@
module.exports = function createSitemap() {
return {
blacklistedDocTypes: [
'navigation-json',
'contributors-json',
'resources-json',
],
blacklistedPaths: [
'test',
'file-not-found',
'overview-dump'
],
$runAfter: ['paths-computed'],
$runBefore: ['rendering-docs'],
$process(docs) {
@ -8,7 +18,16 @@ module.exports = function createSitemap() {
path: 'sitemap.xml',
outputPath: '../sitemap.xml',
template: 'sitemap.template.xml',
urls: docs.filter(doc => doc.outputPath).map(doc => doc.path)
urls: docs
// Filter out docs that are not outputted
.filter(doc => doc.outputPath)
// Filter out unwanted docs
.filter(doc => this.blacklistedDocTypes.indexOf(doc.docType) === -1)
.filter(doc => this.blacklistedPaths.indexOf(doc.path) === -1)
// Capture the path of each doc
.map(doc => doc.path)
// Convert the homepage: `index` to `/`
.map(path => path === 'index' ? '' : path)
});
}
};

View File

@ -46,6 +46,38 @@ describe('createSitemap processor', () => {
processor.$process(docs);
expect(docs.pop().urls).toEqual(['abc', 'fgh']);
});
it('ignoring blacklisted doc types', () => {
const docs = [
{ path: 'abc', outputPath: 'abc', docType: 'good' },
{ path: 'cde', outputPath: 'cde', docType: 'bad' },
{ path: 'fgh', outputPath: 'fgh', docType: 'good' },
];
processor.blacklistedDocTypes = ['bad'];
processor.$process(docs);
expect(docs.pop().urls).toEqual(['abc', 'fgh']);
});
it('ignoring blacklisted paths', () => {
const docs = [
{ path: 'abc', outputPath: 'abc' },
{ path: 'cde', outputPath: 'cde' },
{ path: 'fgh', outputPath: 'fgh' },
];
processor.blacklistedPaths = ['cde'];
processor.$process(docs);
expect(docs.pop().urls).toEqual(['abc', 'fgh']);
});
it('mapping the home page\'s path to `/`', () => {
const docs = [
{ path: 'abc', outputPath: 'abc' },
{ path: 'index', outputPath: 'index.json' },
{ path: 'fgh', outputPath: 'fgh' },
];
processor.$process(docs);
expect(docs.pop().urls).toEqual(['abc', '', 'fgh']);
});
});
});
});