Skip to content

Commit 4a627a4

Browse files
authored
Merge pull request layer5io#496 from Tanuj22/Tanuj22/markdown
feat: add plugin for markdown and a sample blog
2 parents a489714 + 64f35e7 commit 4a627a4

File tree

13 files changed

+1350
-373
lines changed

13 files changed

+1350
-373
lines changed

Diff for: layer5-ng/gatsby-config.js

+13
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ module.exports = {
1818
offset: -50,
1919
},
2020
},
21+
{
22+
resolve: `gatsby-plugin-mdx`,
23+
options: {
24+
extensions: [`.mdx`, `.md`],
25+
},
26+
},
2127
{
2228
resolve: `gatsby-source-filesystem`,
2329
options: {
2430
name: `images`,
2531
path: `${__dirname}/src/images`,
2632
},
2733
},
34+
{
35+
resolve: `gatsby-source-filesystem`,
36+
options: {
37+
path: `${__dirname}/src/posts`,
38+
name: `posts`,
39+
},
40+
},
2841
`gatsby-transformer-sharp`,
2942
`gatsby-plugin-sharp`,
3043
{

Diff for: layer5-ng/gatsby-node.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* See: https://www.gatsbyjs.org/docs/node-apis/
55
*/
6+
const { createFilePath } = require(`gatsby-source-filesystem`);
7+
const path = require(`path`)
68

79
// You can delete this file if you're not using it
810
// Replacing '/' would result in empty string which is invalid
@@ -19,4 +21,48 @@ exports.onCreatePage = ({ page, actions }) => {
1921
deletePage(oldPage)
2022
createPage(page)
2123
}
22-
}
24+
}
25+
26+
exports.createPages = async ({ actions, graphql }) => {
27+
const { createPage } = actions;
28+
const blogPostTemplate = path.resolve(
29+
'src/templates/blog-single.js'
30+
);
31+
const res = await graphql(`
32+
{
33+
allMdx {
34+
nodes {
35+
fields {
36+
slug
37+
}
38+
frontmatter {
39+
title
40+
}
41+
}
42+
}
43+
}
44+
`);
45+
const posts = res.data.allMdx.nodes;
46+
47+
posts.forEach(post => {
48+
createPage({
49+
path: post.fields.slug,
50+
component: blogPostTemplate,
51+
context: {
52+
slug: post.fields.slug,
53+
},
54+
})
55+
})
56+
};
57+
58+
exports.onCreateNode = ({ node, actions, getNode }) => {
59+
const { createNodeField } = actions
60+
if (node.internal.type === `Mdx`) {
61+
const value = createFilePath({ node, getNode });
62+
createNodeField({
63+
name: `slug`,
64+
node,
65+
value,
66+
})
67+
}
68+
};

0 commit comments

Comments
 (0)