【问题标题】:How to customise permalinks in Vuepress / Vuepress blog plugin?如何在 Vuepress / Vuepress 博客插件中自定义永久链接?
【发布时间】:2021-04-28 10:16:54
【问题描述】:

目前,Vuepress permalinks 只能在给定的模板变量上自定义。例如,给定以下目录结构:

posts
  coding
    my_post.md

并使用永久链接/:regular 我得到/posts/coding/my_post.md。但是,由于迁移和现有的 SEO 问题,我需要此链接为 /coding/my_post.md。有没有办法改变Vuepress blog plugin中生成的链接?

【问题讨论】:

    标签: vuepress


    【解决方案1】:

    不幸的是,除了模板变量之外,没有明确的方法可以自定义从 Vuepress 或 Vuepress 博客插件生成的永久链接。我发现的一个解决方案是使用一个自定义插件,将永久链接设置为我所需要的:

    module.exports = {
      extendPageData(pageCtx) {
        // We will force permalinks on the posts to be mapped
        // to root of the site. So /posts/my_post.md becomes /my_post.html
        if (pageCtx.path.startsWith("/posts")) {
          pageCtx.frontmatter.permalink = pageCtx.path.replace("/posts", "");
        }
      }
    };
    

    并按照Plugins page 上提供的指南使用插件。这个解决方案之所以有效,是因为 Vuepress respects the permalink frontmatter。我建议阅读更多关于 plugin options API 的内容,其中有更多关于如何扩展页面数据以及在这种情况下修改永久链接的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2017-10-02
      • 2020-09-08
      • 2019-04-23
      • 1970-01-01
      • 2019-04-17
      • 2020-05-12
      相关资源
      最近更新 更多