From d7013dd17ccceaa67b3d430018278232672e9e95 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 14 Sep 2019 13:14:47 +0300 Subject: [PATCH] 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. --- config.js | 5 +++++ gatsby-node.js | 33 +++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/config.js b/config.js index d41211a32..9688bd476 100644 --- a/config.js +++ b/config.js @@ -21,4 +21,9 @@ module.exports = { moduleName: `_30s`, rollupInputFile: `imports.temp.js`, testModuleFile: `test/_30s.js`, + // Requirable JSONs + requirables: [ + `snippets.json`, + `archivedSnippets.json` + ] }; diff --git a/gatsby-node.js b/gatsby-node.js index 7822edc05..3b9919f37 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -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: {