Gatsby MDX slugs unnavigable, contain directory in name

159 views Asked by At

Slugs created contain the root containing directory, thus passing slug to <Link to={slug}> is broken. The pages are created, but the slugs to link to them unnecessarily contain the containing folder, be it posts or pages.

Navigable pages are:

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

File locations are:

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

** Problem - Created slugs **

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

** Desired Result **

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

Using plugins:

      {
        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`,
      },
    },
1

There are 1 answers

0
curtybear On

Sorted - only 1 gatsby-source-filesystem needed, moved all .mdx files to ./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`,
      },
    },
...