From a3b2d6735adeba765513681f84d4360fada89bfb Mon Sep 17 00:00:00 2001 From: Kapunahele Wong Date: Fri, 17 Jan 2020 11:42:55 -0500 Subject: [PATCH] build(docs-infra): add auto-link-filter for generic words (#34835) Fixes #34794 PR Close #34835 --- .../transforms/angular-base-package/index.js | 5 +++-- .../auto-link-filters/ignoreGenericWords.js | 10 +++++++++ .../ignoreGenericWords.spec.js | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.js create mode 100644 aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.spec.js diff --git a/aio/tools/transforms/angular-base-package/index.js b/aio/tools/transforms/angular-base-package/index.js index 836170636b..7a163afbe3 100644 --- a/aio/tools/transforms/angular-base-package/index.js +++ b/aio/tools/transforms/angular-base-package/index.js @@ -40,6 +40,7 @@ module.exports = new Package('angular-base', [ .factory(require('./services/auto-link-filters/filterPipes')) .factory(require('./services/auto-link-filters/filterAmbiguousDirectiveAliases')) .factory(require('./services/auto-link-filters/ignoreHttpInUrls')) + .factory(require('./services/auto-link-filters/ignoreGenericWords')) .factory(require('./post-processors/add-image-dimensions')) .factory(require('./post-processors/auto-link-code')) @@ -129,9 +130,9 @@ module.exports = new Package('angular-base', [ }) - .config(function(postProcessHtml, addImageDimensions, autoLinkCode, filterPipes, filterAmbiguousDirectiveAliases, ignoreHttpInUrls) { + .config(function(postProcessHtml, addImageDimensions, autoLinkCode, filterPipes, filterAmbiguousDirectiveAliases, ignoreHttpInUrls, ignoreGenericWords) { addImageDimensions.basePath = path.resolve(AIO_PATH, 'src'); - autoLinkCode.customFilters = [ignoreHttpInUrls, filterPipes, filterAmbiguousDirectiveAliases]; + autoLinkCode.customFilters = [ignoreGenericWords, ignoreHttpInUrls, filterPipes, filterAmbiguousDirectiveAliases]; postProcessHtml.plugins = [ require('./post-processors/autolink-headings'), addImageDimensions, diff --git a/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.js b/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.js new file mode 100644 index 0000000000..5d3dab84d6 --- /dev/null +++ b/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.js @@ -0,0 +1,10 @@ + +/** + * This service is used by the autoLinkCode post-processor to ignore generic words + * that could be mistaken as classes, functions, etc. + */ + +module.exports = function ignoreGenericWords() { + const ignoredWords = new Set(['a', 'create', 'error', 'group', 'request', 'value']); + return (docs, words, index) => ignoredWords.has(words[index].toLowerCase()) ? [] : docs; +}; diff --git a/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.spec.js b/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.spec.js new file mode 100644 index 0000000000..de843a3c23 --- /dev/null +++ b/aio/tools/transforms/angular-base-package/services/auto-link-filters/ignoreGenericWords.spec.js @@ -0,0 +1,21 @@ +const ignoreGenericWords = require('./ignoreGenericWords')(); + +describe('ignoreGenericWords', () => { + it('should ignore generic words in all docs', () => { + const docs = [{docType: 'package', name: 'create'}, {docType: 'class', name: 'Foo'}]; + const words = ['create', 'a', 'thing']; + expect(ignoreGenericWords(docs, words, 0)).toEqual([]); + expect(ignoreGenericWords(docs, words, 1)).toEqual([]); + expect(ignoreGenericWords(docs, words, 2)).toEqual(docs); + }); + + it('should ignore generic words with mixed case in all docs', () => { + const docs = [{docType: 'package', name: 'create'}, {docType: 'class', name: 'Foo'}]; + const words = ['Create', 'eRrOr', 'STUFF']; + expect(ignoreGenericWords(docs, words, 0)).toEqual([]); + expect(ignoreGenericWords(docs, words, 1)).toEqual([]); + expect(ignoreGenericWords(docs, words, 2)).toEqual(docs); + }); +}); + +