build(aio): tighten up code autolinking (#20468)

Do not match code "words" that are part of a hyphenated
string of characters: e.g. `platform-browser-dynamic` should
not auto-link `browser`.

Do not match code "words" that correspond to pipe names
but are not preceded by a pipe `|` character. E.g. `package.json` should
not auto link `json` to the `JsonPipe`.

Closes #20187

PR Close #20468
This commit is contained in:
Peter Bacon Darwin
2017-11-15 23:30:09 +00:00
committed by Miško Hevery
parent 68b53c07fd
commit 7c44637fbf
5 changed files with 86 additions and 7 deletions

View File

@ -2,7 +2,7 @@ var createTestPackage = require('../../helpers/test-package');
var Dgeni = require('dgeni');
describe('autoLinkCode post-processor', () => {
let processor, autoLinkCode, aliasMap;
let processor, autoLinkCode, aliasMap, filterPipes;
beforeEach(() => {
const testPackage = createTestPackage('angular-base-package');
@ -14,6 +14,7 @@ describe('autoLinkCode post-processor', () => {
processor = injector.get('postProcessHtml');
processor.docTypes = ['test-doc'];
processor.plugins = [autoLinkCode];
filterPipes = injector.get('filterPipes');
});
it('should insert an anchor into every code item that matches the id of an API doc', () => {
@ -51,6 +52,26 @@ describe('autoLinkCode post-processor', () => {
expect(doc.renderedContent).toEqual('<code>MyClass</code>');
});
it('should ignore code items that match an API doc but are attached to other text via a dash', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: '<code>xyz-MyClass</code>' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('<code>xyz-MyClass</code>');
});
it('should ignore code items that are filtered out by custom filters', () => {
autoLinkCode.customFilters = [filterPipes];
aliasMap.addDoc({ docType: 'pipe', id: 'MyClass', aliases: ['MyClass', 'myClass'], path: 'a/b/myclass', pipeOptions: { name: '\'myClass\'' } });
const doc = { docType: 'test-doc', renderedContent: '<code>{ xyz | myClass } { xyz|myClass } MyClass myClass OtherClass|MyClass</code>' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('<code>' +
'{ xyz | <a href="a/b/myclass" class="code-anchor">myClass</a> } ' +
'{ xyz|<a href="a/b/myclass" class="code-anchor">myClass</a> } ' +
'<a href="a/b/myclass" class="code-anchor">MyClass</a> ' +
'myClass OtherClass|<a href="a/b/myclass" class="code-anchor">MyClass</a>' +
'</code>');
});
it('should insert anchors for individual text nodes within a code block', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: '<code><span>MyClass</span><span>MyClass</span></code>' };