Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate gatsby 1.0 to 2.0 #94

Merged
merged 10 commits into from Feb 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/outline.yaml
@@ -1,5 +1,5 @@
chapters:
- title: 'The Distribution: Create Powerful APIs with Ease'
- title: "The Distribution: Create Powerful APIs with Ease"
path: distribution
items:
- id: index
Expand All @@ -20,6 +20,7 @@ chapters:
- id: data-providers
- id: data-persisters
- id: mercure
- id: mongodb
- id: elasticsearch
- id: pagination
- id: events
Expand Down
4 changes: 2 additions & 2 deletions gatsby-config.js
Expand Up @@ -8,6 +8,8 @@ module.exports = {
siteUrl: process.env.GATSBY_ROOT_URL,
},
plugins: [
'gatsby-plugin-react-helmet',
'gatsby-plugin-sass',
{
resolve: 'gatsby-source-filesystem',
options: {
Expand All @@ -34,8 +36,6 @@ module.exports = {
},
},
'gatsby-plugin-catch-links',
'gatsby-plugin-react-helmet',
'gatsby-plugin-sass',
'gatsby-plugin-sitemap',
'gatsby-plugin-twitter',
{
Expand Down
216 changes: 57 additions & 159 deletions gatsby-node.js
@@ -1,198 +1,96 @@
const Path = require('path');
const path = require('path');
const URL = require('url');
const { createFilePath } = require(`gatsby-source-filesystem`);
const jsyaml = require('js-yaml');
const { readFileSync } = require('fs');
const navHelper = require('./src/lib/navHelper');

const nav = jsyaml.safeLoad(readFileSync(`${__dirname}/src/pages/docs/nav.yml`, 'utf8'));
const nav = jsyaml.safeLoad(readFileSync('./src/pages/docs/nav.yml', 'utf8'));

exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage, createRedirect } = boundActionCreators;
const docTemplate = Path.resolve('src/templates/doc.js');
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const docPageTemplate = path.resolve('src/templates/doc.js');

const docQuery = graphql(`
return graphql(`
{
allMarkdownRemark(sort: { order: DESC, fields: [fields___path] }, limit: 1000) {
allMarkdownRemark(limit: 1000) {
edges {
node {
html
tableOfContents
headings {
depth
value
}
fields {
path
redirect
slug
}
excerpt(pruneLength: 250)
html
id
}
}
}
}
`);

return docQuery.then(values => {
const docs = values.data.allMarkdownRemark.edges;
const parseNav = navHelper.parseNavItem(nav.chapters.filter(navItem => navItem.items));

docs.forEach(edge => {
const path = edge.node.fields.path;
const redirect = edge.node.fields.redirect;
const index = parseNav.findIndex(element => element.path === path || element.path === redirect);
let current, prev, next, rootPath;

const headings = edge.node.headings;
const html = edge.node.html;

const regex = RegExp(/<a href="#((?:[\w-]+(?:-[\w-]+)*)+)" aria-hidden="true" class="anchor">/, 'gm');
let tmpResult;
let result = [];

while ((tmpResult = regex.exec(html)) !== null) {
result.push(tmpResult[1]);
}
`).then(result => {
if (result.errors) {
throw result.errors;
}

headings.forEach((currentVal, index) => {
if (currentVal.depth !== 1) {
return;
}
if (index > 0) {
console.warn(
'\x1b[31m',
`\nMultiple title in single file are not allowed, please change heading node of following title: '${
currentVal.value
}' in ${path}.md\n`,
'\x1b[37m'
);
process.exit(1);
const pages = result.data.allMarkdownRemark.edges;

pages.forEach(edge => {
const slug = edge.node.fields.slug;

let previous = {};
let next = {};

const slugArray = slug.split('/');

nav.chapters.forEach(chapter => {
if (slugArray[2] === chapter.path) {
chapter.items.forEach((item, index) => {
if (index === 0) {
next.slug = `/docs/${slugArray[2]}/${chapter.items[index + 1].id}/`;
next.title = chapter.items[index + 1].title;
} else if (slugArray[3] === item.id) {
Copy link
Member

Choose a reason for hiding this comment

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

Return early instead of using an else if?

previous.slug =
chapter.items[index - 1].id === 'index'
? `/docs/${slugArray[2]}/`
: `/docs/${slugArray[2]}/${chapter.items[index - 1].id}/`;
previous.title = chapter.items[index - 1].title;
if (chapter.items.length - 1 !== index) {
next.slug = `/docs/${slugArray[2]}/${chapter.items[index + 1].id}/`;
next.title = chapter.items[index + 1].title;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Return early instead of using else?

next.slug = null;
}
}
});
}
});

if (headings.length !== result.length) {
console.warn(
'\x1b[31m',
`There is an unexpected diff between number of headers and number of header anchors in ${path}.md, report to gastby-node.js file to figure out why.\n`,
'\x1b[37m'
);
process.exit(1);
return;
}

if (-1 !== index) {
current = parseNav[index];
rootPath = current.rootPath;

prev = 0 < index && parseNav[index - 1];
next = index < parseNav.length - 1 && parseNav[index + 1];

if (prev && prev.rootPath !== rootPath) {
prev = { path: prev.path, title: `${prev.rootPath} - ${prev.title}` };
}
if (next && next.rootPath !== rootPath) {
next = { path: next.path, title: `${next.rootPath} - ${next.title}` };
}

current = {
path: current.path,
title: `${current.rootPath} - ${current.title}`,
};
}

if (next && next.path) {
next.path = next.path.replace(/\/index$/, '/');
}

if (prev && prev.path) {
prev.path = prev.path.replace(/\/index$/, '/');
}

let editSubPaths = path.split('/');
editSubPaths.shift();
const editPath = editSubPaths.length > 2 ? `${editSubPaths.join('/').slice(0, -1)}.md` : `${editSubPaths[0]}/index.md`;
const editSubPaths = slug.slice(6).split('/');
const editPath =
editSubPaths.length > 2 ? `${editSubPaths.join('/').slice(0, -1)}.md` : `${editSubPaths[0]}/index.md`;

createPage({
path,
component: docTemplate,
path: slug,
component: docPageTemplate,
context: {
html: edge.node.html,
editPath,
current,
prev,
title: edge.node.headings[0].value,
previous,
next,
html,
nav: nav.chapters,
},
});

const redirects = [`/${path.slice(0, -1)}`];
if (redirect) {
redirects.push(`/${redirect}`, `/${redirect}/`);
}
redirects.forEach(redirPath =>
createRedirect({
fromPath: redirPath,
toPath: `/${path}`,
isPermanent: true,
redirectInBrowser: true,
})
);
});
});
};

exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators;
if ('MarkdownRemark' !== node.internal.type) {
return;
}
const fileNode = getNode(node.parent);
let nodePath = fileNode.relativePath.replace('.md', '');
let html = node.internal.content;
let localUrls = [];
let matches;
const regex = /(\]\((?!http)(?!#)(.*?)\))/gi;

while ((matches = regex.exec(html))) {
localUrls.push(matches[2]);
}

localUrls.map(url => {
let newUrl = url.replace('.md', '/');
newUrl = `/${URL.resolve(nodePath, newUrl)}`;
html = html.replace(url, newUrl);
return true;
});

node.internal.content = html;
if ('index' === Path.basename(nodePath)) {
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
createNodeField({
node,
name: 'redirect',
value: nodePath,
name: `slug`,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
name: `slug`,
name: 'slug',

value: slug,
});
nodePath = `${Path.dirname(nodePath)}`;
}

createNodeField({
node,
name: 'path',
value: `${nodePath}/`,
});
};

exports.modifyWebpackConfig = ({ config }) => {
config.merge({
resolve: {
root: Path.resolve(__dirname, './src'),
alias: {
styles: 'styles',
images: 'images',
data: 'data',
components: 'components',
},
},
});
return config;
};
51 changes: 27 additions & 24 deletions package.json
Expand Up @@ -3,34 +3,37 @@
"license": "MIT",
"dependencies": {
"classnames": "^2.2.5",
"dotenv": "^4.0.0",
"dotenv": "^6.2.0",
"eject": "^0.0.4",
"gatsby": "^1.9.80",
"gatsby-link": "^1.6.23",
"gatsby-plugin-google-analytics": "^1.0.12",
"gatsby-plugin-manifest": "^1.0.8",
"gatsby-plugin-offline": "^1.0.10",
"gatsby-plugin-react-helmet": "^1.0.8",
"gatsby": "^2.0.91",
"gatsby-plugin-google-analytics": "^2.0.9",
"gatsby-plugin-manifest": "^2.0.13",
"gatsby-plugin-offline": "^2.0.21",
"gatsby-plugin-react-helmet": "^3.0.5",
"gatsby-plugin-remove-serviceworker": "^1.0.0",
"gatsby-plugin-sass": "^1.0.12",
"gatsby-plugin-sharp": "^1.6.48",
"gatsby-plugin-sitemap": "^1.2.5",
"gatsby-plugin-twitter": "^1.0.10",
"gatsby-remark-external-links": "^0.0.3",
"gatsby-remark-prismjs": "^2.0",
"gatsby-remark-relative-images": "^0.1.2",
"gatsby-transformer-sharp": "^1.6.14",
"gatsby-transformer-yaml": "^1.5.11",
"gatsby-plugin-sass": "^2.0.7",
"gatsby-plugin-sharp": "^2.0.17",
"gatsby-plugin-sitemap": "^2.0.4",
"gatsby-plugin-twitter": "^2.0.8",
"gatsby-remark-external-links": "^0.0.4",
"gatsby-remark-prismjs": "^3.2.0",
"gatsby-remark-relative-images": "^0.2.1",
"gatsby-transformer-sharp": "^2.1.10",
"gatsby-transformer-yaml": "^2.1.7",
"h5o": "^0.11.3",
"husky": "^1.0.0-rc.13",
"jsdom": "^11.11.0",
"node-sass": "^4.0.0",
"normalize.css": "^7.0.0",
"prismjs": "^1.13",
"react": "^16.7.0",
"react-async-script-loader": "^0.3.0",
"react-collapsible": "^2.0.3",
"react-dom": "^16.7.0",
"react-helmet": "^5.2.0",
"react-html-parser": "^2.0.1"
"react-html-parser": "^2.0.1",
"react-router-dom": "^4.3.1",
"webpack": "^4.28.1"
},
"devDependencies": {
"babel-eslint": "^8.0.1",
Expand All @@ -42,14 +45,14 @@
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-prettier": "^2.6.2",
"eslint-plugin-react": "^7.2.1",
"gatsby-plugin-catch-links": "^1.0.9",
"gatsby-remark-autolink-headers": "^1.4.4",
"gatsby-remark-copy-images": "^0.2.0",
"gatsby-remark-copy-linked-files": "^1.5.2",
"gatsby-remark-images": "^1.5.67",
"gatsby-plugin-catch-links": "^2.0.9",
"gatsby-remark-autolink-headers": "^2.0.12",
"gatsby-remark-copy-images": "^0.2.1",
"gatsby-remark-copy-linked-files": "^2.0.8",
"gatsby-remark-images": "^3.0.1",
"gatsby-remark-responsive-image": "^1.0.0-alpha13-alpha.435e0178",
"gatsby-source-filesystem": "^1.4.3",
"gatsby-transformer-remark": "^1.7.2",
"gatsby-source-filesystem": "^2.0.13",
"gatsby-transformer-remark": "^2.2.0",
"git-pre-commit": "^2.1.4",
"js-yaml": "^3.10.0",
"lint-staged": "^7.2.0",
Expand Down