Gatsby Headaches: Working With Media (Part 2)<\/h1>\nJuan Diego Rodr\u00edguez<\/address>\n 2023-10-16T13:00:00+00:00
\n 2024-08-30T10:05:08+00:00
\n <\/header>\n
Gatsby is a true Jamstack framework. It works with React-powered components that consume APIs before optimizing and bundling everything to serve as static files with bits of reactivity. That includes media files, like images, video, and audio.<\/p>\n
The problem is that there\u2019s no \u201cone\u201d way to handle media in a Gatsby project. We have plugins for everything, from making queries off your local filesystem and compressing files to inlining SVGs and serving images in the responsive image format.<\/p>\n
Which plugins should be used for certain types of media? How about certain use cases for certain types of media? That\u2019s where you might encounter headaches because there are many plugins — some official and some not — that are capable of handling one or more use cases — some outdated and some not.<\/p>\n
That is what this brief two-part series is about. In Part 1<\/a>, we discussed various strategies and techniques for handling images, video, and audio in a Gatsby project.<\/p>\nThis time, in Part 2, we are covering a different type of media we commonly encounter: documents<\/strong>. Specifically, we will tackle considerations for Gatsby projects that make use of Markdown and PDF files. And before wrapping up, we will also demonstrate an approach for using 3D models.<\/p>\nSolving Markdown Headaches In Gatsby<\/h2>\n
In Gatsby, Markdown files are commonly used to programmatically create pages, such as blog posts. You can write content in Markdown, parse it into your GraphQL data layer, source it into your components, and then bundle it as HTML static files during the build process.<\/p>\n
Let\u2019s learn how to load, query, and handle the Markdown for an existing page in Gatsby.<\/p>\n
Loading And Querying Markdown From GraphQL<\/h3>\n
The first step on your Gatsby project is to load the project\u2019s Markdown files to the GraphQL data layer. We can do this using the gatsby-source-filesystem<\/code> plugin we used to query the local filesystem for image files in Part 1<\/a> of this series.<\/p>\nnpm i gatsby-source-filesystem\n<\/code><\/pre>\nIn gatsby-config.js<\/code>, we declare the folder where Markdown files will be saved in the project:<\/p>\nmodule.exports = {\n plugins: [\n {\n resolve: `gatsby-source-filesystem`,\n options: {\n name: `assets`,\n path: `${ __dirname }\/src\/assets`,\n },\n },\n ],\n};\n<\/code><\/pre>\nLet\u2019s say that we have the following Markdown file located in the project\u2019s .\/src\/assets<\/code> directory:<\/p>\n\n---\ntitle: sample-markdown-file\ndate: 2023-07-29\n---\n\n# Sample Markdown File\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consectetur imperdiet urna, vitae pellentesque mauris sollicitudin at. Sed id semper ex, ac vestibulum nunc. Etiam ,\n\n![A beautiful forest!](\/forest.jpg \"Forest trail\")\n\n```bash\nlorem ipsum dolor sit\n```\n\n## Subsection\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consectetur imperdiet urna, vitae pellentesque mauris sollicitudin at. Sed id semper ex, ac vestibulum nunc. Etiam efficitur, nunc nec placerat dignissim, ipsum ante ultrices ante, sed luctus nisl felis eget ligula. Proin sed quam auctor, posuere enim eu, vulputate felis. Sed egestas, tortor\n<\/code><\/pre>\n<\/div>\nThis example consists of two main sections: the frontmatter<\/strong> and body<\/strong>. It is a common structure for Markdown files.<\/p>\n\n- Frontmatter<\/strong>
\nEnclosed in triple dashes (---<\/code>), this is an optional section at the beginning of a Markdown file that contains metadata and configuration settings for the document. In our example, the frontmatter contains information about the page\u2019s title<\/code> and date<\/code>, which Gatsby can use as GraphQL arguments.<\/li>\n- Body<\/strong>
\nThis is the content that makes up the page\u2019s main body content.<\/li>\n<\/ul>\n
\n 2024-08-30T10:05:08+00:00
\n <\/header>\n
This time, in Part 2, we are covering a different type of media we commonly encounter: documents<\/strong>. Specifically, we will tackle considerations for Gatsby projects that make use of Markdown and PDF files. And before wrapping up, we will also demonstrate an approach for using 3D models.<\/p>\n In Gatsby, Markdown files are commonly used to programmatically create pages, such as blog posts. You can write content in Markdown, parse it into your GraphQL data layer, source it into your components, and then bundle it as HTML static files during the build process.<\/p>\n Let\u2019s learn how to load, query, and handle the Markdown for an existing page in Gatsby.<\/p>\n The first step on your Gatsby project is to load the project\u2019s Markdown files to the GraphQL data layer. We can do this using the In Let\u2019s say that we have the following Markdown file located in the project\u2019s This example consists of two main sections: the frontmatter<\/strong> and body<\/strong>. It is a common structure for Markdown files.<\/p>\nSolving Markdown Headaches In Gatsby<\/h2>\n
Loading And Querying Markdown From GraphQL<\/h3>\n
gatsby-source-filesystem<\/code> plugin we used to query the local filesystem for image files in Part 1<\/a> of this series.<\/p>\n
npm i gatsby-source-filesystem\n<\/code><\/pre>\n
gatsby-config.js<\/code>, we declare the folder where Markdown files will be saved in the project:<\/p>\n
module.exports = {\n plugins: [\n {\n resolve: `gatsby-source-filesystem`,\n options: {\n name: `assets`,\n path: `${ __dirname }\/src\/assets`,\n },\n },\n ],\n};\n<\/code><\/pre>\n
.\/src\/assets<\/code> directory:<\/p>\n
---\ntitle: sample-markdown-file\ndate: 2023-07-29\n---\n\n# Sample Markdown File\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consectetur imperdiet urna, vitae pellentesque mauris sollicitudin at. Sed id semper ex, ac vestibulum nunc. Etiam ,\n\n![A beautiful forest!](\/forest.jpg \"Forest trail\")\n\n```bash\nlorem ipsum dolor sit\n```\n\n## Subsection\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed consectetur imperdiet urna, vitae pellentesque mauris sollicitudin at. Sed id semper ex, ac vestibulum nunc. Etiam efficitur, nunc nec placerat dignissim, ipsum ante ultrices ante, sed luctus nisl felis eget ligula. Proin sed quam auctor, posuere enim eu, vulputate felis. Sed egestas, tortor\n<\/code><\/pre>\n<\/div>\n
\n
\nEnclosed in triple dashes (---<\/code>), this is an optional section at the beginning of a Markdown file that contains metadata and configuration settings for the document. In our example, the frontmatter contains information about the page\u2019s
title<\/code> and
date<\/code>, which Gatsby can use as GraphQL arguments.<\/li>\n
\nThis is the content that makes up the page\u2019s main body content.<\/li>\n<\/ul>\n