Add roadmap pathmap generator

pull/1657/head
Kamran Ahmed 5 years ago
parent 822a55d186
commit ce984825a5
  1. 0
      pages/[roadmap]/[page].js
  2. 39
      scripts/path-map.js
  3. 12
      storage/roadmaps/frontend/meta.json
  4. 2
      storage/roadmaps/frontend/summary.md

@ -1,10 +1,12 @@
const path = require('path'); const path = require('path');
const glob = require('glob'); const glob = require('glob');
const fs = require('fs');
const guides = require('../storage/guides.json'); const guides = require('../storage/guides.json');
const roadmaps = require('../storage/roadmaps'); const roadmaps = require('../storage/roadmaps');
const PAGES_PATH = path.join(__dirname, '../pages'); const PAGES_PATH = path.join(__dirname, '../pages');
const ROADMAPS_PATH = path.join(__dirname, '../storage/roadmaps');
/** /**
* Generate the page routes from the page files inside `/pages` * Generate the page routes from the page files inside `/pages`
@ -19,7 +21,7 @@ const getPageRoutes = () => {
'**/_*.js', // private non-page files e.g. _document.js '**/_*.js', // private non-page files e.g. _document.js
'**/[[]*[]].js', // Ignore dynamic pages i.e. `page/[something].js` files '**/[[]*[]].js', // Ignore dynamic pages i.e. `page/[something].js` files
'**/[[]*[]]/*.js', // Ignore files inside dynamic pages i.e. `[something]/abc.js` '**/[[]*[]]/*.js', // Ignore files inside dynamic pages i.e. `[something]/abc.js`
] ],
}); });
const pageRoutes = {}; const pageRoutes = {};
@ -45,7 +47,7 @@ const getGuideRoutes = () => {
[guide.url]: { [guide.url]: {
page: '/guides/[guide]', page: '/guides/[guide]',
query: slug, query: slug,
} },
}; };
}, {}); }, {});
}; };
@ -55,31 +57,38 @@ const getGuideRoutes = () => {
* @returns {*} * @returns {*}
*/ */
const getRoadmapRoutes = () => { const getRoadmapRoutes = () => {
return roadmaps.reduce((roadmapRoutes, roadmap) => { const roadmaps = fs.readdirSync(ROADMAPS_PATH);
const [, slug] = roadmap.url.split('/'); return roadmaps.reduce((roadmapRoutes, dirName) => {
const roadmapUrl = `/${dirName}`;
const roadmapDir = path.join(ROADMAPS_PATH, dirName);
const pageFilePaths = glob.sync(`${roadmapDir}/**/*.md`);
return { return {
...roadmapRoutes, ...roadmapRoutes,
// Default roadmap path i.e. `{ '/frontend': { page: '/[roadmap]/index', query: 'frontend' }` // Default roadmap path i.e. `{ '/frontend': { page: '/[roadmap]', query: 'frontend' }`
[roadmap.url]: { [roadmapUrl]: {
page: '/[roadmap]', page: '/[roadmap]',
query: slug query: dirName,
}, },
// Route for each of the versions of this roadmap i.e. // Routes for all the pages inside this directory
// `{ '/frontend/2019': { page: '/[roadmap]/[version]', query: 'frontend/2019' } }` ...pageFilePaths.reduce((pageRoutes, pageFilePath) => {
...((roadmap.versions || []).reduce((versionRoutes, version) => { const pageFileName = path.basename(pageFilePath, '.md');
const pageSlug = pageFileName.replace(/^\d+-/, '').toLowerCase();
return { return {
...versionRoutes, ...pageRoutes,
[`${roadmap.url}/${version}`]: { [`${roadmapUrl}/${pageSlug}`]: {
page: '/[roadmap]/[version]', page: '/[roadmap]/[page]',
query: `${slug}/${version}` query: `${roadmapUrl}/${pageSlug}`
} }
}; };
}, {})), }, {})
}; };
}, {}); }, {});
}; };
console.log(getRoadmapRoutes());
/** /**
* Generates the path-map understood by next.js * Generates the path-map understood by next.js
* @returns {{}} * @returns {{}}

@ -0,0 +1,12 @@
{
"title": "Frontend Developer",
"description": "Step by step guide to becoming a modern frontend developer",
"featuredDescription": "Step by step guide to becoming a modern frontend developer in 2019",
"picture": "/static/roadmaps/{version}/frontend.png",
"featured": true,
"versions": [
"latest",
"2018",
"2017"
]
}

@ -0,0 +1,2 @@
## Frontend Developer
How to become a modern frontend developer
Loading…
Cancel
Save