Skip to content

Commit

Permalink
Add module resolver and refactor guide loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranahmedse committed Nov 6, 2019
1 parent a4338eb commit 2cf22c1
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 79 deletions.
6 changes: 3 additions & 3 deletions components/featured-content/guides.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';
import { FeaturedContentWrap } from './style';
import guides from '../../data/guides';
import GuideBlock from '../guide-block';
import guides from 'data/guides';
import GuideBlock from 'components/guide-block';

const FeaturedGuides = () => (
<FeaturedContentWrap className="featured-content-wrap">
Expand All @@ -23,4 +23,4 @@ const FeaturedGuides = () => (
</FeaturedContentWrap>
);

export default FeaturedGuides;
export default FeaturedGuides;
3 changes: 1 addition & 2 deletions components/featured-content/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FeaturedWrap } from './style';
import FeaturedJourneys from './journeys';
import FeaturedGuides from './guides';
import FeaturedRoadmaps from './roadmaps';

Expand All @@ -14,4 +13,4 @@ FeaturedContent.defaultProps = {
className: '',
};

export default FeaturedContent;
export default FeaturedContent;
6 changes: 3 additions & 3 deletions components/featured-content/roadmaps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';
import { FeaturedContentWrap } from './style';
import roadmaps from '../../data/roadmaps';
import RoadmapBlock from '../roadmap-block';
import roadmaps from 'data/roadmaps';
import RoadmapBlock from 'components/roadmap-block';

const FeaturedRoadmaps = () => (
<FeaturedContentWrap className="featured-content-wrap">
Expand All @@ -28,4 +28,4 @@ const FeaturedRoadmaps = () => (
</FeaturedContentWrap>
);

export default FeaturedRoadmaps;
export default FeaturedRoadmaps;
4 changes: 2 additions & 2 deletions components/guide-block/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from 'next/link';
import { Author, AuthorImage, AuthorName, BlockLink, BlockMeta, BlockSubtitle, BlockTitle, PublishDate } from './style';
import { findByUsername } from '../../lib/author';
import { findByUsername } from 'lib/author';

const GuideBlock = ({ guide }) => {
const author = findByUsername(guide.author) || {};
Expand All @@ -23,4 +23,4 @@ const GuideBlock = ({ guide }) => {
)
};

export default GuideBlock;
export default GuideBlock;
4 changes: 2 additions & 2 deletions components/guide-body/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MDXProvider } from '@mdx-js/react';
import MdxComponents from '../mdx-components';
import MdxComponents from 'components/mdx-components';
import { GuideBodyWrap } from './style';

const GuideBody = (props) => (
Expand All @@ -10,4 +10,4 @@ const GuideBody = (props) => (
</MDXProvider>
);

export default GuideBody;
export default GuideBody;
8 changes: 7 additions & 1 deletion data/guides.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"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",
Expand All @@ -12,6 +13,7 @@
"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",
Expand All @@ -21,6 +23,7 @@
"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",
Expand All @@ -30,6 +33,7 @@
"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",
Expand All @@ -39,6 +43,7 @@
"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",
Expand All @@ -48,9 +53,10 @@
"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",
"createdAt": "November 01, 2019"
}
]
]
File renamed without changes.
8 changes: 4 additions & 4 deletions layouts/guide/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import DefaultLayout from '../default';
import PageHeader from '../../components/page-header';
import PageFooter from '../../components/page-footer';
import DefaultLayout from 'layouts/default';
import PageHeader from 'components/page-header';
import PageFooter from 'components/page-footer';

class GuideLayout extends React.Component {
render() {
Expand All @@ -17,4 +17,4 @@ class GuideLayout extends React.Component {
}
}

export default GuideLayout;
export default GuideLayout;
4 changes: 2 additions & 2 deletions lib/author.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import authors from "../data/authors";
import authors from "data/authors";

export const findByUsername = (username) => authors.find(author => author.username === username) || {};
export const findByUsername = (username) => authors.find(author => author.username === username) || {};
28 changes: 28 additions & 0 deletions lib/guide.js
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 2 additions & 2 deletions lib/roadmap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import roadmaps from "../data/roadmaps";
import roadmaps from "data/roadmaps";

export const getRequestedRoadmap = req => {
// Considering it a new roadmap URL e.g. `/roadmaps/frontend`
Expand Down Expand Up @@ -29,4 +29,4 @@ export const getRequestedRoadmap = req => {
version: foundVersion,
picture: (foundRoadmap.picture || '').replace('{version}', foundVersion),
};
};
};
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
export const serverOnlyProps = (callback) => {
return async (props) => {
if (process.browser) {
// noinspection ES6ModulesDependencies,JSUnresolvedVariable
return __NEXT_DATA__.props.pageProps;
}

return await callback(props)
};
};
};
8 changes: 6 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const withSass = require('@zeit/next-sass');
const withCSS = require('@zeit/next-css');
const rehypePrism = require('@mapbox/rehype-prism')
const rehypePrism = require('@mapbox/rehype-prism');

const withMDX = require('@next/mdx')({
extension: /\.(md|mdx)?$/,
Expand All @@ -19,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" },
'/devops': { page: '/[fallback]', query: "devops" },
Expand All @@ -38,6 +40,8 @@ const options = {
use: ['@svgr/webpack'],
});

config.resolve.modules.push(path.resolve('./'));

// Allow loading images
config.module.rules.push({
test: /\.(png|jpg|gif|eot|ttf|woff|woff2)$/,
Expand All @@ -58,4 +62,4 @@ let nextConfig = withSass(options);
nextConfig = withCSS(nextConfig);
nextConfig = withMDX(nextConfig);

module.exports = nextConfig;
module.exports = nextConfig;
8 changes: 4 additions & 4 deletions pages/[fallback]/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Error from 'next/error';
import Roadmap from '../roadmaps/[roadmap]/index';
import { serverOnlyProps } from '../../lib/server';
import { getRequestedRoadmap } from '../../lib/roadmap';
import Roadmap from 'pages/roadmaps/[roadmap]/index';
import { serverOnlyProps } from 'lib/server';
import { getRequestedRoadmap } from 'lib/roadmap';

// Fallback page to handle the old roadmap pages implementation
const OldRoadmap = ({ roadmap }) => {
Expand All @@ -19,4 +19,4 @@ OldRoadmap.getInitialProps = serverOnlyProps(({ req }) => {
});


export default OldRoadmap;
export default OldRoadmap;
12 changes: 6 additions & 6 deletions pages/about.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AboutHeader from '../components/about-header/index';
import PageFooter from '../components/page-footer/index';
import PageHeader from '../components/page-header/index';
import DefaultLayout from '../layouts/default/index';
import FaqList from '../components/faq-list/index';
import AboutHeader from 'components/about-header/index';
import PageFooter from 'components/page-footer/index';
import PageHeader from 'components/page-header/index';
import DefaultLayout from 'layouts/default/index';
import FaqList from 'components/faq-list/index';

const About = () => (
<DefaultLayout>
Expand All @@ -13,4 +13,4 @@ const About = () => (
</DefaultLayout>
);

export default About;
export default About;
29 changes: 14 additions & 15 deletions pages/guides/[guide].js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import GuideLayout from '../../layouts/guide';
import { serverOnlyProps } from '../../lib/server';

import GuideHeader from '../../components/guide-header';
import GuideContent from '../../data/guides/keep-it-clean.md';
import GuideBody from '../../components/guide-body';
import ShareGuide from '../../components/share-guide';
import GuideFooter from '../../components/guide-footer';
import Error from "next/error";
import GuideLayout from 'layouts/guide';
import { serverOnlyProps } from 'lib/server';
import GuideHeader from 'components/guide-header';
import GuideBody from 'components/guide-body';
import ShareGuide from 'components/share-guide';
import GuideFooter from 'components/guide-footer';
import { getRequestedGuide } from "lib/guide";

const Guide = ({ guide }) => {
if (!guide) {
return <Error statusCode={404} />
}

return (
<GuideLayout>
<GuideHeader/>
<GuideBody>
<GuideContent/>
<guide.component />
<ShareGuide/>
</GuideBody>
<GuideFooter/>
Expand All @@ -21,13 +25,8 @@ const Guide = ({ guide }) => {
};

Guide.getInitialProps = serverOnlyProps(({ req }) => {
// Remove URL chunk to make it a slug e.g. /guides/some-guide-item to become `some-guide-item
const slug = req.url
.replace(/^\/*?guides\/*?/, '/')
.replace(/\/*$/, '');

return {
slug,
guide: getRequestedGuide(req)
};
});

Expand Down
6 changes: 3 additions & 3 deletions pages/guides/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DefaultLayout from '../../layouts/default/index';
import PageHeader from '../../components/page-header/index';
import DefaultLayout from 'layouts/default/index';
import PageHeader from 'components/page-header/index';

const Roadmap = () => (
<DefaultLayout>
Expand All @@ -10,4 +10,4 @@ const Roadmap = () => (
</DefaultLayout>
);

export default Roadmap;
export default Roadmap;
12 changes: 6 additions & 6 deletions pages/home.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
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>
Expand All @@ -13,4 +13,4 @@ const Home = (props) => (
</DefaultLayout>
);

export default Home;
export default Home;
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Home from './home';
import DefaultLayout from '../layouts/default';
import DefaultLayout from 'layouts/default';

const Index = () => (
<DefaultLayout>
Expand Down
10 changes: 5 additions & 5 deletions pages/privacy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2cf22c1

Please sign in to comment.