From d2522a7e6baa657c4ca9acbb6bbfcf01f21e5b23 Mon Sep 17 00:00:00 2001 From: Tushar <80577646+TusharThakur04@users.noreply.github.com> Date: Tue, 14 Apr 2026 04:51:15 +0530 Subject: [PATCH] fix(metadata): strip directory prefix when converting nested .md link to .html --- .../metadata/utils/__tests__/parse.test.mjs | 28 +++++++++++++++++++ src/generators/metadata/utils/visitors.mjs | 3 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/generators/metadata/utils/__tests__/parse.test.mjs b/src/generators/metadata/utils/__tests__/parse.test.mjs index b7b9d2f6..51b0b313 100644 --- a/src/generators/metadata/utils/__tests__/parse.test.mjs +++ b/src/generators/metadata/utils/__tests__/parse.test.mjs @@ -220,6 +220,34 @@ describe('parseApiDoc', () => { assert.strictEqual(findLink(entry)?.url, 'events.html#some-section'); }); + + it('strips subdirectory prefix from nested .md links', () => { + const tree = u('root', [ + h('fs'), + u('paragraph', [ + u('link', { url: 'namespaces/comparators.md' }, [ + u('text', 'comparators'), + ]), + ]), + ]); + const [entry] = parseApiDoc({ path, tree }, typeMap); + + assert.strictEqual(findLink(entry)?.url, 'comparators.html'); + }); + + it('strips subdirectory prefix and preserves hash fragments', () => { + const tree = u('root', [ + h('fs'), + u('paragraph', [ + u('link', { url: 'namespaces/comparators.md#some-section' }, [ + u('text', 'comparators'), + ]), + ]), + ]); + const [entry] = parseApiDoc({ path, tree }, typeMap); + + assert.strictEqual(findLink(entry)?.url, 'comparators.html#some-section'); + }); }); describe('document without headings', () => { diff --git a/src/generators/metadata/utils/visitors.mjs b/src/generators/metadata/utils/visitors.mjs index f260079d..9240e37b 100644 --- a/src/generators/metadata/utils/visitors.mjs +++ b/src/generators/metadata/utils/visitors.mjs @@ -1,4 +1,5 @@ 'use strict'; +import { basename } from 'node:path/posix'; import { SKIP } from 'unist-util-visit'; @@ -18,7 +19,7 @@ import { transformNodesToString } from '../../../utils/unist.mjs'; export const visitMarkdownLink = node => { node.url = node.url.replace( QUERIES.markdownUrl, - (_, filename, hash = '') => `${filename}.html${hash}` + (_, filename, hash = '') => `${basename(filename)}.html${hash}` ); return [SKIP];