【问题标题】:Map over graphql output of list from a markdown file从markdown文件映射到列表的graphql输出
【发布时间】:2019-08-26 08:17:53
【问题描述】:

我正在使用 Gatsby.js 并使用 GraphQL 从 markdown 文件中检索数据。 markdown 文件的内容结构为:

---
title: abc
---

- heading A
 - sub-heading A
 - sub-heading B
- heading B
 - sub-heading A
 - sub-heading B
 - sub-heading C
...

使用插件gatsby-transformer-remark,我得到的graphql输出是这样的:

{
  "data": {
    "allMarkdownRemark": {
      "edges": [
        {
          "node": {
            "internal": {
              "content": "\n- heading A\n  - sub-heading An  - sub-heading B\n- heading B ....

是否可以获取一个数组甚至是降价列表的一个对象,以便我可以映射(循环)我的 javascript 代码中的值?

谢谢

【问题讨论】:

    标签: graphql markdown gatsby remarkjs


    【解决方案1】:

    您正在构建目录吗? Gatsby 的备注插件自动generate a table of content for you,也有几个选项可以传入修改。

    从文档中复制:

    {
      allMarkdownRemark {
        edges {
          node {
            html
            tableOfContents(
              pathToSlugField: "frontmatter.path"
              heading: "only show toc from this heading onwards"
              maxDepth: 2
            )
            frontmatter {
              # Assumes you're using path in your frontmatter.
              path
            }
          }
        }
      }
    }
    

    如果您正在制作自定义目录,那么在 frontmatter 中进行会更容易。如果这不可能,我知道的最后一个选项是编写一个自定义解析数据的备注插件,然后将其附加到 graphql 节点

    【讨论】:

    • 感谢您再次帮助我,Derek。由于某些我现在不知道的原因,我得到了一个空的 tableOfContents,即使有'fields.slug' 参数值。在探索为什么会这样的同时,我将使用您的第二个建议,即让 graphql 从每个帖子的前端提取标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2021-05-18
    • 2021-06-23
    • 2021-05-05
    • 2018-09-10
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多