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:
Angelos Chalaris
2019-09-14 13:14:47 +03:00
parent 59d527edb0
commit d7013dd17c
2 changed files with 32 additions and 6 deletions

View File

@ -21,4 +21,9 @@ module.exports = {
moduleName: `_30s`,
rollupInputFile: `imports.temp.js`,
testModuleFile: `test/_30s.js`,
// Requirable JSONs
requirables: [
`snippets.json`,
`archivedSnippets.json`
]
};

View File

@ -2,6 +2,12 @@ const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
const config = require('./config');
const requirables = [];
config.requirables.forEach(fileName => {
requirables.push(require(`./snippet_data/${fileName}`));
})
const toKebabCase = str =>
str &&
str
@ -128,16 +134,31 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest, getNodesByT
createTypes(typeDefs);
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 = {
html: mnode.html,
tags: (mnode.frontmatter.tags || '').split(','),
title: mnode.frontmatter.title,
id,
html: mNode.html,
tags: sNode.attributes.tags,
title: mNode.frontmatter.title,
code: sNode.attributes.codeBlocks.es6
};
createNode({
id: createNodeId(`snippet-${mnode.id}`),
id: createNodeId(`snippet-${sNode.meta.hash}`),
parent: null,
children: [],
internal: {