From b8a37362755aae7f438936587beeb603dda8342e Mon Sep 17 00:00:00 2001
From: Peter Bacon Darwin <pete@bacondarwin.com>
Date: Fri, 18 Aug 2017 14:48:40 +0100
Subject: [PATCH] build(aio): do not auto-link code elements already inside a
 link (#18776)

Closes #18769

PR Close #18776
---
 aio/package.json                                           | 1 +
 .../angular-base-package/post-processors/auto-link-code.js | 6 +++---
 .../post-processors/auto-link-code.spec.js                 | 7 +++++++
 aio/yarn.lock                                              | 4 ++++
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/aio/package.json b/aio/package.json
index 28c876991b..6019a1e2ea 100644
--- a/aio/package.json
+++ b/aio/package.json
@@ -124,6 +124,7 @@
     "unist-util-filter": "^0.2.1",
     "unist-util-source": "^1.0.1",
     "unist-util-visit": "^1.1.1",
+    "unist-util-visit-parents": "^1.1.1",
     "vrsource-tslint-rules": "^4.0.1",
     "watchr": "^3.0.1",
     "yargs": "^7.0.2"
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 cb6e115d04..79b5f31668 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
@@ -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 = {
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 9ddd36a3e0..eec9b0a930 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
@@ -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>');
+  });
 });
diff --git a/aio/yarn.lock b/aio/yarn.lock
index 6efafbcf82..cc67d12936 100644
--- a/aio/yarn.lock
+++ b/aio/yarn.lock
@@ -7789,6 +7789,10 @@ unist-util-stringify-position@^1.0.0:
   dependencies:
     has "^1.0.1"
 
+unist-util-visit-parents@^1.1.1:
+  version "1.1.1"
+  resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-1.1.1.tgz#7d3f56b5b039a3c6e2d16e51cc093f10e4755342"
+
 unist-util-visit@^1.0.0, unist-util-visit@^1.1.0, unist-util-visit@^1.1.1:
   version "1.1.1"
   resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.1.1.tgz#e917a3b137658b335cb4420c7da2e74d928e4e94"