Relevant page loading on detail page

This commit is contained in:
Kamran Ahmed
2019-12-01 15:01:22 +04:00
parent 629f1058f2
commit 35f6070133
4 changed files with 34 additions and 21 deletions

View File

@@ -1,27 +1,28 @@
import roadmaps from "storage/roadmaps";
export const getRequestedRoadmap = req => {
// Considering it a new roadmap URL e.g. `/roadmaps/frontend`
const currentUrl = req.url.replace(/\/$/, '');
// 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));
const normalizedUrl = req.url.replace(/\/$/, '');
const foundRoadmap = roadmaps.find(roadmap => normalizedUrl.startsWith(roadmap.url));
if (!foundRoadmap) {
return null;
}
const roadmapPages = Object.values(foundRoadmap.sidebar || {})
.reduce((acc, menuPages) => {
return [
...acc,
...menuPages
]
}, []);
const foundPage = roadmapPages.find(page => page.url === normalizedUrl) || {};
return {
...foundRoadmap,
version: foundVersion,
picture: (foundRoadmap.picture || '').replace('{version}', foundVersion),
// Use the current page data or that of the found roadmap i.e. show the summary
page: {
title: foundPage.title || foundRoadmap.title,
url: foundPage.url || foundRoadmap.url,
path: foundPage.path || foundRoadmap.path
},
};
};