mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-29 06:28:23 +02:00
Markdown links like `[xx](/workspaces)` written in `*.zh.mdx` rendered as bare `<a href="/workspaces">`, which Next's basePath rewrote to `/docs/workspaces` and the docs middleware then routed to English — silently kicking Chinese readers out of their locale on every internal click. Add a `LocaleLink` MDX `a` override that runs every internal href through `prefixLocale(href, lang)` before passing it to `next/link`, and wire a `DocsLocaleProvider` around the MDX body in both page entry points so the override and `NumberedCard` know the active locale. External links, in-page anchors, relative paths, already-prefixed paths, and default-language pages are deliberately left untouched. Closes the bug reported in https://github.com/multica-ai/multica/issues/2173. Co-authored-by: multica-agent <github@multica.ai>
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import {
|
|
createContext,
|
|
useContext,
|
|
type AnchorHTMLAttributes,
|
|
type ReactNode,
|
|
} from "react";
|
|
import { i18n, type Lang } from "@/lib/i18n";
|
|
import { prefixLocale } from "@/lib/locale-link";
|
|
|
|
const DocsLocaleContext = createContext<Lang>(i18n.defaultLanguage as Lang);
|
|
|
|
// Wraps the rendered MDX subtree so descendant <LocaleLink>s and any
|
|
// editorial component using `useDocsLocale()` know which language the page
|
|
// was rendered in. Mounted at each docs page entry; never elsewhere.
|
|
export function DocsLocaleProvider({
|
|
lang,
|
|
children,
|
|
}: {
|
|
lang: Lang;
|
|
children: ReactNode;
|
|
}) {
|
|
return (
|
|
<DocsLocaleContext.Provider value={lang}>
|
|
{children}
|
|
</DocsLocaleContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useDocsLocale(): Lang {
|
|
return useContext(DocsLocaleContext);
|
|
}
|
|
|
|
// Drop-in replacement for the MDX-rendered `<a>` element. Keeps the same
|
|
// surface shape as the default `a` from `defaultMdxComponents` but routes
|
|
// internal links through the locale prefixer + next/link so client-side
|
|
// navigation stays inside the active locale.
|
|
export function LocaleLink({
|
|
href,
|
|
...rest
|
|
}: AnchorHTMLAttributes<HTMLAnchorElement> & { href?: string }) {
|
|
const lang = useDocsLocale();
|
|
if (!href) return <a {...rest} />;
|
|
const final = prefixLocale(href, lang);
|
|
return <Link href={final} {...rest} />;
|
|
}
|