Skip to content

Commit 7c5da93

Browse files
authored
fix(docs): only rewrite .md links on anchor tags (#187)
The docs link plugin rewrote any href= in the rendered HTML, including escaped markup inside code samples, so an example like <a href="./x.md"> in a code block was changed to a site route. It now only rewrites the href of real <a> tags.
1 parent fb3f961 commit 7c5da93

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎apps/docs/md-links.plugin.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ describe('resolveDocLinks', () => {
4141
'<a href="/agents/tools">c</a> <a href="https://github.com/x/README.md">d</a>',
4242
);
4343
});
44+
45+
it('leaves escaped markup in code and non-anchor attributes alone', () => {
46+
const html =
47+
'<pre><code>&lt;a href="./configuration.md"&gt;</code></pre> ' +
48+
'<a data-href="./security.md" href="./security.md">a</a> <link href="./x.md">';
49+
expect(resolveDocLinks(html, '/src/content/getting-started/installation')).toBe(
50+
'<pre><code>&lt;a href="./configuration.md"&gt;</code></pre> ' +
51+
'<a data-href="./security.md" href="/getting-started/security">a</a> <link href="./x.md">',
52+
);
53+
});
4454
});
4555

4656
describe('mdLinksPlugin', () => {

‎apps/docs/md-links.plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolveMdHref(href: string, pageFile: string): string | null {
4343

4444
export function resolveDocLinks(html: string, pageFile: string): string {
4545
return html.replace(
46-
/(\shref=)(["'])([^"']*)\2/g,
46+
/(<a\s(?:[^>]*?\s)?href=)(["'])([^"']*)\2/gi,
4747
(match, attr: string, quote: string, href: string) => {
4848
const route = resolveMdHref(href, pageFile);
4949
return route === null ? match : `${attr}${quote}${route}${quote}`;

0 commit comments

Comments
 (0)