UNPKG

1.14 kBJavaScriptView Raw
1const { join } = require('path')
2/**
3 * For every route specified under a registered directory,
4 * changes route path to comply with content's permalink configuration.
5 *
6 */
7exports.interceptRoutes = function (routes, content, options) {
8 // get routes and permalink pair for every registered directory
9 const contentRoutePaths = {}
10 options.dirs.forEach(registeredType => {
11 const dirOpts = registeredType[1]
12 const routeName = dirOpts.routeName || options.routeName
13 if (routeName === '') return // don't add route
14 const path = join('/' + dirOpts.permalink || options.permalink)
15 contentRoutePaths[routeName] = path
16 })
17
18 // if routes match, change path to the content's permalink
19 routes.forEach(route => {
20 if (contentRoutePaths[route.name]) {
21 route.path = contentRoutePaths[route.name]
22 }
23 })
24}
25
26/**
27 * Returns route paths for all content pages.
28 */
29exports.getRoutes = function (content) {
30 const contentRoutes = []
31 Object.keys(content).forEach(registeredDir => {
32 content[registeredDir].forEach(page => {
33 contentRoutes.push(page.data.permalink)
34 })
35 })
36 return contentRoutes
37}