build(aio): do not auto-link code elements already inside a link (#18776)
Closes #18769 PR Close #18776
This commit is contained in:
parent
7d72d0eb9b
commit
b8a3736275
|
@ -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"
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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>');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue