Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
72 changes: 72 additions & 0 deletions libraries/gatsby-awt-json-exporter/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
}
15 changes: 15 additions & 0 deletions libraries/gatsby-awt-json-exporter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@alexwilson/gatsby-awt-json-exporter",
"version": "1.0.0",
"description": "Export JSON files",
"main": "index.js",
"author": "Alex Wilson <[email protected]>",
"license": "private",
"private": "true",
"dependencies": {
},
"peerDependencies": {
"gatsby": ">2.0.0-alpha"
}
}

3 changes: 3 additions & 0 deletions services/personal-website/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ module.exports = {
families: ['Overpass:400,600,800']
}
}
},
{
resolve: '@alexwilson/gatsby-awt-json-exporter'
}
],
}
1 change: 1 addition & 0 deletions services/personal-website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down