Skip to content

Commit 143e923

Browse files
[fix] resolve empty overview links at build time
- onEmptyLinkMatch resolved the target from the page directory instead of the docs root, so [](api/...) links stayed empty and unresolved - now resolves from the docs root and emits a root-absolute href without the .md/.mdx extension, matching normalizeMarkdownMdLinks - guard against a missing file or absent sidebar_label to avoid build crash
1 parent d09cf17 commit 143e923

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

docusaurus.config.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,16 @@ const readFile = (workingDir, filePath) => {
9191
const onEmptyLinkMatch = (data, { key, fullMatch, dir }) => {
9292
const filePath = fullMatch.substring(fullMatch.indexOf('(') + 1, fullMatch.length - 1);
9393
if (filePath.indexOf('.md') !== -1 || filePath.indexOf('.mdx') !== -1 || filePath.indexOf('.') === -1) {
94-
const data = readFile(dir, filePath);
95-
return data ? `[${/.*sidebar_label: (.+)/g.exec(data)[1]}]${fullMatch.match(/\(\D+\)/g)[0]}` : fullMatch;
94+
// Links are written root-relative (e.g. api/events/add-field-event.md), so resolve
95+
// the target from the docs root rather than the page's own directory.
96+
const fileContent = readFile(path.join(__dirname, 'docs'), filePath);
97+
if (!fileContent) return fullMatch;
98+
const labelMatch = /sidebar_label: (.+)/.exec(fileContent);
99+
if (!labelMatch) return fullMatch;
100+
// Emit a root-absolute href without the .md/.mdx extension, matching how
101+
// normalizeMarkdownMdLinks rewrites ordinary links.
102+
const href = '/' + filePath.replace(/^\.?\/+/, '').replace(/\.(md|mdx)(?=$|#)/, '');
103+
return `[${labelMatch[1].trim()}](${href})`;
96104
}
97105
return fullMatch;
98106
};

0 commit comments

Comments
 (0)