fix(docs-infra): apply custom `autoLinkCode` filters to single-word `<code>` elements (#41709)

Previously, the `autoLinkCode` Dgeni post-processor would not apply the
custom filters when matching the whole contents of a `<code>` element.
This meant that custom filters would not be applied to single-word
`<code>` 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 `<code>` element.

This commit also updates the `filterPipes` custom filter to allow
matching a pipe's name in a single-word `<code>` 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
This commit is contained in:
George Kalpakas 2021-04-24 10:46:16 +03:00 committed by Jessica Janiuk
parent de19b8bb88
commit a938849148
3 changed files with 32 additions and 6 deletions

View File

@ -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.

View File

@ -89,7 +89,7 @@ describe('autoLinkCode post-processor', () => {
expect(doc.renderedContent).toEqual('<code>xyz-MyClass</code>');
});
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', () => {
'</code>');
});
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: '<code>a</code>',
};
processor.$process([doc]);
expect(doc.renderedContent).toBe('<code>a</code>');
});
it('should ignore generated nodes', () => {
const filterAnchors = (docs, words, index) => (words[index].toLowerCase() === 'a') ? [] : docs;
autoLinkCode.customFilters = [filterAnchors];

View File

@ -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() === '|');
};