Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 26f7f90

Browse files
committed
fix(gatsby-theme-docz): add specific nodes for entries
1 parent aca1bee commit 26f7f90

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

core/gatsby-theme-docz/src/node/createPages.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
const path = require('path')
22
const { getDoczConfig } = require('../utils/parseConfig')
33

4-
const parseDatabase = data => {
5-
try {
6-
return JSON.parse(data.doczDb.db)
7-
} catch (err) {
8-
return null
9-
}
10-
}
11-
124
const mountRoute = (base = '/', route) => {
135
return `${base === '/' ? '' : base}${route}`
146
}
157

8+
const ENTRIES_QUERY = `
9+
{
10+
allDoczEntries{
11+
edges{
12+
node{
13+
id
14+
filepath
15+
route
16+
slug
17+
name
18+
menu
19+
order
20+
headings {
21+
slug
22+
depth
23+
value
24+
}
25+
}
26+
}
27+
}
28+
}
29+
`
30+
1631
module.exports = ({ graphql, actions }, opts) => {
1732
const { paths, ...config } = getDoczConfig(opts)
1833

19-
return graphql(`
20-
{
21-
doczDb {
22-
id
23-
db
24-
}
25-
}
26-
`).then(({ data, errors }) => {
27-
const db = parseDatabase(data)
34+
return graphql(ENTRIES_QUERY).then(({ data, errors }) => {
2835
const hasErrors = errors && errors.length > 0
29-
const hasEntries = db && db.entries && db.entries.length > 0
30-
if (!hasEntries || hasErrors) return
36+
const entries = data.allDoczEntries.edges
37+
if (!entries || entries.length === 0 || hasErrors) return
3138

32-
db.entries.forEach(({ value: entry }) => {
39+
entries.forEach(({ node: entry }) => {
3340
if (!entry) return
3441
const component = path.join(paths.root, entry.filepath)
3542
actions.createPage({

core/gatsby-theme-docz/src/node/sourceNodes.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const crypto = require('crypto')
22
const fs = require('fs-extra')
33
const { Entries, DataServer, states } = require('docz-core')
44
const { parseConfig } = require('../utils/parseConfig')
5+
const { omit } = require('lodash/fp')
56

67
const digest = str =>
78
crypto
@@ -27,7 +28,7 @@ module.exports = async ({ actions, createNodeId }, opts) => {
2728
process.exit(1)
2829
}
2930

30-
dataServer.onStateChange(async ({ type, payload }) => {
31+
const createDbNode = async () => {
3132
const db = await fs.readJSON(config.paths.db)
3233
const contentDigest = digest(JSON.stringify(db))
3334

@@ -40,5 +41,30 @@ module.exports = async ({ actions, createNodeId }, opts) => {
4041
type: 'DoczDb',
4142
},
4243
})
44+
}
45+
46+
const createEntriesNode = async () => {
47+
const map = await entries.get()
48+
const contentDigest = digest(JSON.stringify(map))
49+
const values = Object.entries(map)
50+
51+
values.forEach(([key, entry]) => {
52+
createNode({
53+
...entry,
54+
children: [],
55+
internal: {
56+
contentDigest,
57+
type: 'DoczEntries',
58+
},
59+
})
60+
})
61+
}
62+
63+
await createDbNode()
64+
await createEntriesNode()
65+
66+
dataServer.onStateChange(async () => {
67+
await createDbNode()
68+
await createEntriesNode()
4369
})
4470
}

examples/gatsby/doczrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
menu: ['Home', 'Components'],
3+
}

0 commit comments

Comments
 (0)