fumadocs-core
Version:
The React.js library for building a documentation website
31 lines (30 loc) • 1 kB
JavaScript
"use client";
import { Link } from "./link.js";
import { useParams } from "./framework/index.js";
import { useMemo } from "react";
import { jsx } from "react/jsx-runtime";
// src/dynamic-link.tsx
/**
* Extends the default `Link` component
*
* It supports dynamic hrefs, which means you can use `/[lang]/my-page` with `dynamicHrefs` enabled
*/
function DynamicLink({ href, ref, ...props }) {
const params = useParams();
const url = useMemo(() => href ? updateHref(href, params) : href, [params, href]);
return /* @__PURE__ */ jsx(Link, {
ref,
href: url,
...props
});
}
function updateHref(href, params) {
return href.replace(/\[(.*)]\/?/, (match, key) => {
const hasEndingSlash = match[match.length - 1] === "/";
const value = key in params ? params[key] : void 0;
if (!value) return "";
const replacement = typeof value === "string" ? value : value.join("/");
return hasEndingSlash ? `${replacement}/` : replacement;
});
}
export { DynamicLink, DynamicLink as default, updateHref };