This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Refactoring Site Creation and Additions #157
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1fdc16a
chore: Authors and section fields added.
96e0f6f
fix: Style fix for edit-link.
c5e3356
chore: Author link component added.
69ce7bd
refactor: Site creation moved to gatsby-config, nav refactored.
33c407d
fix: Mistakes in docs revoked.
d9f4307
chore: Remove comments.
1fd74b5
fix: Authorlink wont be rendered without author data.
b6881ac
chore: Tests updated, tests for new components added.
193ea11
chore: Remove /learn path.
c41980f
fix: Coding style fixes.
9b0e891
chore: Tests updated.
9548e20
fix: Linter issues fixed
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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; | ||
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, | ||
}); | ||
} | ||
} | ||
} | ||
}; |
This file contains hidden or 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,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; |
This file contains hidden or 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 |
---|---|---|
@@ -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; |
This file contains hidden or 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 hidden or 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.
nit: Can we initialize it with
null
instead ofundefined
. That way, the intent is more explicit — assigning a deliberate non-value.Or if we need it to be initialized with
undefined
, we can just leave it uninitialized.+same throughout.