diff --git a/data/guides.json b/data/guides.json index 50100927a..f13f1d40f 100644 --- a/data/guides.json +++ b/data/guides.json @@ -3,7 +3,6 @@ "title": "Design Patterns for Humans", "description": "A language agnostic, ultra-simplified explanation to design patterns", "slug": "/guides/design-patterns-for-humans", - "path": "/data/guides/design-patterns-for-humans.md", "featured": true, "author": "kamranahmedse", "createdAt": "June 12, 2019", @@ -13,7 +12,6 @@ "title": "Learn Regex", "description": "An easy to understand guide on regular expressions with real world examples", "slug": "/guides/learn-regex", - "path": "/data/guides/learn-regex.md", "featured": true, "author": "ziishaned", "createdDate": "June 19, 2019", @@ -23,7 +21,6 @@ "title": "Bash Guide", "description": "Easy to understand guide for bash with real world usage examples.", "slug": "/guides/bash-guide", - "path": "/data/guides/bash-guide.md", "featured": true, "author": "idnan", "createdAt": "May 18, 2018", @@ -33,7 +30,6 @@ "title": "DNS in One Picture", "description": "Quick illustrative guide on how a website is found on the internet.", "slug": "/guides/dns-in-one-picture", - "path": "/data/guides/dns-in-one-picture.md", "featured": true, "author": "kamranahmedse", "createdAt": "May 11, 2018", @@ -43,7 +39,6 @@ "title": "Using React Hooks", "description": "Start using React hooks in your react applications today with this guide.", "slug": "/guides/using-react-hooks", - "path": "/data/guides/using-react-hooks.md", "featured": true, "author": "kamranahmedse", "createdAt": "October 22, 2019", @@ -53,7 +48,6 @@ "title": "HTTP Caching", "description": "Everything you need to know about web caching", "slug": "/guides/http-caching", - "path": "/data/guides/http-caching.md", "featured": true, "author": "kamranahmedse", "updatedAt": "November 01, 2019", diff --git a/lib/guide.js b/lib/guide.js index e51a66368..a5aaad320 100644 --- a/lib/guide.js +++ b/lib/guide.js @@ -6,18 +6,15 @@ export const getRequestedGuide = req => { 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$/, ''); + // We will use this URL format to find the relevant markdown + // file inside the `/data` directory. For example `/guides/learn-regex` + // has to have `/guides/learn-regex.md` file inside the `data` directory + const path = guide.slug.replace(/^\//, ''); try { return { ...guide, - component: require(`../${path}.md`).default, + component: require(`../data/${path}.md`).default, // component: require(guide.path.replace(/^\//, '')).default }; } catch (e) { diff --git a/next.config.js b/next.config.js index 652c592e2..841d9507b 100644 --- a/next.config.js +++ b/next.config.js @@ -20,6 +20,7 @@ const options = { '/terms': { page: '/terms' }, '/roadmaps': { page: '/roadmaps' }, '/guides': { page: '/guides' }, + '/guides/design-patterns-for-humans': { page: '/guides/[guide]', query: "design-patterns-for-humans" }, '/frontend': { page: '/[fallback]', query: "frontend" }, '/backend': { page: '/[fallback]', query: "backend" }, diff --git a/package.json b/package.json index f4619d865..f0b9b3b9d 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "styled-components": "^4.4.0" }, "devDependencies": { - "babel-plugin-styled-components": "^1.10.6" + "babel-plugin-styled-components": "^1.10.6", + "glob": "^7.1.5" } } diff --git a/pages/home.js b/pages/home.js deleted file mode 100644 index f3313472e..000000000 --- a/pages/home.js +++ /dev/null @@ -1,16 +0,0 @@ -import FeaturedContent from 'components/featured-content/index'; -import HeroSection from 'components/hero-section/index'; -import PageFooter from 'components/page-footer/index'; -import PageHeader from 'components/page-header/index'; -import DefaultLayout from 'layouts/default/index'; - -const Home = (props) => ( - - - - - - -); - -export default Home; diff --git a/pages/index.js b/pages/index.js index 202ab2803..f3313472e 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,10 +1,16 @@ -import Home from './home'; -import DefaultLayout from 'layouts/default'; +import FeaturedContent from 'components/featured-content/index'; +import HeroSection from 'components/hero-section/index'; +import PageFooter from 'components/page-footer/index'; +import PageHeader from 'components/page-header/index'; +import DefaultLayout from 'layouts/default/index'; -const Index = () => ( +const Home = (props) => ( - + + + + ); -export default Index; +export default Home; diff --git a/path-map.js b/path-map.js new file mode 100644 index 000000000..6271f7d39 --- /dev/null +++ b/path-map.js @@ -0,0 +1,35 @@ +const path = require('path'); +const fs = require('fs'); +const glob = require('glob'); + +const PAGES_PATH = path.join(__dirname, 'pages'); + +/** + * Generate the page routes from the page files inside `/pages` + * directory. Gives the format understood by next + * { + * '/slug': { page: '/path/to-file' } + * } + */ +const getPageRoutes = () => { + const files = glob.sync(`${PAGES_PATH}/**/*.js`, { + ignore: [ + '**/_*.js', // private non-page files e.g. _document.js + '**/[[]*[]].js', // Ignore dynamic pages i.e. `page/[something].js` files + '**/[[]*[]]/*.js', // Ignore files inside dynamic pages i.e. `[something]/abc.js` + ] + }); + + const pageRoutes = {}; + + files.forEach(file => { + const pageName = file.replace(PAGES_PATH, '').replace('.js', ''); + const pagePath = pageName.replace('/index', '') || '/'; + + pageRoutes[pagePath] = { page: `${pageName}` } + }); + + return pageRoutes; +}; + +console.log(getPageRoutes()); diff --git a/yarn.lock b/yarn.lock index 1dec97f77..592bf050f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3269,6 +3269,18 @@ glob@^7.0.0, glob@^7.0.3, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@~7.1.1: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.1.5: + version "7.1.5" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.5.tgz#6714c69bee20f3c3e64c4dd905553e532b40cdc0" + integrity sha512-J9dlskqUXK1OeTOYBEn5s8aMukWMwWfs+rPTn/jn50Ux4MNXVhubL1wu/j2t+H4NVI+cXEcCaYellqaPVGXNqQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + globals@^11.1.0: version "11.12.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"