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
dunglas
merged 10 commits into
api-platform:master
from
Fabious:feature/migrate-to-gatsby-v2
Feb 13, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
87beccd
migrate gatsby 1.0 to 2.0
Fabious 82dd70d
remove dev relics + fix travis ?
Fabious ce589c2
fix travis for MenuItem + update gatsby-link
Fabious f11a264
fix use of global location
Fabious e84b1ab
linter
Fabious 60e37bd
lint (arrow-body-style)
Fabious 73c7c68
update eslint
Fabious 748ec35
fix mobile scroll when coming from sidemenu
Fabious 71761df
reactivate search bar
Fabious 7fe07e1
add mongodb documentation
Fabious File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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) { | ||||||
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return early instead of using |
||||||
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`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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; | ||||||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?