找回密码
 立即注册
首页 业界区 业界 Vuepress-Theme-Hope - 不显示“编辑此页”

Vuepress-Theme-Hope - 不显示“编辑此页”

康器 3 天前
直接上答案

theme.ts配置中添加下面这行配置项
  1. {
  2.   editLinkPattern: ":repo/edit/:branch/:path",// 我是部署的GitLab,具体匹配规则可以照着代码管理工具改
  3. }
复制代码
原因

花了几个小时,最后在源码里找到答案,我们先来看官方文档中这个配置项的说明:
1.png

文档中editLinkPattern与上面的editLink配置项放一起时极具迷惑性:
你可能直觉认为editLink配置项比editLinkPattern的优先级高,即editLink为true时,就应该展示“编辑此页”链接,哪怕自动生成的匹配规则错了,等测试发现后再来配置editLinkPattern。
这就埋下了坑,你如果不是内置支持的GitHub、Gitlab、Gitee和Bitbucket链接(例如内网链接),那么不配置这项就直接不会展示“编辑此页”链接,哪怕editLink配置为true。
源码解析
  1. // packages/theme/src/client/modules/info/components/PageMeta.ts
  2.     const {
  3.       repo,
  4.       docsRepo = repo,
  5.       docsBranch = "main",
  6.       docsDir = "",
  7.       editLink,
  8.       editLinkPattern = "",
  9.     } = themeLocale.value;
  10.     const showEditLink = frontmatter.value.editLink ?? editLink ?? true;
  11.     if (!showEditLink) return null;// 可以看到`editLink`就算为`true`,还要判断下面`link`的值
  12.     if (!docsRepo) return null;
  13.     const link = resolveEditLink({
  14.       docsRepo,
  15.       docsBranch,
  16.       docsDir,
  17.       editLinkPattern,
  18.       filePathRelative: page.value.filePathRelative,
  19.     });// 只要`resolveEditLink`方法返回null,同样不会渲染"编辑此页"链接
  20.     if (!link) return null;
  21.     return {
  22.       text: themeLocale.value.metaLocales.editLink,
  23.       link,
  24.     };
复制代码
我们再来看看resolveEditLink方法
  1. // packages/theme/src/client/modules/info/utils/resolveEditLink.ts
  2. export const resolveEditLink = ({
  3.   docsRepo,
  4.   docsBranch,
  5.   docsDir,
  6.   filePathRelative,
  7.   editLinkPattern,
  8. }: EditLinkOptions): string | null => {
  9.   if (!filePathRelative) return null;
  10.   const repoType = resolveRepoType(docsRepo);// 只要不是内置支持的链接,`resolveRepoType`方法就会返回null
  11.   let pattern: string | undefined;
  12.   if (editLinkPattern) pattern = editLinkPattern;// 这里`editLinkPattern`为"",就不会给`pattern`赋值
  13.   else if (repoType !== null) pattern = editLinkPatterns[repoType];// `repoType`为null,也不会给`pattern`赋值
  14.   if (!pattern) return null;// `pattern`为`undefined`,返回null,导致不渲染"编辑此页"链接
  15.   return pattern
  16.     .replace(
  17.       /:repo/u,
  18.       isLinkHttp(docsRepo) ? docsRepo : `https://github.com/${docsRepo}`,
  19.     )
  20.     .replace(/:branch/u, docsBranch)
  21.     .replace(
  22.       /:path/u,
  23.       removeLeadingSlash(`${removeEndingSlash(docsDir)}/${filePathRelative}`),
  24.     );
  25. };
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册