Add module resolver and refactor guide loading
This commit is contained in:
28
lib/guide.js
Normal file
28
lib/guide.js
Normal 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;
|
||||
};
|
Reference in New Issue
Block a user