From c5db9d2b69a2b729ab78ae052b83f883b610e055 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Mon, 13 Jun 2022 01:18:48 +0100 Subject: [PATCH] feat(export): Make Gatsby content available via JSON --- libraries/gatsby-awt-json-exporter/README.md | 0 .../gatsby-awt-json-exporter/gatsby-node.js | 72 +++++++++++++++++++ .../gatsby-awt-json-exporter/package.json | 15 ++++ services/personal-website/gatsby-config.js | 3 + services/personal-website/package.json | 1 + 5 files changed, 91 insertions(+) create mode 100644 libraries/gatsby-awt-json-exporter/README.md create mode 100644 libraries/gatsby-awt-json-exporter/gatsby-node.js create mode 100644 libraries/gatsby-awt-json-exporter/package.json diff --git a/libraries/gatsby-awt-json-exporter/README.md b/libraries/gatsby-awt-json-exporter/README.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/gatsby-awt-json-exporter/gatsby-node.js b/libraries/gatsby-awt-json-exporter/gatsby-node.js new file mode 100644 index 0000000000..993a24ce20 --- /dev/null +++ b/libraries/gatsby-awt-json-exporter/gatsby-node.js @@ -0,0 +1,72 @@ +const fs = require('fs') +const path = require('path') + +const indexPath = `./public/__content.json` +const collectionPath = `./public/__content/` + +exports.onPostBuild = ({graphql}) => { + graphql(` + { + content: allMarkdownRemark( + sort: { order: DESC, fields: [frontmatter___date] }, + ) { + edges { + node { + snippet: excerpt(pruneLength: 220, format: PLAIN) + html + fields { + id + slug + _legacy_slug + } + frontmatter { + title + tags + date + } + } + } + } + } + `).then(result => { + if (!fs.existsSync(collectionPath)) fs.mkdirSync(collectionPath) + + const topics = {} + const content = result.data.content.edges.map(({ node }) => { + const packet = { + id: node.fields.id, + raw: node + } + + if (node.frontmatter.tags) { + node.frontmatter.tags.forEach(tag => { + if (!(tag in topics)) { + topics[tag] = { + members: [], + total: 0 + } + } + topics[tag].members.push(packet.id) + topics[tag].total++ + }) + } + + return packet + }) + + const all = { + content, + topics, + meta: { + content: content.length, + topics: topics.length, + updated: (new Date).toISOString() + } + } + fs.writeFileSync(path.resolve(indexPath), JSON.stringify(all)) + + content.map(content => { + fs.writeFileSync(path.resolve(collectionPath, `${content.id}.json`), JSON.stringify(content)) + }) + }) +} diff --git a/libraries/gatsby-awt-json-exporter/package.json b/libraries/gatsby-awt-json-exporter/package.json new file mode 100644 index 0000000000..fc5a28db63 --- /dev/null +++ b/libraries/gatsby-awt-json-exporter/package.json @@ -0,0 +1,15 @@ +{ + "name": "@alexwilson/gatsby-awt-json-exporter", + "version": "1.0.0", + "description": "Export JSON files", + "main": "index.js", + "author": "Alex Wilson ", + "license": "private", + "private": "true", + "dependencies": { + }, + "peerDependencies": { + "gatsby": ">2.0.0-alpha" + } + } + \ No newline at end of file diff --git a/services/personal-website/gatsby-config.js b/services/personal-website/gatsby-config.js index d74e5079cb..e1ab4d6bdb 100644 --- a/services/personal-website/gatsby-config.js +++ b/services/personal-website/gatsby-config.js @@ -225,6 +225,9 @@ module.exports = { families: ['Overpass:400,600,800'] } } + }, + { + resolve: '@alexwilson/gatsby-awt-json-exporter' } ], } diff --git a/services/personal-website/package.json b/services/personal-website/package.json index 36f6156bc6..e544c51cb1 100644 --- a/services/personal-website/package.json +++ b/services/personal-website/package.json @@ -25,6 +25,7 @@ "homepage": "https://github.com/alexwilson/personal-website#readme", "dependencies": { "@alexwilson/gatsby-remark-rewrite-images": "^1.0.0", + "@alexwilson/gatsby-awt-json-exporter": "^1.0.0", "@alexwilson/legacy-components": "*", "@loadable/component": "^5.15.2", "date-fns": "^2.11.1",