Ron Liu Blog

  • [gatsbyjs] How to do tags in gatsby

Check this gatsby official site

  • In the frontmatter, create a new item called tags, then put it as a string array like below:

    ---
    tags: ["gatsbyjs", "react"]
    ---
  • Group tags

    query() {
      allMarkdownRemark {
        group(field: frontmatter___tags) {
          tags: fieldValue
          totalCount
        }
      }
    }
  • Make a tag page

    query($tags: String) {
      allMarkdownRemark(
        limit: 2000
        sort: { frontmatter: { date: DESC } }
        filter: { frontmatter: { tags: { in: [$tag] } } }
      ) {
        totalCount
        edges {
          node {
            fields {
              slug
            }
            frontmatter {
              title
            }
          }
        }
      }
    }