Skip to content

Commit d2522a7

Browse files
fix(metadata): strip directory prefix when converting nested .md link to .html
1 parent 3d00fa8 commit d2522a7

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,34 @@ describe('parseApiDoc', () => {
220220

221221
assert.strictEqual(findLink(entry)?.url, 'events.html#some-section');
222222
});
223+
224+
it('strips subdirectory prefix from nested .md links', () => {
225+
const tree = u('root', [
226+
h('fs'),
227+
u('paragraph', [
228+
u('link', { url: 'namespaces/comparators.md' }, [
229+
u('text', 'comparators'),
230+
]),
231+
]),
232+
]);
233+
const [entry] = parseApiDoc({ path, tree }, typeMap);
234+
235+
assert.strictEqual(findLink(entry)?.url, 'comparators.html');
236+
});
237+
238+
it('strips subdirectory prefix and preserves hash fragments', () => {
239+
const tree = u('root', [
240+
h('fs'),
241+
u('paragraph', [
242+
u('link', { url: 'namespaces/comparators.md#some-section' }, [
243+
u('text', 'comparators'),
244+
]),
245+
]),
246+
]);
247+
const [entry] = parseApiDoc({ path, tree }, typeMap);
248+
249+
assert.strictEqual(findLink(entry)?.url, 'comparators.html#some-section');
250+
});
223251
});
224252

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

src/generators/metadata/utils/visitors.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
import { basename } from 'node:path/posix';
23

34
import { SKIP } from 'unist-util-visit';
45

@@ -18,7 +19,7 @@ import { transformNodesToString } from '../../../utils/unist.mjs';
1819
export const visitMarkdownLink = node => {
1920
node.url = node.url.replace(
2021
QUERIES.markdownUrl,
21-
(_, filename, hash = '') => `${filename}.html${hash}`
22+
(_, filename, hash = '') => `${basename(filename)}.html${hash}`
2223
);
2324

2425
return [SKIP];

0 commit comments

Comments
 (0)