Created
May 20, 2018 18:37
-
-
Save sudarshang/69305d604eda0b2cd6746edfe681fe59 to your computer and use it in GitHub Desktop.
react-static moo.do blog config.js (copy pasted from react-static slack)
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
import webpack from 'webpack'; | |
import React, { Component } from 'react'; | |
import { reloadRoutes } from 'react-static/node'; | |
import jdown from 'jdown'; | |
import chokidar from 'chokidar'; | |
import path, { dirname } from 'path'; | |
import RSS from 'rss'; | |
import fs from 'fs'; | |
var isDev = process.env.REACT_STATIC_ENV === 'development'; | |
chokidar.watch('content').on('all', () => reloadRoutes()); | |
const args = process.argv.slice(2), | |
showDrafts = args.indexOf('drafts') >= 0; | |
export default { | |
siteRoot: process.env.REACT_STATIC_STAGING ? undefined : '##BR_SITE_ROOT##', | |
getSiteData: () => ({ | |
title: 'Moo.do', | |
}), | |
getRoutes: async () => { | |
var { posts } = await jdown('content'); | |
posts = posts.sort((a, b) => b.date - a.date); | |
var failed = posts.filter(post => !post.title || !post.slug || !post.date); | |
if (failed.length > 0) | |
{ | |
console.log('ERROR IN POST', failed); | |
} | |
if (!showDrafts) | |
{ | |
var drafts = posts.filter(post => post.draft); | |
if (drafts.length > 0) | |
{ | |
console.log('Ignoring drafts:', drafts.length); | |
} | |
posts = posts.filter(post => !post.draft); | |
} | |
posts = posts.map((post) => { | |
post.date = '' + post.date; | |
return post; | |
}); | |
if (!isDev) | |
{ | |
generateRSS(posts); | |
} | |
let postsUpdates = posts.filter(post => post.category === 'updates'), | |
postsIdeas = posts.filter(post => post.category === 'ideas'); | |
return [ | |
{ | |
path: '/', | |
component: 'src/containers/Home', | |
getData: () => ({ | |
id: 'about', | |
}), | |
}, | |
{ | |
path: '/premium', | |
component: 'src/containers/Premium', | |
}, | |
{ | |
path: '/help', | |
component: 'src/containers/Help', | |
getData: () => ({ | |
page: 'basics' | |
}), | |
children: [ | |
{ | |
path: 'basics', | |
component: 'src/containers/Help', | |
getData: () => ({ | |
page: 'basics' | |
}) | |
}, | |
{ | |
path: 'email', | |
component: 'src/containers/Help', | |
getData: () => ({ | |
page: 'email' | |
}) | |
}, | |
] | |
}, | |
{ | |
path: '/us', | |
component: 'src/containers/Us', | |
}, | |
{ | |
path: '/contact', | |
component: 'src/containers/Contact', | |
}, | |
{ | |
path: '/press', | |
component: 'src/containers/Press', | |
}, | |
{ | |
path: '/privacy', | |
component: 'src/containers/Privacy', | |
}, | |
{ | |
path: '/unsubscribe', | |
component: 'src/containers/Unsubscribe', | |
}, | |
{ | |
path: '/purchase', | |
component: 'src/containers/Purchase', | |
}, | |
{ | |
path: '/invoice', | |
component: 'src/containers/Invoice', | |
}, | |
{ | |
path: '/beta', | |
component: 'src/containers/Beta', | |
}, | |
...paginate({ | |
items: posts, | |
path: '/blog', | |
}), | |
...paginate({ | |
items: postsUpdates, | |
path: '/blog/updates', | |
}), | |
...paginate({ | |
items: postsIdeas, | |
path: '/blog/ideas', | |
}), | |
...posts.map(post => | |
{ | |
return { | |
path: `/blog/${post.slug}`, | |
component: 'src/containers/Blog', | |
getData: () => ({ | |
posts: [ post ] | |
}), | |
}; | |
}), | |
{ | |
is404: true, | |
component: 'src/containers/404', | |
}, | |
]; | |
}, | |
Document: class CustomHtml extends Component | |
{ | |
render() | |
{ | |
const { Html, Head, Body, children, renderMeta } = this.props; | |
return ( | |
<Html> | |
<Head> | |
<meta httpEquiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta httpEquiv="X-UA-Compatible" content="IE=edge" /> | |
<title>Moo.do</title> | |
<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, user-scalable=yes" /> | |
<meta name="description" content="##BR_NAME## Application" /> | |
<meta name="robots" content="index, follow" /> | |
<link rel="canonical" href="##BR_URL##/" /> | |
<meta property="og:locale" content="en_US" /> | |
<meta property="og:type" content="website" /> | |
<meta property="og:title" content="##BR_NAME##" /> | |
<meta property="og:description" content="##BR_NAME## Application" /> | |
<meta property="og:site_name" content="##BR_URL##" /> | |
<meta property="og:url" content="##BR_URL##" /> | |
<meta property="og:image" content="##BR_SM_ICO##" /> | |
<link rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/iffimmolghilclfndeiebgppddmagofk" /> | |
<link rel="preload" as="font" href="/landing/css/fonts/landing.woff" type="font/woff" crossOrigin="true" /> | |
<link rel="stylesheet" href="/landing/css/fonticons.css" type="text/css" /> | |
<link rel="stylesheet" href="/landing/css/landing.css" type="text/css" /> | |
<link rel="stylesheet" href="/landing/css/blog.css" type="text/css" /> | |
<link rel="stylesheet" href="/landing/css/about.css" type="text/css" /> | |
<link rel="stylesheet" href="/landing/css/press.css" type="text/css" /> | |
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="true" /> | |
<script src="https://apis.google.com/js/client.js?onload=GoogleApiLoaded" defer async></script> | |
<link rel="image_src" type="image/png" href="##BR_URL##/favicon.ico" /> | |
<link rel="icon" href="##BR_URL##/favicon.ico" type="image/x-icon" /> | |
</Head> | |
<Body>{children}</Body> | |
</Html> | |
); | |
} | |
}, | |
webpack: (config, { defaultLoaders }) => | |
{ | |
config.resolveLoader = { | |
modules: ['node_modules', path.resolve(__dirname, '../webpackLoaders')] | |
} | |
config.module.rules = [ | |
{ | |
oneOf: [ | |
{ | |
test: defaultLoaders.jsLoader.test, | |
exclude: defaultLoaders.jsLoader.exclude, | |
use: defaultLoaders.jsLoader.use.concat([{ loader: 'loaderMoodo?DEBUG' }]) | |
}, | |
defaultLoaders.cssLoader, | |
defaultLoaders.fileLoader, | |
] | |
} | |
]; | |
config.plugins.push(new webpack.DefinePlugin({ | |
DEBUG: isDev | |
})); | |
return config; | |
} | |
}; | |
function paginate({ | |
items, | |
pageSize = 5, | |
pageToken = 'page', | |
path, | |
decorate | |
}) | |
{ | |
const itemsCopy = [...items]; // Make a copy of the items | |
const pages = []; // Make an array for all of the different pages | |
while (itemsCopy.length) | |
{ | |
// Splice out all of the items into separate pages using a set pageSize | |
pages.push(itemsCopy.splice(0, pageSize)); | |
} | |
const num = pages.length; | |
// Move the first page out of pagination. This is so page one doesn't require a page number. | |
const firstPage = pages.shift(); | |
const routes = [ | |
{ | |
path: path, | |
component: 'src/containers/Blog', | |
getData: () => ({ | |
posts: firstPage, | |
numPages: num, | |
path | |
}) | |
}, | |
// map over each page to create an array of page routes, and spread it! | |
...pages.map((page, i) => ({ | |
component: 'src/containers/Blog', | |
path: `${path}/${i + 2}`, | |
getData: () => ({ | |
posts: page, | |
numPages: num, | |
path | |
}) | |
})) | |
] | |
return routes; | |
} | |
var generateRSS = (posts) => | |
{ | |
var feed = new RSS({ | |
title: 'Moo.do', | |
description: 'Moo.do - Organize your way', | |
feed_url: 'https://www.moo.do/blog/rss.xml', | |
site_url: 'https://www.moo.do', | |
image_url: 'https://www.moo.do/img/apple-touch-icon.png', | |
pubDate: new Date().toString() | |
}); | |
posts.map((post) => | |
{ | |
let path = `https://www.moo.do/blog/${post.slug}`, | |
contents = post.contents.replace(/src="\//g, 'src="https://www.moo.do/').replace(/href="\//g, 'href="https://www.moo.do/'); | |
feed.item({ | |
title: post.title, | |
link: path, | |
description: contents, | |
pubDate: new Date(post.date).toString(), | |
guid: path | |
}) | |
}); | |
fs.writeFile(__dirname + '/dist/blog/rss.xml', feed.xml({ indent: true }), (err) => | |
{ | |
if (err) console.log(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment