Skip to content

Commit

Permalink
fix(gatsby-theme-docz): add specific nodes for entries
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Feb 27, 2019
1 parent aca1bee commit 26f7f90
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
47 changes: 27 additions & 20 deletions core/gatsby-theme-docz/src/node/createPages.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
const path = require('path')
const { getDoczConfig } = require('../utils/parseConfig')

const parseDatabase = data => {
try {
return JSON.parse(data.doczDb.db)
} catch (err) {
return null
}
}

const mountRoute = (base = '/', route) => {
return `${base === '/' ? '' : base}${route}`
}

const ENTRIES_QUERY = `
{
allDoczEntries{
edges{
node{
id
filepath
route
slug
name
menu
order
headings {
slug
depth
value
}
}
}
}
}
`

module.exports = ({ graphql, actions }, opts) => {
const { paths, ...config } = getDoczConfig(opts)

return graphql(`
{
doczDb {
id
db
}
}
`).then(({ data, errors }) => {
const db = parseDatabase(data)
return graphql(ENTRIES_QUERY).then(({ data, errors }) => {
const hasErrors = errors && errors.length > 0
const hasEntries = db && db.entries && db.entries.length > 0
if (!hasEntries || hasErrors) return
const entries = data.allDoczEntries.edges
if (!entries || entries.length === 0 || hasErrors) return

db.entries.forEach(({ value: entry }) => {
entries.forEach(({ node: entry }) => {
if (!entry) return
const component = path.join(paths.root, entry.filepath)
actions.createPage({
Expand Down
28 changes: 27 additions & 1 deletion core/gatsby-theme-docz/src/node/sourceNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const crypto = require('crypto')
const fs = require('fs-extra')
const { Entries, DataServer, states } = require('docz-core')
const { parseConfig } = require('../utils/parseConfig')
const { omit } = require('lodash/fp')

const digest = str =>
crypto
Expand All @@ -27,7 +28,7 @@ module.exports = async ({ actions, createNodeId }, opts) => {
process.exit(1)
}

dataServer.onStateChange(async ({ type, payload }) => {
const createDbNode = async () => {
const db = await fs.readJSON(config.paths.db)
const contentDigest = digest(JSON.stringify(db))

Expand All @@ -40,5 +41,30 @@ module.exports = async ({ actions, createNodeId }, opts) => {
type: 'DoczDb',
},
})
}

const createEntriesNode = async () => {
const map = await entries.get()
const contentDigest = digest(JSON.stringify(map))
const values = Object.entries(map)

values.forEach(([key, entry]) => {
createNode({
...entry,
children: [],
internal: {
contentDigest,
type: 'DoczEntries',
},
})
})
}

await createDbNode()
await createEntriesNode()

dataServer.onStateChange(async () => {
await createDbNode()
await createEntriesNode()
})
}
3 changes: 3 additions & 0 deletions examples/gatsby/doczrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
menu: ['Home', 'Components'],
}

0 comments on commit 26f7f90

Please sign in to comment.