Skip to content

Add RSS feed for blog posts #40

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

Merged
merged 1 commit into from
Aug 23, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"babel-runtime": "^5.8.20",
"express": "^4.13.3",
"js-yaml": "^3.4.0",
"react": "^0.13.3",
"react": "^0.14.3",
"sane": "^1.2.0",
"webpack": "^1.12.1"
},
Expand Down
15 changes: 11 additions & 4 deletions resources/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function readSite(siteRoot) {

await fileWalker(site.root, (absPath, stat) => {
var relPath = path.relative(site.root, absPath);

return readFileData(absPath, relPath, stat).then(data => {
data = normalizeData(data);
var dirName = path.dirname(relPath);
Expand All @@ -50,7 +51,7 @@ function buildSite(buildRoot, site) {



var PAGEISH = [ '.html.js', '.md', '.markdown' ];
var PAGEISH = [ '.html.js', '.xml.js', '.md', '.markdown' ];

function isPageish(filePath) {
for (var i = 0; i < PAGEISH.length; i++) {
Expand All @@ -72,6 +73,7 @@ function readFileData(absPath, relPath, stat) {
}
return readFile(absPath).then(content => {
var frontMatter = FRONT_MATTER_RX.exec(content);

if (!frontMatter) {
return { absPath, relPath, stat, content };
}
Expand Down Expand Up @@ -109,9 +111,14 @@ function urlToFile(file) {
}
} else {
url = '/' + file.relPath;
for (var i = 0; i < PAGEISH.length; i++) {
if (endsWith(url, PAGEISH[i])) {
url = url.slice(0, -PAGEISH[i].length) + '.html';

if (endsWith(file.relPath, '.xml.js')) {
url = url.slice(0, -'.js'.length);
} else {
for (var i = 0; i < PAGEISH.length; i++) {
if (endsWith(url, PAGEISH[i])) {
url = url.slice(0, -PAGEISH[i].length) + '.html';
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions resources/renderReactPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function renderReactPage(options) {

var html = React.renderToStaticMarkup(React.createElement(component, props));

if (html.indexOf('<feed') !== -1) {
return '<?xml version="1.0" encoding="utf-8"?>' + html;
}

// Assert correct return
if (html.indexOf('</body></html') === -1) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion resources/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function writer(buildDir, file, site) {
var data;

// Render JS file
if (endsWith(file.relPath, '.html.js')) {
if (endsWith(file.relPath, '.html.js') || endsWith(file.relPath, '.xml.js')) {
data = renderReactPage({
component: require(path.resolve(file.absPath)),
props: { site, page: file }
Expand Down
41 changes: 41 additions & 0 deletions site/blog/rss.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

var path = require('path');
var React = require('react');

var BlogRss = React.createClass({
render: function() {
var site = this.props.site;
var posts = site.files.blog
.filter(file => !file.draft && path.extname(file.relPath) === '.md')
.sort((a, b) => a.date < b.date);
return (
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Blog | GraphQL</title>
<link href="http://graphql.org/blog/"/>

{posts.map(post =>
<entry>
<title>{post.title}</title>
<link href={'http://graphql.org' + post.url}/>
<id>{post.permalink}</id>
<updated>{new Date(post.date).toISOString()}</updated>
<summary>{post.title}</summary>
<content>{post.title}</content>
<author>
<name>{post.byline}</name>
</author>
</entry>
)}
</feed>
);
}
});

module.exports = BlogRss;