mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(docs-infra): don't auto-link code symbols used as link text
When an inline code symbol is used as the text of an explicit markdown link (e.g. [`httpResource`](/guide/http/http-resource)), the codespan renderer recognized it as an API symbol and wrapped it in a second anchor pointing at the API reference. This produced nested <a> tags, so the explicit link was effectively replaced by the API symbol link. Disable auto-linking while rendering a link's inner tokens so the explicit href is preserved, matching the pattern already used by the heading and docs-card renderers. Fixes #69549
This commit is contained in:
committed by
Alex Rickabaugh
parent
2df56d7de0
commit
cbd97072f4
@@ -2,3 +2,4 @@
|
||||
[same page](#test)
|
||||
[same site](../other/page)
|
||||
[npm packages](https://docs.npmjs.com/getting-started/what-is-npm 'What is npm?')
|
||||
[`CommonModule`](../other/page)
|
||||
|
||||
@@ -38,6 +38,10 @@ describe('markdown to html', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should not auto-link a code symbol used as link text', () => {
|
||||
expect(parsedMarkdown).toContain('<a href="../other/page"><code>CommonModule</code></a>');
|
||||
});
|
||||
|
||||
it('should throw if on absolute links to adev', async () => {
|
||||
try {
|
||||
parsedMarkdown = await parseMarkdown(
|
||||
|
||||
@@ -35,5 +35,12 @@ export function linkRender(this: AdevDocsRenderer, {href, title, tokens}: Tokens
|
||||
}
|
||||
|
||||
const titleAttribute = title ? ` title="${title}"` : '';
|
||||
return `<a href="${href}"${titleAttribute}${anchorTarget(href)}>${this.parser.parseInline(tokens)}</a>`;
|
||||
// Disable auto-linking while rendering the link's content so that a code symbol used as the
|
||||
// link text (e.g. [`httpResource`](/guide/http/http-resource)) isn't turned into a nested
|
||||
// anchor pointing at the API reference, which would override the explicit link.
|
||||
const previousDisableAutoLinking = this.context.disableAutoLinking;
|
||||
this.context.disableAutoLinking = true;
|
||||
const content = this.parser.parseInline(tokens);
|
||||
this.context.disableAutoLinking = previousDisableAutoLinking;
|
||||
return `<a href="${href}"${titleAttribute}${anchorTarget(href)}>${content}</a>`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user