build(aio): do not auto-link code elements already inside a link (#18776)

Closes #18769

PR Close #18776
This commit is contained in:
Peter Bacon Darwin
2017-08-18 14:48:40 +01:00
committed by Miško Hevery
parent efee81eb57
commit 3b571a4f3d
4 changed files with 15 additions and 3 deletions

View File

@ -1,4 +1,4 @@
const visit = require('unist-util-visit');
const visit = require('unist-util-visit-parents');
const is = require('hast-util-is-element');
const textContent = require('hast-util-to-string');
@ -17,8 +17,8 @@ module.exports = function autoLinkCode(getDocFromAlias) {
function autoLinkCodeImpl() {
return (ast) => {
visit(ast, node => {
if (is(node, 'code')) {
visit(ast, (node, ancestors) => {
if (is(node, 'code') && ancestors.every(ancestor => !is(ancestor, 'a'))) {
const docs = getDocFromAlias(textContent(node));
if (docs.length === 1 && autoLinkCodeImpl.docTypes.indexOf(docs[0].docType) !== -1) {
const link = {

View File

@ -36,4 +36,11 @@ describe('autoLinkCode post-processor', () => {
processor.$process([doc]);
expect(doc.renderedContent).toEqual('<code>MyClass</code>');
});
it('should ignore code items that are already inside a link', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: '<a href="..."><div><code>MyClass</code></div></a>' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('<a href="..."><div><code>MyClass</code></div></a>');
});
});