【问题标题】:Gatsby MDX slugs unnavigable, contain directory in nameGatsby MDX slugs 无法导航,名称中包含目录
【发布时间】:2021-03-06 14:49:00
【问题描述】:

创建的 slug 包含根目录,因此将 slug 传递给<Link to={slug}> 被破坏。 页面已创建,但链接到它们的 slug 不必要地包含包含文件夹,无论是帖子还是页面。

可导航的页面是:

   localhost:8000/test-mdx-1/
   localhost:8000/posts/test-mdx-2/
   localhost:8000/test-mdx-3/

文件位置是:

   ./src/posts/test-mdx-1.mdx
   ./src/pages/posts/test-mdx-2.mdx
   ./src/pages/test-mdx-3.mdx

** 问题 - 创建 slug **

   slug: "posts/test-mdx-1/"
   slug: "pages/posts/test-mdx-2/"
   slug: "pages/test-mdx-3/"

** 期望的结果 **

   slug: "test-mdx-1/"
   slug: "posts/test-mdx-2/"
   slug: "test-mdx-3/"

使用插件:

      {
        resolve: 'gatsby-plugin-mdx',
        options: {
        extensions: [`.mdx`],
        gatsbyRemarkPlugins: [
          {
            resolve: 'gatsby-remark-images',
            options: { maxWidth: 600 },
          },
          {
            resolve: `gatsby-remark-responsive-iframe`,
            options: {
              wrapperStyle: `margin-bottom: 1.0725rem`,
            },
          },
        ],
      },
    },
    // ** as per tutorial
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: `pages`,
        path: `${__dirname}/src/pages`,
      },
    },
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: `posts`,
        path: `${__dirname}/src/posts`,
      },
    },
    // ** Create mdx pages outside of ./src/pages directory
    {
      resolve: 'gatsby-plugin-page-creator',
      options: {
        path: `${__dirname}/src/posts`,
      },
    },

【问题讨论】:

    标签: gatsby gatsby-plugin gatsby-plugin-mdx


    【解决方案1】:

    已排序 - 只需要 1 个 gatsby-source-filesystem, 将所有 .mdx 文件移至 ./src/posts

    gatsby-config.js

    ...
        {
          resolve: 'gatsby-plugin-mdx',
          options: {
            extensions: [`.mdx`],
            gatsbyRemarkPlugins: [
              {
                resolve: 'gatsby-remark-images',
                options: { maxWidth: 600 },
              },
              {
                resolve: `gatsby-remark-responsive-iframe`,
                options: {
                  wrapperStyle: `margin-bottom: 1.0725rem`,
                },
              },
            ],
          },
        },
        {
          resolve: 'gatsby-source-filesystem',
          options: {
            name: `posts`,
            path: `${__dirname}/src/posts`,
          },
        },
        // ** Create mdx pages outside of ./src/pages directory
        {
          resolve: 'gatsby-plugin-page-creator',
          options: {
            path: `${__dirname}/src/posts`,
          },
        },
    ...
    

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多