Setup snippet schema initial load logic
Use dynamic imports to load all the JSON files specified in config, read data from both sources, output the proper data in the new Snippet type nodes.
This commit is contained in:
@ -21,4 +21,9 @@ module.exports = {
|
|||||||
moduleName: `_30s`,
|
moduleName: `_30s`,
|
||||||
rollupInputFile: `imports.temp.js`,
|
rollupInputFile: `imports.temp.js`,
|
||||||
testModuleFile: `test/_30s.js`,
|
testModuleFile: `test/_30s.js`,
|
||||||
|
// Requirable JSONs
|
||||||
|
requirables: [
|
||||||
|
`snippets.json`,
|
||||||
|
`archivedSnippets.json`
|
||||||
|
]
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,6 +2,12 @@ const path = require(`path`);
|
|||||||
const { createFilePath } = require(`gatsby-source-filesystem`);
|
const { createFilePath } = require(`gatsby-source-filesystem`);
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
|
|
||||||
|
const requirables = [];
|
||||||
|
|
||||||
|
config.requirables.forEach(fileName => {
|
||||||
|
requirables.push(require(`./snippet_data/${fileName}`));
|
||||||
|
})
|
||||||
|
|
||||||
const toKebabCase = str =>
|
const toKebabCase = str =>
|
||||||
str &&
|
str &&
|
||||||
str
|
str
|
||||||
@ -128,16 +134,31 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByT
|
|||||||
createTypes(typeDefs);
|
createTypes(typeDefs);
|
||||||
|
|
||||||
const markdownNodes = getNodesByType('MarkdownRemark');
|
const markdownNodes = getNodesByType('MarkdownRemark');
|
||||||
const snippetDataNodes = getNodesByType('SnippetDataJson');
|
|
||||||
|
|
||||||
markdownNodes.forEach(mnode => {
|
const snippetNodes = requirables
|
||||||
|
.reduce((acc, sArr) => {
|
||||||
|
return ({
|
||||||
|
...acc,
|
||||||
|
...sArr.data.reduce((snippets, snippet) => {
|
||||||
|
return ({
|
||||||
|
...snippets,
|
||||||
|
[snippet.id]: snippet
|
||||||
|
});
|
||||||
|
}, {})
|
||||||
|
});
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
Object.entries(snippetNodes).forEach(([id, sNode]) => {
|
||||||
|
let mNode = markdownNodes.find(mN => mN.frontmatter.title === id);
|
||||||
let nodeContent = {
|
let nodeContent = {
|
||||||
html: mnode.html,
|
id,
|
||||||
tags: (mnode.frontmatter.tags || '').split(','),
|
html: mNode.html,
|
||||||
title: mnode.frontmatter.title,
|
tags: sNode.attributes.tags,
|
||||||
|
title: mNode.frontmatter.title,
|
||||||
|
code: sNode.attributes.codeBlocks.es6
|
||||||
};
|
};
|
||||||
createNode({
|
createNode({
|
||||||
id: createNodeId(`snippet-${mnode.id}`),
|
id: createNodeId(`snippet-${sNode.meta.hash}`),
|
||||||
parent: null,
|
parent: null,
|
||||||
children: [],
|
children: [],
|
||||||
internal: {
|
internal: {
|
||||||
|
|||||||
Reference in New Issue
Block a user