Skip to content

Commit

Permalink
This diff support creation of tables of content for routes described …
Browse files Browse the repository at this point in the history
…in jsRoutes. (#56)

Previously, jsRoutes was taken as is - there was no reprocessing at all. All routes that needed to appear in the table of contents had to be in mdRoutes. The issue there is that mdRoutes mixed 2 kind of routes - those with documentations (as intended) and those with standalone components.
With this PR, we can go back to a model where mdRoutes contains only routes with markdown files (and can easily be generated instead of manually created)
  • Loading branch information
jckr committed Aug 7, 2018
1 parent 41ee66d commit 268ba33
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions base/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const generatePaths = (children, parentPath, order = []) =>
// order represents the rank of each path in all of its parents.
// ie the 2nd child of the 3rd child of the 1st tree would be [0, 2, 1]
const updatedOrder = [...order, rank]
const updatedChild = {...child, path, order: updatedOrder}
const updatedChild = { ...child, path, order: updatedOrder }

return child.children
? { ...updatedChild, children: generatePaths(child.children, path, updatedOrder) }
: { ...updatedChild, hasToc: true }
Expand Down Expand Up @@ -68,10 +68,13 @@ const reduction = cur =>
? [{ path: cur.path, redirect: getNestedPath(cur.children[0]) }, ...cur.children.map(reduction)]
: cur

export const trees = mdRoutes.reduce((out, { name, path, children = [], data = [] }) => {
out[path] = { name, tree: generatePaths([...children, ...data], path) }
return out
}, {})
export const trees = [...(mdRoutes || []), ...(jsRoutes || [])].reduce(
(out, { name, path, children = [], data = [] }) => {
out[path] = { name, tree: generatePaths([...children, ...data], path) }
return out
},
{}
)

const routes = Object.keys(trees)
.reduce((out, key) => {
Expand All @@ -98,23 +101,23 @@ const routes = Object.keys(trees)
return 0
})

let lastRouteWithDocs
let lastRouteWithContent
const routesPrevNext = routes.reduce((prev, route, i) => {
// this adds to each route with documentation (.markdown property) the reference
// of the previous and next routes with documentation
// of the previous and next routes with documentation
prev.push(route)
if (route.markdown) {
if (lastRouteWithDocs !== undefined) {
prev[lastRouteWithDocs].next = {
if (route.markdown || route.component) {
if (lastRouteWithContent !== undefined) {
prev[lastRouteWithContent].next = {
name: route.name,
path: route.path
}
prev[i].prev = {
name: prev[lastRouteWithDocs].name,
path: prev[lastRouteWithDocs].path
name: prev[lastRouteWithContent].name,
path: prev[lastRouteWithContent].path
}
}
lastRouteWithDocs = i
lastRouteWithContent = i
}
return prev
}, [])
Expand Down Expand Up @@ -148,7 +151,6 @@ export default [
exact: true,
component: Search
},
...jsRoutes,
...flatRoutes
]

Expand Down

0 comments on commit 268ba33

Please sign in to comment.