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:
parent
de19b8bb88
commit
a938849148
|
@ -55,7 +55,7 @@ module.exports = function autoLinkCode(getDocFromAlias) {
|
||||||
const index = parent.children.indexOf(node);
|
const index = parent.children.indexOf(node);
|
||||||
|
|
||||||
// Can we convert the whole text node into a doc link?
|
// 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)) {
|
if (foundValidDoc(docs, node.value, file)) {
|
||||||
parent.children.splice(index, 1, createLinkNode(docs[0], node.value));
|
parent.children.splice(index, 1, createLinkNode(docs[0], node.value));
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,14 +89,18 @@ module.exports = function autoLinkCode(getDocFromAlias) {
|
||||||
return ancestors.some(ancestor => is(ancestor, 'a'));
|
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) {
|
function getNodes(node, file) {
|
||||||
return textContent(node)
|
return textContent(node)
|
||||||
.split(/([A-Za-z0-9_.-]+)/)
|
.split(/([A-Za-z0-9_.-]+)/)
|
||||||
.filter(word => word.length)
|
.filter(word => word.length)
|
||||||
.map((word, index, words) => {
|
.map((word, index, words) => {
|
||||||
// remove docs that fail the custom filter tests
|
const filteredDocs = getFilteredDocsFromAlias(words, index);
|
||||||
const filteredDocs = autoLinkCodeImpl.customFilters.reduce(
|
|
||||||
(docs, filter) => filter(docs, words, index), getDocFromAlias(word));
|
|
||||||
|
|
||||||
return foundValidDoc(filteredDocs, word, file) ?
|
return foundValidDoc(filteredDocs, word, file) ?
|
||||||
// Create a link wrapping the text node.
|
// Create a link wrapping the text node.
|
||||||
|
|
|
@ -89,7 +89,7 @@ describe('autoLinkCode post-processor', () => {
|
||||||
expect(doc.renderedContent).toEqual('<code>xyz-MyClass</code>');
|
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];
|
autoLinkCode.customFilters = [filterPipes];
|
||||||
aliasMap.addDoc({
|
aliasMap.addDoc({
|
||||||
docType: 'pipe',
|
docType: 'pipe',
|
||||||
|
@ -115,6 +115,27 @@ describe('autoLinkCode post-processor', () => {
|
||||||
'</code>');
|
'</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', () => {
|
it('should ignore generated nodes', () => {
|
||||||
const filterAnchors = (docs, words, index) => (words[index].toLowerCase() === 'a') ? [] : docs;
|
const filterAnchors = (docs, words, index) => (words[index].toLowerCase() === 'a') ? [] : docs;
|
||||||
autoLinkCode.customFilters = [filterAnchors];
|
autoLinkCode.customFilters = [filterAnchors];
|
||||||
|
|
|
@ -8,5 +8,6 @@ module.exports = function filterPipes() {
|
||||||
docs.filter(doc =>
|
docs.filter(doc =>
|
||||||
doc.docType !== 'pipe' ||
|
doc.docType !== 'pipe' ||
|
||||||
doc.pipeOptions.name !== '\'' + words[index] + '\'' ||
|
doc.pipeOptions.name !== '\'' + words[index] + '\'' ||
|
||||||
index > 0 && words[index - 1].trim() === '|');
|
index === 0 ||
|
||||||
|
words[index - 1].trim() === '|');
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue