Skip to content

Commit

Permalink
Add built in slug for gatsby-plugin-mdx (#25767)
Browse files Browse the repository at this point in the history
* initial slug query

* initial commit for slug, behavior not ideal

* initial commit, not ideal behavior

* undo gatsby-dev in packagejson

* should be nullable

* use relative path from context node instead

* use relative path from context node instead

* with relative path we don't need to slugify slashes

* add whitespace

* better naming

* add another test case

* use resolvable extensions list

* add additional tests

* fix typo in test

* use path for parsing instead

* can be const

* fix test typos

* add minor comment

* make some minor changes to remove try/catch

Co-authored-by: Laurie Barth <laurie@LauriesrkLaptop.fios-router.home>
  • Loading branch information
LB and Laurie Barth committed Jul 16, 2020
1 parent e95b876 commit c82569f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 0 deletions.
20 changes: 20 additions & 0 deletions e2e-tests/mdx/cypress/integration/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,24 @@ describe(`Pages`, () => {
cy.visit(`/`).waitForRouteChange()
cy.getTestElement(`external`).contains(`Now an external import`)
})

it (`generates slug for mdx in pages dir`, () => {
cy.visit(`/list`).waitForRouteChange()
cy.getTestElement(`mdx-slug`).contains(`another`)
})

it (`generates complex slug with md extension`, () => {
cy.visit(`/list`).waitForRouteChange()
cy.getTestElement(`md-slug`).contains(`my-blog`)
})

it (`generates a slug with an index file`, () => {
cy.visit(`/list`).waitForRouteChange()
cy.getTestElement(`complex-slug`).contains(`about`)
})

it (`generates a slug with a slash`, () => {
cy.visit(`/list`).waitForRouteChange()
cy.getTestElement(`embed-slug`).contains(`about/embedded`)
})
})
8 changes: 8 additions & 0 deletions e2e-tests/mdx/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ module.exports = {
path: `${__dirname}/src/pages/`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/posts`,
},
},
{resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.mdx`, `.md`],
defaultLayouts: {
default: require.resolve("./src/components/layout.js"),
},
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/mdx/src/pages/another.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is to add another source page for slugification.
46 changes: 46 additions & 0 deletions e2e-tests/mdx/src/pages/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react'
import { graphql } from 'gatsby'

const ListPage = ({ data }) => {
const anotherPage = data.another.nodes[0]
const blogPage = data.blog.nodes[0]
const aboutPage = data.complex.nodes[0]
const embedPage = data.embed.nodes[0]


return (
<div>
<div data-testid="mdx-slug">{anotherPage.slug}</div>
<div data-testid="md-slug">{blogPage.slug}</div>
<div data-testid="complex-slug">{aboutPage.slug}</div>
<div data-testid="embed-slug">{embedPage.slug}</div>

</div>
)
}

export const query = graphql`
{
another: allMdx(filter: {slug: {eq: "another"}}) {
nodes {
slug
}
}
blog: allMdx(filter: {slug: {eq: "my-blog"}}) {
nodes {
slug
}
}
complex: allMdx(filter: {slug: {eq: "about/"}}) {
nodes {
slug
}
}
embed: allMdx(filter: {slug: {eq: "about/embedded"}}) {
nodes {
slug
}
}
}
`
export default ListPage
1 change: 1 addition & 0 deletions e2e-tests/mdx/src/posts/about/embedded.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a page that should include a slash slug
1 change: 1 addition & 0 deletions e2e-tests/mdx/src/posts/about/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This should be an about page we can access with a nested slug.
1 change: 1 addition & 0 deletions e2e-tests/mdx/src/posts/my-blog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Let's see if we can generate a slug for this too
28 changes: 28 additions & 0 deletions packages/gatsby-plugin-mdx/gatsby/source-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const remove = require(`unist-util-remove`)
const toString = require(`mdast-util-to-string`)
const generateTOC = require(`mdast-util-toc`)
const prune = require(`underscore.string/prune`)
const slugify = require(`slugify`)
const path = require(`path`)

const debug = require(`debug`)(`gatsby-plugin-mdx:extend-node-type`)
const getTableOfContents = require(`../utils/get-table-of-content`)
Expand Down Expand Up @@ -133,6 +135,32 @@ module.exports = (
rawBody: { type: `String!` },
fileAbsolutePath: { type: `String!` },
frontmatter: { type: `MdxFrontmatter` },
slug: {
type: `String`,
async resolve(mdxNode, args, context) {
const nodeWithContext = context.nodeModel.findRootNodeAncestor(
mdxNode,
node => node.internal && node.internal.type === `File`
)

if (!nodeWithContext) {
return null
}
const fileRelativePath = nodeWithContext.relativePath

const parsedPath = path.parse(fileRelativePath)

let relevantPath
if (parsedPath.name === `index`) {
relevantPath = fileRelativePath.replace(parsedPath.base, ``)
} else {
relevantPath = fileRelativePath.replace(parsedPath.ext, ``)
}
return slugify(relevantPath, {
remove: /[^\w\s$*_+~.()'"!\-:@/]/g, // this is the set of allowable characters
})
},
},
body: {
type: `String!`,
async resolve(mdxNode) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"remark": "^10.0.1",
"remark-retext": "^3.1.3",
"retext-english": "^3.0.4",
"slugify": "^1.4.4",
"static-site-generator-webpack-plugin": "^3.4.2",
"style-to-object": "^0.3.0",
"underscore.string": "^3.3.5",
Expand Down

1 comment on commit c82569f

@elrumordelaluz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi all,
Probably this PR isn't the source of the problem I found, but since is one of the last commits related on the last releases I am posting here in case…

Upgrading @mdx-js/mdx and @mdx-js/react from 1.6.10 into 1.6.11 and gatsby-plugin-mdx from 1.2.26 into 1.2.27, since I am using createMDXNode from gatsby-plugin-mdx/utils/create-mdx-node I started to have a loop of errors like

  TypeError: Cannot read property 'gatsbyRemarkPlugins' of undefined

Downgrading to the previous (patch) version, everything works well again. Maybe isn't safe to use create-mdx-node? In that case, is there a better way to create MDS nodes from an internal node?

I can open a new issue In any case.
Thanks in advance,

Please sign in to comment.