Gatsby + Markdown: How to get data from a specific markdown file into a single page?

Florent Despinoy

I'm new to Gatsby, and making my best to learn it (along with React, in which I have no prior knowledge either). I'd like to create a single page getting data from one or several markdown files.

For now I'm testing it out with just Gatsby, in order to later reproduce that technique with Netlify CMS markdown files (and be able to update the page texts with Netlify CMS admin panel). So far, I've managed to add markdown pages to Gatsby, thanks to this tutorial. But this method only creates dynamic pages, which is far more complex than what I need.

Is there a simple way to import one specific markdown file, let's say src/markdowns/hero-texts.md, in (let's also say) pages/index.js, and then call data with their frontmatter tags, in the cleanest way as possible?

I've tried countless researches on Google just to find which plugin or coding term would handle that, without success. I totally get some of the explanations above may be full of technical misunderstandings, sorry for that...

ksav

You have a markdown file called hero-texts.md and you want to be able to query its frontmatter content.

Install the plugins gatsby-transformer-remark and gatsby-source-filesystem and setup the gatsby-source-filesystem options to find your markdown files.


// gatsby-config.js

module.exports = {
    plugins: [
        {
        resolve: `gatsby-source-filesystem`,
            options: {
              name: `markdown`,
              path: `${__dirname}/src/markdowns/`
            }
        },
        `gatsby-transformer-remark`
    ]
}

You could make a graphql page query like this inside index.js (then the result of the query is automatically added to your index component under props.data)

// src/pages/index.js

import React from "react"
import { graphql } from "gatsby"

const IndexPage = ({data}) => {
  return (
  <>
    <p>{data.markdownRemark.frontmatter.author}</p>
    <p>{data.markdownRemark.frontmatter.date}</p>
    <p>{data.markdownRemark.frontmatter.title}</p>
  </>
)}

export default IndexPage

export const pageQuery = graphql`
  query IndexPageQuery {
    markdownRemark(fileAbsolutePath: { regex: "/hero-texts.md/" }) {
      frontmatter {
        author
        date
        title
      }
    }
  }
`

It will perform the graphql query at build time, and add the result of the query to the data prop of the IndexPage page component.

So in effect, pulling in all the frontmatter fields from a markdown file that looked like this.

// src/markdowns/hero-texts.md

---
title: "Gatsby + Markdown: How to simply get data from a specific markdown in a single page?"
author: Florent Despinoy
date: 2019-08-06
---

# This is my markdown post

The content of this markdown file would not be queried by pageQuery (only the frontmatter would)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get a list of image URLs from a Markdown file in Python?

Render markdown from a yaml multiline string in a Jekyll data file

How to build single page site from Markdown?

How to start page numbering in R Markdown from the second page?

How can another file be Included in a Hugo/Markdown page?

Middleman: referencing URL stored in a data file from markdown

Rendering multiple R scripts as markdown within single markdown file

How to render two files from a single markdown

How to get specific data from a CSV file

Print markdown file from GitHub

How to read markdown code from a file from an R markdown document

How to create a page in Gatsby from JSON file?

How can I create components from markdown in Gatsby like a page?

How can i get directory name of markdown file in gatsby?

How to query markdown files of a specific language in Gatsby?

Showing images in markdown for gatsby

Gatsby Unable to process image from markdown files

How to use createResovers to convert a Markdown FIELD (not a file) to HTML in Gatsby

How to pass data between Gatsby Markdown pages in a React JS app

How To Fetch Data From Markdown File and Display it as a HTML input Form

How do you apply styling to array from a markdown file for Gatsby built website?

How to write a JSON file based on data from multiple markdown files

Regex get section in markdown file

Gatsby - Get multiple images from an array in a Markdown file

Gatsby Markdown Inline Images

How to set page numbering from the third page in R markdown?

How to use gatsby-remark-images when the images aren't in the same folder of the markdown file

How to get the text from an AttributedString initialized with markdown?

How to rotate a single page to landscape in R Markdown pdf output

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive