fix(docs-infra): do not auto-link `http://` to the `common/http` (#31051)

Previously, our auto-linking feature would match `http` in URLs (such as
`http://...`) to the `common/http` package and automatically create a
link to that, which was undesirable. While it is possible to work around
that via `<code class="no-auto-link">http://...</code>`, most people
didn't even realize the issue.

Since in this case it is possible to reliably know it is a false match,
this commit fixes it by applying a custom auto-link filter that ignores
all docs for `http`, if it comes before `://`.

Fixes #31012

PR Close #31051
This commit is contained in:
George Kalpakas 2019-06-14 14:02:56 +03:00 committed by Kara Erickson
parent 7814ef55c5
commit c6b180380a
5 changed files with 67 additions and 4 deletions

View File

@ -38,6 +38,7 @@ module.exports = new Package('angular-base', [
.factory(require('./services/copyFolder')) .factory(require('./services/copyFolder'))
.factory(require('./services/filterPipes')) .factory(require('./services/filterPipes'))
.factory(require('./services/filterAmbiguousDirectiveAliases')) .factory(require('./services/filterAmbiguousDirectiveAliases'))
.factory(require('./services/ignoreHttpInUrls'))
.factory(require('./services/getImageDimensions')) .factory(require('./services/getImageDimensions'))
.factory(require('./post-processors/add-image-dimensions')) .factory(require('./post-processors/add-image-dimensions'))
@ -128,9 +129,9 @@ module.exports = new Package('angular-base', [
}) })
.config(function(postProcessHtml, addImageDimensions, autoLinkCode, filterPipes, filterAmbiguousDirectiveAliases) { .config(function(postProcessHtml, addImageDimensions, autoLinkCode, filterPipes, filterAmbiguousDirectiveAliases, ignoreHttpInUrls) {
addImageDimensions.basePath = path.resolve(AIO_PATH, 'src'); addImageDimensions.basePath = path.resolve(AIO_PATH, 'src');
autoLinkCode.customFilters = [filterPipes, filterAmbiguousDirectiveAliases]; autoLinkCode.customFilters = [ignoreHttpInUrls, filterPipes, filterAmbiguousDirectiveAliases];
postProcessHtml.plugins = [ postProcessHtml.plugins = [
require('./post-processors/autolink-headings'), require('./post-processors/autolink-headings'),
addImageDimensions, addImageDimensions,

View File

@ -38,7 +38,7 @@ module.exports = function autoLinkCode(getDocFromAlias) {
// Only interested in text nodes that are not inside links // Only interested in text nodes that are not inside links
if (ancestors.every(ancestor => !is(ancestor, 'a'))) { if (ancestors.every(ancestor => !is(ancestor, 'a'))) {
const parent = ancestors[ancestors.length-1]; const parent = ancestors[ancestors.length - 1];
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?
@ -66,6 +66,7 @@ module.exports = function autoLinkCode(getDocFromAlias) {
}); });
}; };
} }
function foundValidDoc(docs) { function foundValidDoc(docs) {
return docs.length === 1 && return docs.length === 1 &&
!docs[0].internal && !docs[0].internal &&

View File

@ -1,6 +1,6 @@
/** /**
* This service is used by the autoLinkCode post-processors to filter out pipe docs * This service is used by the autoLinkCode post-processor to filter out pipe docs
* where the matching word is the pipe name and is not preceded by a pipe * where the matching word is the pipe name and is not preceded by a pipe
*/ */
module.exports = function filterPipes() { module.exports = function filterPipes() {

View File

@ -0,0 +1,12 @@
/**
* This service is used by the autoLinkCode post-processor to ignore the `http(s)` part in URLs
* (i.e. `http://...` or `https://...`).
*/
module.exports = function ignoreHttpInUrls() {
const ignoredSchemes = ['http', 'https'];
return (docs, words, index) => {
const httpInUrl = ignoredSchemes.includes(words[index]) && (words[index + 1] === '://');
return httpInUrl ? [] : docs;
};
};

View File

@ -0,0 +1,49 @@
const ignoreHttpInUrls = require('./ignoreHttpInUrls')();
describe('ignoreHttpInUrls', () => {
it('should ignore all docs when matching `http` in `http://...`', () => {
const docs = [{ docType: 'package', name: 'http' }, { docType: 'class', name: 'Foo' }];
const words1 = ['http', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words1, 0)).toEqual([]);
const words2 = ['URL', ': ', 'http', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words2, 2)).toEqual([]);
});
it('should ignore all docs when matching `https` in `https://...`', () => {
const docs = [{ docType: 'package', name: 'https' }, { docType: 'class', name: 'Foo' }];
const words1 = ['https', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words1, 0)).toEqual([]);
const words2 = ['URL', ': ', 'https', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words2, 2)).toEqual([]);
});
it('should keep all docs when not matching `http(s)`', () => {
const docs = [{ docType: 'package', name: 'http' }, { docType: 'class', name: 'Foo' }];
const words1 = ['http', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words1, 2)).toEqual(docs);
const words2 = ['URL', ': ', 'https', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words2, 0)).toEqual(docs);
const words3 = ['file', '://', 'example', '.', 'com', '/'];
expect(ignoreHttpInUrls(docs, words3, 0)).toEqual(docs);
});
it('should keep all docs when not matching `http(s)` at the beginning of a URL', () => {
const docs = [{ docType: 'package', name: 'http' }, { docType: 'class', name: 'Foo' }];
const words1 = ['http', ' ', 'is', ' ', 'cool'];
expect(ignoreHttpInUrls(docs, words1, 0)).toEqual(docs);
const words2 = ['https', ' ', 'is', ' ', 'cooler'];
expect(ignoreHttpInUrls(docs, words2, 0)).toEqual(docs);
const words3 = ['http', '://', 'http', '.', 'com'];
expect(ignoreHttpInUrls(docs, words3, 2)).toEqual(docs);
});
});