-
Notifications
You must be signed in to change notification settings - Fork 3
Static site generation for GitHub pages #18
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "presets": ["env", "react"], | ||
| "plugins": ["transform-react-jsx"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| .DS_Store | ||
| .DS_Store | ||
| node_modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| const React = require("react"); | ||
| const ReactMarkdown = require("react-markdown"); | ||
|
|
||
| const capitalise = string => { | ||
| if (string === "aws") return string.toUpperCase(); | ||
|
Contributor
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. In terms of future-proofing, this could be an array of names?
Contributor
Author
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. Good shout, will do |
||
|
|
||
| return `${string.charAt(0).toUpperCase()}${string.slice(1)}`; | ||
| }; | ||
|
|
||
| const Sidebar = ({ files, latest }) => ( | ||
| <aside className="sticky"> | ||
| <a href="#"> | ||
| <img src="./dual-masthead.svg" /> | ||
| </a> | ||
| <ul className="categories"> | ||
| {Object.keys(files).map((folder, index) => { | ||
| const documents = files[folder]; | ||
| if (documents.length === 0) return null; | ||
|
|
||
| return ( | ||
| <li key={index}> | ||
| <a href={`#${folder}`}>{folder}</a> | ||
| </li> | ||
| ); | ||
| })} | ||
| </ul> | ||
|
|
||
| <div className="latestWrapper"> | ||
| <h2>Latest additions</h2> | ||
| <ul className="latest"> | ||
| {latest.map((f, index) => ( | ||
| <li key={index}> | ||
| <a href={`#${f.name}`}>{f.headline}</a> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| </aside> | ||
| ); | ||
|
|
||
| const Content = ({ files }) => ( | ||
| <main> | ||
| <header> | ||
| <a | ||
| className="github-button" | ||
| href="https://github.com/times/learning" | ||
| data-icon="octicon-star" | ||
| data-size="large" | ||
| data-show-count="true" | ||
| aria-label="Star times/learning on GitHub" | ||
| > | ||
| Star | ||
| </a> | ||
|
|
||
| <h1>🎓 Learning</h1> | ||
| <p>Useful posts, articles, videos and podcasts related to development</p> | ||
| </header> | ||
|
|
||
| {Object.keys(files).map((folder, index) => { | ||
| const documents = files[folder]; | ||
| if (documents.length === 0) return null; | ||
|
|
||
| return ( | ||
| <section key={index} id={folder}> | ||
| <h2>{capitalise(folder)}</h2> | ||
| {documents.map(({ name, content }, index) => ( | ||
| <div className="markdown" key={index} id={name}> | ||
| <ReactMarkdown source={content} /> | ||
| </div> | ||
| ))} | ||
| </section> | ||
| ); | ||
| })} | ||
| </main> | ||
| ); | ||
|
|
||
| const App = ({ files, latest }) => ( | ||
| <div className="wrapper"> | ||
| <Sidebar files={files} latest={latest} /> | ||
| <Content files={files} /> | ||
| </div> | ||
| ); | ||
|
|
||
| module.exports = App; | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| const fs = require("fs"); | ||
| const React = require("react"); | ||
| const ReactDOMServer = require("react-dom/server"); | ||
|
|
||
| const App = require("./app"); | ||
|
|
||
| const getDirectories = (path, ignore = []) => | ||
| fs | ||
| .readdirSync(path) | ||
| .filter(a => fs.statSync(`${path}${a}`).isDirectory()) | ||
| .filter(a => !ignore.includes(a)); | ||
|
|
||
| const getFiles = (path, extension, ignore = []) => | ||
| fs | ||
| .readdirSync(path) | ||
| .filter(a => fs.statSync(`${path}${a}`).isFile()) | ||
| .filter(a => a.endsWith(extension)) | ||
| .filter(a => !ignore.includes(a)) | ||
| .map(a => { | ||
| const content = fs.readFileSync(`${path}${a}`).toString(); | ||
| return { | ||
| name: a, | ||
| content, | ||
| headline: content.match(/# \[(.*?)\]/)[1], | ||
| dateAdded: fs.statSync(`${path}${a}`).birthtime | ||
|
Contributor
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.
Contributor
Author
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. Seemed the most appropriate from this list |
||
| }; | ||
| }); | ||
|
|
||
| const directories = getDirectories("./", [".git", "node_modules"]); | ||
|
|
||
| const markdownFiles = directories.reduce( | ||
| (acc, s) => | ||
| Object.assign({}, acc, { | ||
| [s]: getFiles(`./${s}/`, ".md") | ||
| }), | ||
| {} | ||
| ); | ||
|
|
||
| const latestFiles = directories | ||
| .reduce((acc, s) => [...acc, ...getFiles(`./${s}/`, ".md")], []) | ||
| .sort((a, b) => { | ||
| const diff = a.dateAdded - b.dateAdded; | ||
| if (diff === 0) return diff; | ||
| return diff > 0 ? -1 : 1; | ||
| }) | ||
| .splice(0, 3); | ||
|
Contributor
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. It's fine, honestly, but technically
Contributor
Author
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. Damn, I think I meant |
||
|
|
||
| const html = ReactDOMServer.renderToStaticMarkup( | ||
| <App files={markdownFiles} latest={latestFiles} /> | ||
| ); | ||
|
|
||
| fs.writeFileSync( | ||
| "./index.html", | ||
| `<html> | ||
| <head> | ||
| <title>Learning!</title> | ||
|
|
||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
|
||
| <link rel="stylesheet" href="./style.css" /> | ||
| <link rel="stylesheet" href="https://fonts.timesdev.tools/fonts/TimesModern-Bold.css" /> | ||
| <link rel="stylesheet" href="https://fonts.timesdev.tools/fonts/TimesModern-Regular.css" /> | ||
| </head> | ||
| <body> | ||
| ${html} | ||
|
|
||
| <script async defer src="https://buttons.github.io/buttons.js"></script> | ||
| <script src="node_modules/stickyfilljs/dist/stickyfill.min.js"></script> | ||
| <script type="text/javascript"> | ||
| Stickyfill.add(document.querySelectorAll('.sticky')); | ||
| </script> | ||
| </body> | ||
| </html>` | ||
| ); | ||
|
|
||
| process.exit(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "learning", | ||
| "version": "1.0.0", | ||
| "description": | ||
| "Useful posts, articles, videos and podcasts related to development", | ||
| "main": "index.js", | ||
| "repository": "git@github.com:times/learning.git", | ||
| "author": "Chris Hutchinson <chris.hutchinson@thetimes.co.uk>", | ||
| "license": "MIT", | ||
| "private": false, | ||
| "scripts": { | ||
| "build": "./node_modules/babel-cli/bin/babel-node.js index.js" | ||
| }, | ||
| "dependencies": { | ||
| "babel-cli": "^6.26.0", | ||
| "babel-core": "^6.26.0", | ||
| "babel-plugin-transform-react-jsx": "^6.24.1", | ||
| "babel-preset-env": "^1.6.1", | ||
| "babel-preset-react": "^6.24.1", | ||
| "react": "^16.1.1", | ||
| "react-dom": "^16.1.1", | ||
| "react-markdown": "^3.0.1", | ||
| "stickyfilljs": "^2.0.3" | ||
| } | ||
| } |
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.
Don't think we've discussed as a team what our default
prettiersettings should be – the EB version (i.e. single quotes, trailing commas) or the tool defaults? Either way we should maybe add a.pretterrcto this repoThere 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.
Oh sorry, I totally meant to add one - I'll do that