From de19b8bb8818ea00d3949cd8f4204e5b3ef49ab0 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Sat, 24 Apr 2021 10:46:16 +0300 Subject: [PATCH] fix(docs-infra): do not process generated nodes for auto-linking (#41709) While generating the docs, when a `` element is inspected for auto-linking, the `autoLinkCode` Dgeni post-processor will break its contents up into words and generate text nodes for those words that should not be auto-linked. Previously, our text node visitor would visit these generated text nodes and try to auto-link them too. As a result, it would unnecessarily process nodes that had already been checked (and could potentially generate links that would otherwise be ignored). You can see an occurrence of this issue in the [Create the product list][1] section of the "Getting started with Angular" tutorial (look for ``). This commit fixes this by ensuring the visitor will skip the current node and any nodes generated by `autoLinkCode`. [1]: https://v11.angular.io/start#create-the-product-list PR Close #41709 --- .../post-processors/auto-link-code.js | 2 ++ .../post-processors/auto-link-code.spec.js | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) 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 91bfbf6330..4fe687ffd1 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 @@ -63,6 +63,8 @@ module.exports = function autoLinkCode(getDocFromAlias) { const nodes = getNodes(node, file); // Replace the text node with the links and leftover text nodes Array.prototype.splice.apply(parent.children, [index, 1].concat(nodes)); + // Do not visit this node's children or the newly added nodes + return [visit.SKIP, index + nodes.length]; } }); }); 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 6cd68bbd7a..cfd58ca55c 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 @@ -110,11 +110,32 @@ describe('autoLinkCode post-processor', () => { '{ xyz | myClass } ' + '{ xyz|myClass } ' + 'MyClass ' + - 'myClass ' + + 'myClass ' + 'OtherClass|MyClass' + ''); }); + it('should ignore generated nodes', () => { + 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 code items that match an internal API doc', () => { aliasMap.addDoc({ docType: 'class',