Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Refactoring Site Creation and Additions #157

Merged
merged 12 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"hosting": {
"target": "nodejs-dev",
"public": "public",
"rewrites": [{
"source": "/learn/**",
"destination": "/learn/"
}],
"ignore": [
"firebase.json",
"**/.*",
Expand Down
29 changes: 10 additions & 19 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
if (process.env.ENVIROMENT !== 'production') {
require('dotenv').config()
require('dotenv').config();
}

// const contentfulConfig = {
// spaceId: process.env.SPACE_ID,
// accessToken: process.env.ACCESS_TOKEN,
// }

const config = require('./src/config')
const config = require('./src/config');

module.exports = {
pathPrefix: process.env.PATH_PREFIX,
Expand Down Expand Up @@ -73,10 +68,6 @@ module.exports = {
],
},
},
// {
// resolve: `gatsby-source-contentful`,
// options: contentfulConfig,
// },
{
resolve: `gatsby-plugin-sitemap`,
options: {
Expand Down Expand Up @@ -104,30 +95,30 @@ module.exports = {
}
}`,
serialize: ({ site, allSitePage, allMarkdownRemark }) => {
let pages = []
let pages = [];
allSitePage.edges.map(edge => {
pages.push({
url: site.siteMetadata.siteUrlNoSlash + edge.node.path,
changefreq: `daily`,
priority: 0.7,
})
})
});
});
allMarkdownRemark.edges.map(edge => {
pages.push({
url: `${site.siteMetadata.siteUrlNoSlash}/learn/${
url: `${site.siteMetadata.siteUrlNoSlash}/${
edge.node.fields.slug
}`,
changefreq: `daily`,
priority: 0.7,
})
})
});
});

return pages
return pages;
},
},
},
{
resolve: `gatsby-plugin-emotion`,
},
],
}
};
165 changes: 148 additions & 17 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,162 @@
const createSlug = require('./src/util/createSlug');
const path = require('path');

exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect, createNodeField } = actions;

// If this is the learn page, accept all following paths.
console.log(page.path);
if (!!~page.path.indexOf('/learn')) {
deletePage(page);
createPage({
...page,
path: '/'
});
createPage({
...page,
matchPath: "/learn/*",
});
}
}
return new Promise((resolve, reject) => {
const docTemplate = path.resolve('./src/templates/learn.tsx');

resolve(
graphql(
`
{
allMarkdownRemark(
filter: { fields: { slug: { ne: "" } } }
sort: { fields: [fileAbsolutePath], order: ASC }
) {
edges {
node {
id
fileAbsolutePath
html
parent {
... on File {
relativePath
}
}
frontmatter {
title
description
authors
section
}
fields {
slug
}
}
next {
frontmatter {
title
}
fields {
slug
}
}
previous {
frontmatter {
title
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
const { edges } = result.data.allMarkdownRemark;
let navigationData = {};
const docPages = [];
edges.forEach(({ node }, index) => {
const {
fields: { slug, authors },
frontmatter: { title, section },
parent: { relativePath },
} = node;

let previousNodeData = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we initialize it with null instead of undefined. That way, the intent is more explicit — assigning a deliberate non-value.

let previousNodeData = null; //null - deliberate non-value

Or if we need it to be initialized with undefined, we can just leave it uninitialized.

let previousNodeData; //undefined - unintentional value

+same throughout.

const previousNode = index === 0 ? undefined : edges[index - 1].node;
if (previousNode) {
previousNodeData = {
slug: previousNode.fields.slug,
title: previousNode.frontmatter.title,
};
}

let nextNodeData = undefined;
const nextNode =
index === edges.length - 1 ? undefined : edges[index + 1].node;
if (nextNode) {
nextNodeData = {
slug: nextNode.fields.slug,
title: nextNode.frontmatter.title,
};
}

let data;
if (!navigationData[section]) {
data = { title, slug, section };
navigationData = { ...navigationData, [section]: [data] };
} else {
data = { title, slug, section };
navigationData = {
...navigationData,
[section]: [...navigationData[section], data],
};
}
docPages.push({
slug,
next: nextNodeData,
previous: previousNodeData,
relativePath,
});
});

docPages.forEach(page => {
createPage({
path: `/${page.slug}`,
component: docTemplate,
context: {
slug: page.slug,
next: page.next,
previous: page.previous,
relativePath: page.relativePath,
navigationData: navigationData,
},
});
if (page.slug === 'introduction-to-nodejs')
createPage({
path: `/`,
component: docTemplate,
context: {
slug: page.slug,
next: page.next,
previous: page.previous,
relativePath: page.relativePath,
navigationData: navigationData,
},
})
});
})
);
});
};

exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === 'MarkdownRemark') {
const { createNodeField } = actions;

const slug = createSlug(node.frontmatter.title);
createNodeField({
node,
name: `slug`,
value: slug,
});

let authors = node.frontmatter.authors;
if (authors) {
authors = authors.split(',');
createNodeField({
node,
name: `authors`,
value: authors,
});
}
}
}
};
47 changes: 35 additions & 12 deletions src/components/article.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
import React from 'react';
import { RemarkPage, PageInfo } from '../types';
import { PaginationInfo } from '../types';
import Pagination from './pagination';
import EditLink from './edit-link';
import AuthorLink from './author-link';

type Props = {
page: RemarkPage;
previous?: PageInfo;
next?: PageInfo;
}
title: string;
html: string;
authors: string[];
relativePath: string;
next?: PaginationInfo;
previous?: PaginationInfo;
};

const Article = ({ page, previous, next }: Props) => (
const Article = ({
title,
html,
previous,
next,
relativePath,
authors,
}: Props) => (
<article className="article-reader">
<h1 className="article-reader__headline">{page.frontmatter.title}</h1>
<div dangerouslySetInnerHTML={{ __html: page.html }} />
<EditLink relativePath={page.parent.relativePath} />
<Pagination previous={previous} next={next}/>
<h1 className="article-reader__headline">{title}</h1>
<div dangerouslySetInnerHTML={{ __html: html }} />
<div
style={{
display: 'flex',
flexWrap: 'wrap',
marginTop: '5rem',
alignItems: 'center',
}}>
Contributors:
{authors && authors.map(author => (
author && <AuthorLink username={author} key={author}/>
))}
</div>
<EditLink relativePath={relativePath} />
<Pagination previous={previous} next={next} />
</article>
)
);

export default Article
export default Article;
21 changes: 21 additions & 0 deletions src/components/author-link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';

type Props = {
username: string;
key: string;
};

const AuthorLink = ({ username }: Props) => {
if (!username) {
return null;
}
const githubLink = `https://github.com/${username}`;

return (
<a style={{ marginLeft: '0.5rem' }} href={githubLink}>
{username}
</a>
);
};

export default AuthorLink;
12 changes: 6 additions & 6 deletions src/components/edit-link.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';

type Props = {
relativePath?: string
}
relativePath?: string;
};

const EditLink = ({ relativePath }: Props) => {
if (!relativePath) {
Expand All @@ -12,10 +12,10 @@ const EditLink = ({ relativePath }: Props) => {
const href = `https://github.com/nodejs/nodejs.dev/edit/master/src/documentation/${relativePath}`;

return (
<a href={href} >
<a style={{ display: 'inlineFlex', marginTop: '1rem' }} href={href}>
Edit this page on GitHub
</a>
)
}
);
};

export default EditLink
export default EditLink;
3 changes: 2 additions & 1 deletion src/components/navigation-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useRef, useEffect } from 'react';
import { Link } from 'gatsby';

type Props = {
key: string;
isDone: boolean;
isActive: boolean;
slug: string;
Expand Down Expand Up @@ -33,7 +34,7 @@ const NavigationItem = ({ isDone, isActive, slug, title, onClick }: Props) => {
}

return (
<Link ref={handleRef} to={`/learn/${slug}`} onClick={onClick} className={className}>
<Link ref={handleRef} to={`/${slug}`} onClick={onClick} className={className}>
{title}
</Link>
);
Expand Down
Loading