import { resolveRoute } from 'vuepress/client' /** * The resolved link of a theme entry * * 主题条目的解析后链接 */ export interface ResolvedAutoLink { /** * The icon of the target page * * 目标页面的图标 */ icon?: string /** * The route path of the target page * * 目标页面的路由路径 */ link: string /** * Whether the route of the target page is missing * * 目标页面的路由是否缺失 */ notFound: boolean /** * The text of the link * * 链接的文本 */ text: string } /** * Resolve the route, text and icon of a link with the route table * * The route table maps `README.md`, `*.md` and extension-less paths to their * real route paths, so `/guide/README.md` resolves to `/guide/`. When the route * is missing the link is kept as is and the text falls back to it. * * 使用路由表解析链接的路由、文本与图标 * * 路由表会把 `README.md`、`*.md` 与无扩展名的路径映射到真实路由路径,因此 `/guide/README.md` 会解析为 * `/guide/`。当路由缺失时,链接会原样保留,文本回退为该链接。 * * @example * resolveAutoLink('/guide/README.md') * // { link: '/guide/', text: 'Guide', notFound: false } * * @param link - The link to resolve / 要解析的链接 * @param currentPath - The current route path / 当前路由路径 * @returns The resolved link / 解析后的链接 */ export const resolveAutoLink = ( link: string, currentPath?: string, ): ResolvedAutoLink => { const { meta, notFound, path } = resolveRoute<{ icon?: string title?: string }>(link, currentPath) if (notFound) return { link: path, notFound: true, text: path } return { link: path, notFound: false, text: meta.title || path, ...(meta.icon ? { icon: meta.icon } : {}), } }