24
loading...
This website collects cookies to deliver better user experience
npm install gatsby-transformer-remark
gatsby-transformer-remark
plugin supports multiple input sources (e.g., various content management systems), but the easiest option is to host markdown files locally. To do that, you will need the following plugin:npm install gatsby-source-filesystem
gatsby-remark-images
plugin has several nifty features such as:npm install gatsby-remark-images gatsby-plugin-sharp gatsby-transformer-sharp
gatsby-remark-autolink-headers
plugin automatically generates HTML IDs for each heading, allowing your readers to link to a specific section in your posts.npm install gatsby-remark-autolink-headers
npm install gatsby-plugin-catch-links
npm install gatsby-remark-copy-linked-files
npm install gatsby-transformer-remark gatsby-remark-prismjs prismjs
/content
. You can create .md
files directly in that directory, or you can create any number of subdirectories. The path you use will determine the slugs at which your content will be hosted. E.g., creating a page under /content/my/article.md
will render to the following page: https://SITE_URL/my/article
.A slug is a common term used to denote the part of a URL identifying a page.
gatsby-config.js
.module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content`,
name: `blog`,
},
},
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 730,
},
},
`gatsby-remark-autolink-headers`,
`gatsby-plugin-catch-links`,
`gatsby-remark-prismjs`,
`gatsby-remark-copy-linked-files`,
],
},
},
`gatsby-transformer-sharp`,
{
resolve: `gatsby-plugin-sharp`,
options: {
defaults: {
formats: [`auto`, `webp`],
placeholder: `blurred`,
quality: 95,
breakpoints: [750, 1080, 1366, 1920],
backgroundColor: `transparent`,
tracedSVGOptions: {},
blurredOptions: {},
jpgOptions: {},
pngOptions: {},
webpOptions: {},
avifOptions: {},
},
},
},
],
};
gatsby-node.js
file and add the following (if one does not exist, create it):const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
// generate post slug
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Get all markdown blog posts sorted by date
const result = await graphql(
`
{
allMarkdownRemark(sort: { fields: [frontmatter___date], order: ASC }) {
nodes {
id
fields {
slug
}
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild(
`There was an error loading your blog posts`,
result.errors
);
return;
}
// Create blog posts pages only if there's at least one markdown file
const posts = result.data.allMarkdownRemark.nodes;
if (posts.length == 0) {
reporter.warn(`No blog posts found`);
return;
}
// Define a template for blog post
const component = path.resolve(`./src/components/blog-post.js`);
posts.forEach((post, index) => {
const previousPostId = index === 0 ? null : posts[index - 1].id;
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id;
console.log(`Creating blog page: ${post.fields.slug}`);
createPage({
path: post.fields.slug,
component,
context: {
id: post.id,
previousPostId,
nextPostId,
},
});
});
};
./src/templates/blog-post.js
. Go ahead and create this file and populate it with a React component that will display the rendered markdown and other elements. Here's an example.