From a938849148660f2353aa27c64138f59ef122f95f Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Sat, 24 Apr 2021 10:46:16 +0300 Subject: [PATCH] fix(docs-infra): apply custom `autoLinkCode` filters to single-word `` elements (#41709) Previously, the `autoLinkCode` Dgeni post-processor would not apply the custom filters when matching the whole contents of a `` element. This meant that custom filters would not be applied to single-word `` elements. You can see occurrences of this issue in the following sections of the "Reactive forms" guide: - [Creating nested form groups][1] (look for `street, city, state, and zip controls`) - [Using the FormBuilder service to generate controls][2] (look for `group method`) This commit fixes this by also applying the custom filters when processing the whole contents of a `` element. This commit also updates the `filterPipes` custom filter to allow matching a pipe's name in a single-word `` element (where there is no preceeding `|` character). [1]: https://v10.angular.io/guide/reactive-forms#creating-nested-form-groups [2]: https://v10.angular.io/guide/reactive-forms#using-the-formbuilder-service-to-generate-controls PR Close #41709 --- .../post-processors/auto-link-code.js | 12 ++++++---- .../post-processors/auto-link-code.spec.js | 23 ++++++++++++++++++- .../services/auto-link-filters/filterPipes.js | 3 ++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.js b/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.js index 4fe687ffd1..c0a370c294 100644 --- a/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.js +++ b/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.js @@ -55,7 +55,7 @@ module.exports = function autoLinkCode(getDocFromAlias) { const index = parent.children.indexOf(node); // Can we convert the whole text node into a doc link? - const docs = getDocFromAlias(node.value); + const docs = getFilteredDocsFromAlias([node.value], 0); if (foundValidDoc(docs, node.value, file)) { parent.children.splice(index, 1, createLinkNode(docs[0], node.value)); } else { @@ -89,14 +89,18 @@ module.exports = function autoLinkCode(getDocFromAlias) { return ancestors.some(ancestor => is(ancestor, 'a')); } + function getFilteredDocsFromAlias(words, index) { + // Remove docs that fail the custom filter tests. + return autoLinkCodeImpl.customFilters.reduce( + (docs, filter) => filter(docs, words, index), getDocFromAlias(words[index])); + } + function getNodes(node, file) { return textContent(node) .split(/([A-Za-z0-9_.-]+)/) .filter(word => word.length) .map((word, index, words) => { - // remove docs that fail the custom filter tests - const filteredDocs = autoLinkCodeImpl.customFilters.reduce( - (docs, filter) => filter(docs, words, index), getDocFromAlias(word)); + const filteredDocs = getFilteredDocsFromAlias(words, index); return foundValidDoc(filteredDocs, word, file) ? // Create a link wrapping the text node. diff --git a/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.spec.js b/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.spec.js index cfd58ca55c..f90572265c 100644 --- a/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.spec.js +++ b/aio/tools/transforms/angular-base-package/post-processors/auto-link-code.spec.js @@ -89,7 +89,7 @@ describe('autoLinkCode post-processor', () => { expect(doc.renderedContent).toEqual('xyz-MyClass'); }); - it('should ignore code items that are filtered out by custom filters', () => { + it('should ignore code items that are filtered out by custom filters (multiple words)', () => { autoLinkCode.customFilters = [filterPipes]; aliasMap.addDoc({ docType: 'pipe', @@ -115,6 +115,27 @@ describe('autoLinkCode post-processor', () => { ''); }); + it('should ignore code items that are filtered out by custom filters (single word)', () => { + const filterAnchors = (docs, words, index) => (words[index].toLowerCase() === 'a') ? [] : docs; + autoLinkCode.customFilters = [filterAnchors]; + autoLinkCode.docTypes = ['directive']; + + aliasMap.addDoc({ + docType: 'directive', + id: 'MyAnchorDirective', + aliases: ['MyAnchorDirective', 'a'], + path: 'a/b/my-anchor-directive', + }); + const doc = { + docType: 'test-doc', + renderedContent: 'a', + }; + + processor.$process([doc]); + + expect(doc.renderedContent).toBe('a'); + }); + it('should ignore generated nodes', () => { const filterAnchors = (docs, words, index) => (words[index].toLowerCase() === 'a') ? [] : docs; autoLinkCode.customFilters = [filterAnchors]; diff --git a/aio/tools/transforms/angular-base-package/services/auto-link-filters/filterPipes.js b/aio/tools/transforms/angular-base-package/services/auto-link-filters/filterPipes.js index ebaf0c5764..3f3b6b738e 100644 --- a/aio/tools/transforms/angular-base-package/services/auto-link-filters/filterPipes.js +++ b/aio/tools/transforms/angular-base-package/services/auto-link-filters/filterPipes.js @@ -8,5 +8,6 @@ module.exports = function filterPipes() { docs.filter(doc => doc.docType !== 'pipe' || doc.pipeOptions.name !== '\'' + words[index] + '\'' || - index > 0 && words[index - 1].trim() === '|'); + index === 0 || + words[index - 1].trim() === '|'); };