parent
629f1058f2
commit
35f6070133
4 changed files with 34 additions and 21 deletions
@ -1,27 +1,28 @@ |
|||||||
import roadmaps from "storage/roadmaps"; |
import roadmaps from "storage/roadmaps"; |
||||||
|
|
||||||
export const getRequestedRoadmap = req => { |
export const getRequestedRoadmap = req => { |
||||||
// Considering it a new roadmap URL e.g. `/roadmaps/frontend`
|
const normalizedUrl = req.url.replace(/\/$/, ''); |
||||||
const currentUrl = req.url.replace(/\/$/, ''); |
const foundRoadmap = roadmaps.find(roadmap => normalizedUrl.startsWith(roadmap.url)); |
||||||
// Get the roadmap version out of the URL e.g. `/roadmaps/frontend/2019`
|
|
||||||
const [foundVersion = ''] = currentUrl.match(/(\d+|latest)$/) || ['latest']; |
|
||||||
const foundVersionRegex = new RegExp(`\/?${foundVersion}$`); |
|
||||||
// Remove version from the URL because urls in roadmaps list don't have versions
|
|
||||||
const urlWithoutVersion = currentUrl.replace(foundVersionRegex, ''); |
|
||||||
|
|
||||||
const urlToSlugList = [ |
|
||||||
currentUrl, |
|
||||||
urlWithoutVersion, |
|
||||||
]; |
|
||||||
|
|
||||||
const foundRoadmap = roadmaps.find(roadmap => urlToSlugList.includes(roadmap.url)); |
|
||||||
if (!foundRoadmap) { |
if (!foundRoadmap) { |
||||||
return null; |
return null; |
||||||
} |
} |
||||||
|
|
||||||
|
const roadmapPages = Object.values(foundRoadmap.sidebar || {}) |
||||||
|
.reduce((acc, menuPages) => { |
||||||
|
return [ |
||||||
|
...acc, |
||||||
|
...menuPages |
||||||
|
] |
||||||
|
}, []); |
||||||
|
|
||||||
|
const foundPage = roadmapPages.find(page => page.url === normalizedUrl) || {}; |
||||||
return { |
return { |
||||||
...foundRoadmap, |
...foundRoadmap, |
||||||
version: foundVersion, |
// Use the current page data or that of the found roadmap i.e. show the summary
|
||||||
picture: (foundRoadmap.picture || '').replace('{version}', foundVersion), |
page: { |
||||||
|
title: foundPage.title || foundRoadmap.title, |
||||||
|
url: foundPage.url || foundRoadmap.url, |
||||||
|
path: foundPage.path || foundRoadmap.path |
||||||
|
}, |
||||||
}; |
}; |
||||||
}; |
}; |
||||||
|
Loading…
Reference in new issue