Skip to content

Commit f2f1fc1

Browse files
fix: add guard for full .md URLs
1 parent d2522a7 commit f2f1fc1

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/generators/metadata/utils/__tests__/parse.test.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,29 @@ describe('parseApiDoc', () => {
248248

249249
assert.strictEqual(findLink(entry)?.url, 'comparators.html#some-section');
250250
});
251+
252+
it('ignores .md full URLs with any protocol', () => {
253+
const protocolLinks = [
254+
'https://github.com/example/config.md',
255+
'http://internal-server.com/docs.md',
256+
'file:///C:/Shared/docs/readme.md',
257+
];
258+
259+
for (const url of protocolLinks) {
260+
const tree = u('root', [
261+
h('fs'),
262+
u('paragraph', [u('link', { url }, [u('text', 'external link')])]),
263+
]);
264+
const [entry] = parseApiDoc({ path, tree }, typeMap);
265+
266+
// Assert that the URL comes out exactly as it went in
267+
assert.strictEqual(
268+
findLink(entry)?.url,
269+
url,
270+
`Failed to ignore protocol: ${url}`
271+
);
272+
}
273+
});
251274
});
252275

253276
describe('document without headings', () => {

src/generators/metadata/utils/visitors.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import { transformNodesToString } from '../../../utils/unist.mjs';
1717
* @param {import('@types/mdast').Link} node A Markdown link node
1818
*/
1919
export const visitMarkdownLink = node => {
20+
// REJECT PROTOCOLS: Catches http:, https:, mailto:, ftp:, file:, vscode:, etc.
21+
if (/^[a-z]+:/i.test(node.url)) {
22+
return [SKIP];
23+
}
2024
node.url = node.url.replace(
2125
QUERIES.markdownUrl,
2226
(_, filename, hash = '') => `${basename(filename)}.html${hash}`

0 commit comments

Comments
 (0)