Files
developer-roadmap/lib/roadmap.js

28 lines
918 B
JavaScript
Raw Normal View History

2019-11-13 23:06:07 +04:00
import roadmaps from "storage/roadmaps";
2019-10-31 23:45:09 +04:00
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, '');
2019-10-31 23:45:09 +04:00
const urlToSlugList = [
currentUrl,
urlWithoutVersion,
2019-10-31 23:45:09 +04:00
];
const foundRoadmap = roadmaps.find(roadmap => urlToSlugList.includes(roadmap.url));
2019-11-01 02:27:06 +04:00
if (!foundRoadmap) {
return null;
}
2019-10-31 23:49:30 +04:00
2019-10-31 23:45:09 +04:00
return {
2019-10-31 23:49:30 +04:00
...foundRoadmap,
2019-10-31 23:45:09 +04:00
version: foundVersion,
2019-10-31 23:49:30 +04:00
picture: (foundRoadmap.picture || '').replace('{version}', foundVersion),
2019-10-31 23:45:09 +04:00
};
};