You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gatsby无法查询Markdown文件frontmatter字段问题求助

问题根因
  • Frontmatter语法不符合YAML规范

    标准Markdown frontmatter必须被上下两组---完整包裹,且YAML语法要求键名与取值之间的冒号后必须添加半角空格。你当前的Markdown文件缺失头部的---标识符,且三个字段的冒号后均无空格,导致解析器无法识别frontmatter字段。

  • 插件加载顺序错误

    gatsby-transformer-remark依赖gatsby-source-filesystem读取的Markdown源文件,必须将文件读取插件配置放在转换插件之前,否则转换器无法获取到待解析的文件内容。

  • 缓存未清理

    Gatsby会缓存已解析的节点数据,修改配置或文件后如果直接重启服务,旧缓存会导致新配置不生效。

  • 存放路径不匹配

    你配置的文件读取路径只有src/notes/和src/projects/,如果Markdown文件没有存放在这两个目录下,会无法被插件扫描到。

解决步骤
  1. 修正Markdown文件的frontmatter格式,补全头部的---,并在每个字段的冒号后添加半角空格:
---
title: Place Sharing Website
stack: React.js & Node.js
slug: place-sharing
---

### You can share the places that you visited
  1. 调整gatsby.config.js的插件加载顺序,将gatsby-transformer-remark移到所有gatsby-source-filesystem配置的后方:
module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `notes`,
        path: `${__dirname}/src/notes/`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `projects`,
        path: `${__dirname}/src/projects/`,
      },
    },
    "gatsby-transformer-remark",
  ],
  siteMetadata: {
    title: "My Homepage",
    description: "This is where I write my thoughts.",
  },
}
  1. 确认修改后的Markdown文件存放在src/notes/或src/projects/目录下。
  2. 执行以下命令清理Gatsby缓存并重启开发服务:
    gatsby clean && gatsby develop
  3. 服务启动后访问GraphiQL控制台重新执行查询语句,即可正常获取到frontmatter的三个字段。

内容的提问来源于stack exchange,提问作者Nyi Nyi Hmue Aung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 09:48:03