【问题标题】:gatsby-plugin-mdx with rehype-autolink-headers only working with wrap option带有 rehype-autolink-headers 的 gatsby-plugin-mdx 仅适用于 wrap 选项
【发布时间】:2021-09-09 07:23:11
【问题描述】:

我正在尝试使用 Gatsby + MDX 设置我的网站。我正在查看文档here 并希望使用autolink-header-option。我已经尝试为此使用 rehype 和 remark 插件,但我只能使用 Rehype 插件并且只能使用 wrap 选项。我更喜欢标题前有链接的 GitHub(默认)样式。

我在gatsby-config.js 中使用以下配置,并在更新文件后清除了.cachepublic,以确保没有缓存任何内容。我也没有收到任何错误,一切都成功构建并运行,只是没有任何指向标题的链接。

{
  resolve: `gatsby-plugin-mdx`,
  options: {
    rehypePlugins: [
      // To pass options, use a 2-element array with the
      // configuration in an object in the second element
      require("rehype-autolink-headings"),
    ],
  },
},

更新

在尝试了多种配置后,我能够让它按预期工作的方法是使用不同的插件配置。

{
  resolve: `gatsby-plugin-mdx`,
  options: {
    gatsbyRemarkPlugins: [ `gatsby-remark-autolink-headers` ],
    plugins: [ `gatsby-remark-autolink-headers` ],
  },
}

gatsbyRemarkPluginsplugins 都是必需的:https://github.com/gatsbyjs/gatsby/issues/15486

【问题讨论】:

    标签: reactjs gatsby mdxjs gatsby-plugin-mdx


    【解决方案1】:

    rehype-autolink-headingsREADME 提到:

    此软件包适用于具有 ID 的标题。向标题添加 ID 的一种方法是在此插件之前使用 remark-slug。

    将您的配置更改为以下内容应该可以解决它:

    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        rehypePlugins: [
          require("rehype-slug"),
          // To pass options, use a 2-element array with the
          // configuration in an object in the second element
          require("rehype-autolink-headings"),
        ],
      },
    },
    

    事实上,您链接到的文档也有这个额外的require 行,但它没有说明它的用途。

    【讨论】:

    • 感谢您的回复,我没有看到该 id 是必需的,因此尝试不使用 rehype-slug 插件。你用过这个/有它的工作吗?当我设置这个时,我得到一个 <a> 标签被应用,但它没有孩子,所以不能被点击。最后,我改用了gatsby-remark-autolink-headers,正如我在上面的原始帖子中更新的那样。
    • 要将标题包装在<a> 标签中,我认为您需要指定之前的wrap 选项:[require("rehype-autolink-headings"), { behavior: "wrap" }]
    猜你喜欢
    • 2021-11-08
    • 2021-07-17
    • 2022-01-11
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2020-06-04
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多