Create the content directorys:
Create the files:
Reporter and hooks:
exports.onPreInit = ({ reporter }) => {
reporter.info(`Hello World!`);
}
Create a page:
Create Directory : src/pages/templates
Create the hello-world component:
import * as React from "react";
export default function Page()
{
return (
<div>
This is my sick template
</div>
);
}
Render it from gatsby-node.js
const path = require(`path`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const helloWorldComponent = path.resolve("src/templates/hello-world.js");
createPage({
path: "hello-world",
component: helloWorldComponent,
});
}
Navigate to it
Create the routes:
const result = await graphql(`
query MyQuery {
allMarkdownRemark {
nodes {
parent {
... on File {
relativeDirectory
name
}
}
}
}
}
`);
result.data.allMarkdownRemark.nodes.forEach(node => {
const { relativeDirectory, name } = node.parent;
createPage({
path: `${relativeDirectory}/${name}`,
component: helloWorldComponent,
})
})
Add id to the routing query
const result = await graphql(`
query MyQuery {
allMarkdownRemark {
nodes {
id
parent {
... on File {
relativeDirectory
name
}
}
}
}
}
`);
Pass that in as context:
const { id } = node;
createPage({
path: `${relativeDirectory}/${name}`,
component: helloWorldComponent,
context : {
id
}
});
export const query = graphql`
query($id : String!)
{
markdownRemark(id: { eq : $id })
{
html
}
}
`;
extract the html from the props
export default function Page({ data }){
const { html } = data.markdownRemark;
Render that out
<div dangerouslySetInnerHTML={{ __html : html }} />
Use the same query as the routing
query MyQuery {
allMarkdownRemark {
nodes {
parent {
... on File {
relativeDirectory
name
}
}
}
}
}
Map all nodes and render them out
Render them out