首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在Gatsby中修改MDX的前置内容?

有没有办法在Gatsby中修改MDX的前置内容?
EN

Stack Overflow用户
提问于 2020-05-04 06:36:02
回答 1查看 85关注 0票数 1

我正在和Gatsby一起使用gatsby-plugin-mdx。我已经在我的frontmatter中添加了一个draft字段,当NODE_ENV"production"时,我想重写它的值,使其始终为false。请注意,gatsby-plugin-draft似乎不修改MDX AST,并且与gatsby-plugin-mdx不兼容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-04 17:46:40

你可以在onCreateNode方法中做到这一点。

YOu可以执行以下操作:

代码语言:javascript
复制
// onCreateNode.js
const { createFilePath } = require('gatsby-source-filesystem')

module.exports = ({ node, getNode, actions }) => {
  const { createNodeField } = actions

  if (node.internal.type === 'Mdx') {
    const slug = createFilePath({ node, getNode, basePath: 'pages' })
    const isProduction = ... // TODO: implement

    createNodeField({
      node,
      name: 'draft',
      value: isProduction?  false :  node.frontmatter['draft'],
    })
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61582537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档