Skip to content

Commit

Permalink
feat(gatsby-theme-docz): set first entry as root when no / route given
Browse files Browse the repository at this point in the history
  • Loading branch information
rakannimer committed Sep 29, 2019
1 parent 3c0aecf commit 8f89a7d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions core/gatsby-theme-docz/lib/createPages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { get } = require('lodash/fp')
const { parseConfig } = require('docz-core')

const ENTRIES_QUERY = `
{
Expand All @@ -23,12 +24,27 @@ const ENTRIES_QUERY = `
}
`

module.exports = ({ graphql, actions }) => {
return graphql(ENTRIES_QUERY).then(({ data, errors }) => {
module.exports = async ({ graphql, actions }, opts = {}) => {
return graphql(ENTRIES_QUERY).then(async ({ data, errors }) => {
const hasErrors = errors && errors.length > 0
const entries = get('allDoczEntries.edges', data)
if (!entries || entries.length === 0 || hasErrors) return

const defaultEntry = entries.find(({ node: entry }) => entry.route === '/')
if (defaultEntry === undefined) {
const config = await parseConfig(opts)
// Create a default entry unless specifically denied by config
const shouldNotCreateRootRoute = Boolean(config.noRootRoute)
if (shouldNotCreateRootRoute === false) {
// Set the first found entry as the default entry
const createdDefaultEntry = {
node: {
...entries[0].node,
route: '/',
},
}
entries.unshift(createdDefaultEntry)
}
}
entries.forEach(({ node: entry }) => {
if (!entry) return
actions.createPage({
Expand Down

0 comments on commit 8f89a7d

Please sign in to comment.