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
This commit is contained in:
Pete Bacon Darwin
2019-11-16 22:12:04 +00:00
committed by Alex Rickabaugh
parent 526537fefe
commit 06bca0cc66
2 changed files with 14 additions and 3 deletions

View File

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