Update createPages to use new schemas
Breaks archive snippet pages, needs some investigation
This commit is contained in:
201
gatsby-node.js
201
gatsby-node.js
@ -8,7 +8,7 @@ const requirables = [];
|
|||||||
|
|
||||||
config.requirables.forEach(fileName => {
|
config.requirables.forEach(fileName => {
|
||||||
requirables.push(require(`./snippet_data/${fileName}`));
|
requirables.push(require(`./snippet_data/${fileName}`));
|
||||||
})
|
});
|
||||||
|
|
||||||
const toKebabCase = str =>
|
const toKebabCase = str =>
|
||||||
str &&
|
str &&
|
||||||
@ -16,100 +16,9 @@ const toKebabCase = str =>
|
|||||||
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||||
.map(x => x.toLowerCase())
|
.map(x => x.toLowerCase())
|
||||||
.join('-');
|
.join('-');
|
||||||
/*
|
|
||||||
exports.createPages = ({ graphql, actions }) => {
|
|
||||||
const { createPage } = actions;
|
|
||||||
|
|
||||||
const snippetPage = path.resolve(`./src/docs/templates/SnippetPage.js`);
|
|
||||||
const tagPage = path.resolve(`./src/docs/templates/TagPage.js`);
|
|
||||||
return graphql(
|
|
||||||
`
|
|
||||||
{
|
|
||||||
allMarkdownRemark(
|
|
||||||
sort: { fields: [frontmatter___title], order: ASC }
|
|
||||||
limit: 1000
|
|
||||||
) {
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
fields {
|
|
||||||
slug
|
|
||||||
}
|
|
||||||
frontmatter {
|
|
||||||
tags
|
|
||||||
}
|
|
||||||
fileAbsolutePath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`,
|
|
||||||
).then(result => {
|
|
||||||
if (result.errors) {
|
|
||||||
throw result.errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create individual snippet pages.
|
|
||||||
const snippets = result.data.allMarkdownRemark.edges;
|
|
||||||
|
|
||||||
snippets.forEach((post, index) => {
|
|
||||||
if(post.node.fileAbsolutePath.indexOf('README') !== -1)
|
|
||||||
return;
|
|
||||||
if (post.node.fileAbsolutePath.indexOf(config.snippetArchivePath) === -1)
|
|
||||||
createPage({
|
|
||||||
path: `/snippet${post.node.fields.slug}`,
|
|
||||||
component: snippetPage,
|
|
||||||
context: {
|
|
||||||
slug: post.node.fields.slug,
|
|
||||||
scope: `./snippets`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
else
|
|
||||||
createPage({
|
|
||||||
path: `/archive${post.node.fields.slug}`,
|
|
||||||
component: snippetPage,
|
|
||||||
context: {
|
|
||||||
slug: post.node.fields.slug,
|
|
||||||
scope: `./snippets_archive`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create tag pages.
|
|
||||||
const tags = snippets.reduce((acc, post) => {
|
|
||||||
if (!post.node.frontmatter || !post.node.frontmatter.tags) return acc;
|
|
||||||
const primaryTag = post.node.frontmatter.tags.split(',')[0];
|
|
||||||
if (!acc.includes(primaryTag)) acc.push(primaryTag);
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
tags.forEach(tag => {
|
|
||||||
const tagPath = `/tag/${toKebabCase(tag)}/`;
|
|
||||||
const tagRegex = `/^\\s*${tag}/`;
|
|
||||||
createPage({
|
|
||||||
path: tagPath,
|
|
||||||
component: tagPage,
|
|
||||||
context: {
|
|
||||||
tag,
|
|
||||||
tagRegex,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
createPage({
|
|
||||||
path: `/beginner`,
|
|
||||||
component: tagPage,
|
|
||||||
context: {
|
|
||||||
tag: `beginner snippets`,
|
|
||||||
tagRegex: `/beginner/`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
exports.onCreateNode = ({ node, actions, getNode }) => {
|
exports.onCreateNode = ({ node, actions, getNode }) => {
|
||||||
const { createNodeField, createNode } = actions;
|
const { createNodeField } = actions;
|
||||||
|
|
||||||
if (node.internal.type === `MarkdownRemark`) {
|
if (node.internal.type === `MarkdownRemark`) {
|
||||||
const value = createFilePath({ node, getNode });
|
const value = createFilePath({ node, getNode });
|
||||||
@ -119,7 +28,6 @@ exports.onCreateNode = ({ node, actions, getNode }) => {
|
|||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByType }) => {
|
exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByType }) => {
|
||||||
@ -134,6 +42,7 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByT
|
|||||||
slug: String
|
slug: String
|
||||||
path: String
|
path: String
|
||||||
text: TextData
|
text: TextData
|
||||||
|
archived: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type HtmlData @infer {
|
type HtmlData @infer {
|
||||||
@ -164,12 +73,13 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByT
|
|||||||
|
|
||||||
const snippetNodes = requirables
|
const snippetNodes = requirables
|
||||||
.reduce((acc, sArr) => {
|
.reduce((acc, sArr) => {
|
||||||
|
const archivedScope = sArr.meta.scope.indexOf('archive') !== -1;
|
||||||
return ({
|
return ({
|
||||||
...acc,
|
...acc,
|
||||||
...sArr.data.reduce((snippets, snippet) => {
|
...sArr.data.reduce((snippets, snippet) => {
|
||||||
return ({
|
return ({
|
||||||
...snippets,
|
...snippets,
|
||||||
[snippet.id]: snippet
|
[snippet.id]: { ...snippet, archived: archivedScope}
|
||||||
});
|
});
|
||||||
}, {})
|
}, {})
|
||||||
});
|
});
|
||||||
@ -193,7 +103,8 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByT
|
|||||||
text: {
|
text: {
|
||||||
full: sNode.attributes.text,
|
full: sNode.attributes.text,
|
||||||
short: sNode.attributes.text.slice(0, sNode.attributes.text.indexOf('\n\n'))
|
short: sNode.attributes.text.slice(0, sNode.attributes.text.indexOf('\n\n'))
|
||||||
}
|
},
|
||||||
|
archived: sNode.archived
|
||||||
};
|
};
|
||||||
createNode({
|
createNode({
|
||||||
id: createNodeId(`snippet-${sNode.meta.hash}`),
|
id: createNodeId(`snippet-${sNode.meta.hash}`),
|
||||||
@ -228,3 +139,101 @@ exports.createResolvers = ({ createResolvers }) => createResolvers({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
exports.createPages = ({ graphql, actions }) => {
|
||||||
|
const { createPage } = actions;
|
||||||
|
|
||||||
|
const snippetPage = path.resolve(`./src/docs/templates/SnippetPage.js`);
|
||||||
|
const tagPage = path.resolve(`./src/docs/templates/TagPage.js`);
|
||||||
|
|
||||||
|
return graphql(
|
||||||
|
`
|
||||||
|
{
|
||||||
|
allSnippet(sort: {fields: id}) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
slug
|
||||||
|
tags {
|
||||||
|
all
|
||||||
|
primary
|
||||||
|
}
|
||||||
|
text {
|
||||||
|
full
|
||||||
|
short
|
||||||
|
}
|
||||||
|
title
|
||||||
|
html {
|
||||||
|
code
|
||||||
|
example
|
||||||
|
full
|
||||||
|
text
|
||||||
|
}
|
||||||
|
code {
|
||||||
|
src
|
||||||
|
example
|
||||||
|
}
|
||||||
|
archived
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
).then(result => {
|
||||||
|
if (result.errors) {
|
||||||
|
throw result.errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create individual snippet pages.
|
||||||
|
const snippets = result.data.allSnippet.edges;
|
||||||
|
|
||||||
|
snippets.forEach(snippet => {
|
||||||
|
if(!snippet.archived) {
|
||||||
|
createPage({
|
||||||
|
path: `/snippet${snippet.node.slug}`,
|
||||||
|
component: snippetPage,
|
||||||
|
context: {
|
||||||
|
slug: snippet.node.slug,
|
||||||
|
scope: `./snippets`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
createPage({
|
||||||
|
path: `/archive${snippet.node.slug}`,
|
||||||
|
component: snippetPage,
|
||||||
|
context: {
|
||||||
|
slug: snippet.node.slug,
|
||||||
|
scope: `./snippets_archive`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create tag pages.
|
||||||
|
const tags = [...new Set(snippets.map(snippet => snippet.tags.primary))].sort((a,b) => a.localeCompare(b));
|
||||||
|
|
||||||
|
tags.forEach(tag => {
|
||||||
|
const tagPath = `/tag/${toKebabCase(tag)}/`;
|
||||||
|
const tagRegex = `/^\\s*${tag}/`;
|
||||||
|
createPage({
|
||||||
|
path: tagPath,
|
||||||
|
component: tagPage,
|
||||||
|
context: {
|
||||||
|
tag,
|
||||||
|
tagRegex,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
createPage({
|
||||||
|
path: `/beginner`,
|
||||||
|
component: tagPage,
|
||||||
|
context: {
|
||||||
|
tag: `beginner snippets`,
|
||||||
|
tagRegex: `/beginner/`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user