-
Notifications
You must be signed in to change notification settings - Fork 100
MDX/Markdown in frontmatter #352
Comments
Having recently encountered the same issues, I did get around it by using the runtime. Support for frontmatter would be great, but if it winds up being undesirable, adding some documentation would still most likely be valuable. |
@ChristopherBiscardi I'd love to get your opinion before I start hacking anything together :) |
@mrozilla Can you make the use case a bit more concrete for me? It seems like you want to write multiple MDX files in one file? |
Here's a minimal example of such .mdx file: ---
title: This is a title that includes a [link](https://example.com)
---
This is a body that includes a [link](https://example.com) This could, of course, be accomplished by multiple MDX files and referencing, however, Netlify CMS doesn't save like this. Since we're already parsing the body of an Does this answer your question? :) |
hmm yeah, I could see that being useful. There seem to be two paths to this functionality.
I think I'm inclined towards path 1 because frontmatter is something gatsby-mdx implements for compatibility with export const frontmatter = {
title: "This is a title that includes a [link](https://example.com)"
}
# some mdx
yadda yadda An alternate implementation would be to transform frontmatter into top-level exports, but this seems like it could conflict and the processing boils down to the same thing anyway (if export includes strings, process as mdx) export const title = "This is a title that includes a [link](https://example.com)"
# some mdx
yadda yadda Another concern is that if you use MDX in the In the meantime you can accomplish this with some special processing in exports.onCreateNode = ({ node, actions, getNode, createNodeId }) => {
const { createNodeField, createNode, createParentChildLink } = actions;
if (node.internal.type === `Mdx`) {
const { frontmatter } = node;
if (frontmatter.title) {
const myNode = createNode({
id: createNodeId(`${node.id} >>> MdxCustomSetupType`),
parent: node.id,
children: [],
internal: {
type: `MdxCustomSetupType`,
contentDigest: crypto
.createHash(`md5`)
.update(node.frontmatter.title)
.digest(`hex`),
content: node.frontmatter.title,
mediaType: "text/markdown",
description: `A custom node so that MDX will process the title field`
}
});
createParentChildLink({ parent: node, child: myNode });
}
}
}; The query would look something like this (note that you may want to change the names of the variables to make it make more sense for users). {
mdx {
frontmatter {
title // would be a string
}
childMdxCustomSetupType {
body // would be the MDX version of the title
}
}
} and you'd have to use |
Closing due to the core logic related to this proposal being moved to https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-mdx |
Is your feature request related to a problem? Please describe.
I'm using this package with Netlify CMS. There are situations (e.g. with variable list types) when mdx/markdown ends up saved in the frontmatter of an
.mdx
file.Describe the solution you'd like
Since we're already parsing the body of an
.mdx
file, we could do the same thing to certain fields in the frontmatter which can then be rendered through theMDXRenderer
just like the body.Describe alternatives you've considered
At the moment, it's possible to manually parse the saved string through
@mdx/runtime
which warns about overhead and bundle sizes (sure, Gatsby is static but still would have to load the larger bundle, affecting the TTI).The trickiest part of this would probably be defining which frontmatter fields to parse, not sure about a robust-enough solution here yet—I'm using a field called
markdown
ormdx
in my userland solution.Additional context
I'd love to take a stab at a PR but wanted to check with the maintainers (for opinions and possibly guidance) and the community (for interest) first :) Cheers for a lovely package!
The text was updated successfully, but these errors were encountered: