Add pathmap generation

pull/1331/head
Kamran Ahmed 5 years ago
parent 2cf22c1777
commit 159741f0af
  1. 6
      data/guides.json
  2. 13
      lib/guide.js
  3. 1
      next.config.js
  4. 3
      package.json
  5. 16
      pages/home.js
  6. 16
      pages/index.js
  7. 35
      path-map.js
  8. 12
      yarn.lock

@ -3,7 +3,6 @@
"title": "Design Patterns for Humans", "title": "Design Patterns for Humans",
"description": "A language agnostic, ultra-simplified explanation to design patterns", "description": "A language agnostic, ultra-simplified explanation to design patterns",
"slug": "/guides/design-patterns-for-humans", "slug": "/guides/design-patterns-for-humans",
"path": "/data/guides/design-patterns-for-humans.md",
"featured": true, "featured": true,
"author": "kamranahmedse", "author": "kamranahmedse",
"createdAt": "June 12, 2019", "createdAt": "June 12, 2019",
@ -13,7 +12,6 @@
"title": "Learn Regex", "title": "Learn Regex",
"description": "An easy to understand guide on regular expressions with real world examples", "description": "An easy to understand guide on regular expressions with real world examples",
"slug": "/guides/learn-regex", "slug": "/guides/learn-regex",
"path": "/data/guides/learn-regex.md",
"featured": true, "featured": true,
"author": "ziishaned", "author": "ziishaned",
"createdDate": "June 19, 2019", "createdDate": "June 19, 2019",
@ -23,7 +21,6 @@
"title": "Bash Guide", "title": "Bash Guide",
"description": "Easy to understand guide for bash with real world usage examples.", "description": "Easy to understand guide for bash with real world usage examples.",
"slug": "/guides/bash-guide", "slug": "/guides/bash-guide",
"path": "/data/guides/bash-guide.md",
"featured": true, "featured": true,
"author": "idnan", "author": "idnan",
"createdAt": "May 18, 2018", "createdAt": "May 18, 2018",
@ -33,7 +30,6 @@
"title": "DNS in One Picture", "title": "DNS in One Picture",
"description": "Quick illustrative guide on how a website is found on the internet.", "description": "Quick illustrative guide on how a website is found on the internet.",
"slug": "/guides/dns-in-one-picture", "slug": "/guides/dns-in-one-picture",
"path": "/data/guides/dns-in-one-picture.md",
"featured": true, "featured": true,
"author": "kamranahmedse", "author": "kamranahmedse",
"createdAt": "May 11, 2018", "createdAt": "May 11, 2018",
@ -43,7 +39,6 @@
"title": "Using React Hooks", "title": "Using React Hooks",
"description": "Start using React hooks in your react applications today with this guide.", "description": "Start using React hooks in your react applications today with this guide.",
"slug": "/guides/using-react-hooks", "slug": "/guides/using-react-hooks",
"path": "/data/guides/using-react-hooks.md",
"featured": true, "featured": true,
"author": "kamranahmedse", "author": "kamranahmedse",
"createdAt": "October 22, 2019", "createdAt": "October 22, 2019",
@ -53,7 +48,6 @@
"title": "HTTP Caching", "title": "HTTP Caching",
"description": "Everything you need to know about web caching", "description": "Everything you need to know about web caching",
"slug": "/guides/http-caching", "slug": "/guides/http-caching",
"path": "/data/guides/http-caching.md",
"featured": true, "featured": true,
"author": "kamranahmedse", "author": "kamranahmedse",
"updatedAt": "November 01, 2019", "updatedAt": "November 01, 2019",

@ -6,18 +6,15 @@ export const getRequestedGuide = req => {
return null; return null;
} }
// Remove any slashes from the beginning // We will use this URL format to find the relevant markdown
// Webpack module resolver takes care of the base path // file inside the `/data` directory. For example `/guides/learn-regex`
// Look at `config.resolve.modules` in next.config.js // has to have `/guides/learn-regex.md` file inside the `data` directory
// Remove `.md` from the end const path = guide.slug.replace(/^\//, '');
// We need to put that in `require` below to make
// webpack bundle all the markdown files
const path = guide.path.replace(/^\//, '').replace(/\.md$/, '');
try { try {
return { return {
...guide, ...guide,
component: require(`../${path}.md`).default, component: require(`../data/${path}.md`).default,
// component: require(guide.path.replace(/^\//, '')).default // component: require(guide.path.replace(/^\//, '')).default
}; };
} catch (e) { } catch (e) {

@ -20,6 +20,7 @@ const options = {
'/terms': { page: '/terms' }, '/terms': { page: '/terms' },
'/roadmaps': { page: '/roadmaps' }, '/roadmaps': { page: '/roadmaps' },
'/guides': { page: '/guides' }, '/guides': { page: '/guides' },
'/guides/design-patterns-for-humans': { page: '/guides/[guide]', query: "design-patterns-for-humans" }, '/guides/design-patterns-for-humans': { page: '/guides/[guide]', query: "design-patterns-for-humans" },
'/frontend': { page: '/[fallback]', query: "frontend" }, '/frontend': { page: '/[fallback]', query: "frontend" },
'/backend': { page: '/[fallback]', query: "backend" }, '/backend': { page: '/[fallback]', query: "backend" },

@ -34,6 +34,7 @@
"styled-components": "^4.4.0" "styled-components": "^4.4.0"
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-styled-components": "^1.10.6" "babel-plugin-styled-components": "^1.10.6",
"glob": "^7.1.5"
} }
} }

@ -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) => (
<DefaultLayout>
<PageHeader />
<HeroSection />
<FeaturedContent />
<PageFooter />
</DefaultLayout>
);
export default Home;

@ -1,10 +1,16 @@
import Home from './home'; import FeaturedContent from 'components/featured-content/index';
import DefaultLayout from 'layouts/default'; 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) => (
<DefaultLayout> <DefaultLayout>
<Home /> <PageHeader />
<HeroSection />
<FeaturedContent />
<PageFooter />
</DefaultLayout> </DefaultLayout>
); );
export default Index; export default Home;

@ -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());

@ -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" once "^1.3.0"
path-is-absolute "^1.0.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: globals@^11.1.0:
version "11.12.0" version "11.12.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e" resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"

Loading…
Cancel
Save