From 06bca0cc669680d875d294fdf0246b941026a861 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sat, 16 Nov 2019 22:12:04 +0000 Subject: [PATCH] fix(docs-infra): do not auto-link code in bash and json snippets (#33877) Previously any code block, which was not marked with `no-auto-link` css class would have its contents auto-linked to API pages. Sometimes this results in false positive links being generated. This is problematic for triple backticked blocks, which cannot provide the `no-auto-link` CSS class to prevent the linking. This commit fixes the problem by allowing the auto-linker to be configured not to auto-link code blocks that have been marked with an "ignored" language. By default these are `bash` and `json`. Triple backticked blocks are able to specify the language directly after the first set of triple backticks. Fixes #33859 PR Close #33877 --- .../post-processors/auto-link-code.js | 10 +++++++--- .../post-processors/auto-link-code.spec.js | 7 +++++++ 2 files changed, 14 insertions(+), 3 deletions(-) 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 589af005bf..7ed9f89b24 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 @@ -24,15 +24,19 @@ module.exports = function autoLinkCode(getDocFromAlias) { autoLinkCodeImpl.docTypes = []; autoLinkCodeImpl.customFilters = []; autoLinkCodeImpl.codeElements = ['code']; + autoLinkCodeImpl.ignoredLanguages = ['bash', 'sh', 'shell', 'json', 'markdown']; return autoLinkCodeImpl; function autoLinkCodeImpl() { return (ast) => { visit(ast, 'element', (node, ancestors) => { - // Only interested in code elements that are not inside links + // Only interested in code elements that: + // * do not have `no-auto-link` class + // * do not have an ignored language + // * are not inside links if (autoLinkCodeImpl.codeElements.some(elementType => is(node, elementType)) && - (!node.properties.className || - node.properties.className.indexOf('no-auto-link') === -1) && + (!node.properties.className || !node.properties.className.includes('no-auto-link')) && + !autoLinkCodeImpl.ignoredLanguages.includes(node.properties.language) && ancestors.every(ancestor => !is(ancestor, 'a'))) { visit(node, 'text', (node, ancestors) => { // Only interested in text nodes that are not inside links 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 fa8e3a740d..59281a5a73 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 @@ -168,4 +168,11 @@ describe('autoLinkCode post-processor', () => { processor.$process([doc]); expect(doc.renderedContent).toEqual('MyClass'); }); + + it('should ignore code blocks that are marked with an "ignored" language', () => { + aliasMap.addDoc({docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass'}); + const doc = {docType: 'test-doc', renderedContent: 'MyClass'}; + processor.$process([doc]); + expect(doc.renderedContent).toEqual('MyClass'); + }); });