import { ensureEndingSlash, ensureLeadingSlash, isLinkExternal, isLinkWithProtocol, } from '@vuepress/helper/shared' /** * Whether a link must not be placed inside a scope * * Absolute, external, protocol, hash and query links are kept as is, only a * relative path is placed inside the scope of its group. * * 判断链接是否不应放入作用域 * * 绝对链接、外部链接、协议链接、哈希与查询链接保持原样,只有相对路径会放入所属分组的作用域。 * * @param link - The link to check / 需要判断的链接 * @returns Whether the link needs no scope / 链接是否无需作用域 */ export const isScopedLink = (link: string): boolean => link.startsWith('/') || link.startsWith('#') || link.startsWith('?') || isLinkExternal(link) || isLinkWithProtocol(link) /** * Resolve the link of an entry against the scope of its group * * An empty link points to the directory index of the scope, a relative link is * placed inside the scope, and an absolute, external, protocol, hash or query * link is kept as is. * * 根据分组的作用域解析条目的链接 * * 空链接指向作用域的目录首页,相对链接放置在作用域内,绝对链接、外部链接、协议链接、哈希与查询链接保持原样。 * * @example * resolveScopedLink('foo.md', '/guide/') * // '/guide/foo.md' * * @param link - The link to resolve / 需要解析的链接 * @param scope - The scope of the group / 分组的作用域 * @returns The resolved link / 解析后的链接 */ export const resolveScopedLink = (link: string, scope: string): string => isScopedLink(link) ? link : `${ensureEndingSlash(scope)}${link}` /** * Resolve the link of an entry against a prefix * * The same as {@link resolveScopedLink}, with the argument order of the `prefix` * option of the classic theme. * * 根据前缀解析条目的链接 * * 与 {@link resolveScopedLink} 相同,参数顺序对应经典主题的 `prefix` 选项。 * * @example * resolvePrefix('/guide/', 'foo.md') * // '/guide/foo.md' * * @default '' * @param prefix - The prefix of the group / 分组的前缀 * @param path - The link to resolve / 需要解析的链接 * @returns The resolved link / 解析后的链接 */ export const resolvePrefix = (prefix = '', path = ''): string => resolveScopedLink(path, prefix) /** * Normalize a directory to the scope it represents * * The scope always starts and ends with a slash, so that `guide` and `/guide/` * both resolve to `/guide/`. * * A value without a leading slash is always resolved from the site root, which * is how the generated sidebar data is looked up. * * 将目录规范化为其代表的作用域 * * 作用域总是以斜杠开头和结尾,因此 `guide` 与 `/guide/` 都会得到 `/guide/`。 * * 不以斜杠开头的值总是从站点根目录开始解析,与生成的侧边栏数据的查找方式一致。 * * @example * resolveDirectoryScope('guide') * // '/guide/' * * @param directory - The directory to normalize / 需要规范化的目录 * @returns The route scope / 路由范围 */ export const resolveDirectoryScope = (directory: string): string => ensureEndingSlash(ensureLeadingSlash(directory)) /** * Resolve the scope of a group from its `base` * * A base that does not start with a slash is relative to the scope of the * parent group. * * 从分组的 `base` 解析其作用域 * * 不以斜杠开头的 base 相对于父分组的作用域。 * * @example * resolveGroupScope('api/', '/guide/') * // '/guide/api/' * * @param base - The base of the group / 分组的基路径 * @param scope - The scope of the parent group / 父分组的作用域 * @returns The scope of the group / 分组的作用域 */ export const resolveGroupScope = ( base: string | undefined, scope: string, ): string => base ? resolveDirectoryScope( base.startsWith('/') ? base : `${ensureEndingSlash(scope)}${base}`, ) : scope