Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d78bc5

Browse files
committed
Core & UI: Improve createRelativeLink and getPageByHref for i18n usage
1 parent ef944c9 commit 7d78bc5

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

.changeset/tricky-singers-throw.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'fumadocs-core': patch
3+
'fumadocs-ui': patch
4+
---
5+
6+
Improve `createRelativeLink` and `getPageByHref` for i18n usage

packages/core/src/source/loader.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export interface LoaderOutput<Config extends LoaderConfig> {
9292
getPageByHref: (
9393
href: string,
9494
options?: {
95+
language?: string;
96+
9597
/**
9698
* resolve relative file paths in `href` from specified dirname, must be a virtual path.
9799
*/
@@ -299,30 +301,26 @@ function createOutput(options: LoaderOptions): LoaderOutput<LoaderConfig> {
299301
set pageTree(v) {
300302
pageTree = v;
301303
},
302-
getPageByHref(href, { dir = '' } = {}) {
303-
const pages = Array.from(walker.pages.values());
304+
getPageByHref(href, { dir = '', language } = {}) {
305+
const pages = this.getPages(language);
304306
const [value, hash] = href.split('#', 2);
307+
let target;
305308

306309
if (
307310
value.startsWith('.') &&
308311
(value.endsWith('.md') || value.endsWith('.mdx'))
309312
) {
310313
const hrefPath = joinPath(dir, value);
311-
const target = pages.find((item) => item.file.path === hrefPath);
312-
313-
if (target)
314-
return {
315-
page: target,
316-
hash,
317-
};
314+
target = pages.find((item) => item.file.path === hrefPath);
315+
} else {
316+
target = pages.find((item) => item.url === value);
318317
}
319318

320-
const target = pages.find((item) => item.url === value);
321-
if (target)
322-
return {
323-
page: target,
324-
hash,
325-
};
319+
if (!target) return;
320+
return {
321+
page: target,
322+
hash,
323+
};
326324
},
327325
getPages(language = options.i18n?.defaultLanguage ?? '') {
328326
const pages: Page[] = [];

packages/ui/src/mdx.server.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export function createRelativeLink(
1717
return async function RelativeLink({ href, ...props }) {
1818
// resolve relative href
1919
if (href && href.startsWith('.')) {
20-
const target = source.getPageByHref(href, { dir: page.file.dirname });
20+
const target = source.getPageByHref(href, {
21+
dir: page.file.dirname,
22+
language: page.locale,
23+
});
2124

2225
if (target) {
2326
href = target.hash

0 commit comments

Comments
 (0)