Add module resolver and refactor guide loading

This commit is contained in:
Kamran Ahmed
2019-11-06 18:20:09 +04:00
parent a4338eb00c
commit 2cf22c1777
23 changed files with 116 additions and 79 deletions

28
lib/guide.js Normal file
View File

@@ -0,0 +1,28 @@
import guides from "data/guides";
export const getRequestedGuide = req => {
const guide = guides.find(guide => guide.slug === req.url);
if (!guide) {
return null;
}
// Remove any slashes from the beginning
// Webpack module resolver takes care of the base path
// Look at `config.resolve.modules` in next.config.js
// Remove `.md` from the end
// We need to put that in `require` below to make
// webpack bundle all the markdown files
const path = guide.path.replace(/^\//, '').replace(/\.md$/, '');
try {
return {
...guide,
component: require(`../${path}.md`).default,
// component: require(guide.path.replace(/^\//, '')).default
};
} catch (e) {
console.log(e);
}
return null;
};