Skip to content

Commit f45579d

Browse files
j0k3rleebyron
authored andcommitted
Add RSS feed for blog post (graphql#40)
I needed React >= 0.14.0 to avoid self-closing tag in RSS (mostly for the `<link />` node). Feel free to jump to any most up to date version. I've also hacked around to be able to generate a xml file instead of html.
1 parent 269de4c commit f45579d

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"babel-runtime": "^5.8.20",
2626
"express": "^4.13.3",
2727
"js-yaml": "^3.4.0",
28-
"react": "^0.13.3",
28+
"react": "^0.14.3",
2929
"sane": "^1.2.0",
3030
"webpack": "^1.12.1"
3131
},

resources/Site.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async function readSite(siteRoot) {
2424

2525
await fileWalker(site.root, (absPath, stat) => {
2626
var relPath = path.relative(site.root, absPath);
27+
2728
return readFileData(absPath, relPath, stat).then(data => {
2829
data = normalizeData(data);
2930
var dirName = path.dirname(relPath);
@@ -50,7 +51,7 @@ function buildSite(buildRoot, site) {
5051

5152

5253

53-
var PAGEISH = [ '.html.js', '.md', '.markdown' ];
54+
var PAGEISH = [ '.html.js', '.xml.js', '.md', '.markdown' ];
5455

5556
function isPageish(filePath) {
5657
for (var i = 0; i < PAGEISH.length; i++) {
@@ -72,6 +73,7 @@ function readFileData(absPath, relPath, stat) {
7273
}
7374
return readFile(absPath).then(content => {
7475
var frontMatter = FRONT_MATTER_RX.exec(content);
76+
7577
if (!frontMatter) {
7678
return { absPath, relPath, stat, content };
7779
}
@@ -109,9 +111,14 @@ function urlToFile(file) {
109111
}
110112
} else {
111113
url = '/' + file.relPath;
112-
for (var i = 0; i < PAGEISH.length; i++) {
113-
if (endsWith(url, PAGEISH[i])) {
114-
url = url.slice(0, -PAGEISH[i].length) + '.html';
114+
115+
if (endsWith(file.relPath, '.xml.js')) {
116+
url = url.slice(0, -'.js'.length);
117+
} else {
118+
for (var i = 0; i < PAGEISH.length; i++) {
119+
if (endsWith(url, PAGEISH[i])) {
120+
url = url.slice(0, -PAGEISH[i].length) + '.html';
121+
}
115122
}
116123
}
117124
}

resources/renderReactPage.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function renderReactPage(options) {
2323

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

26+
if (html.indexOf('<feed') !== -1) {
27+
return '<?xml version="1.0" encoding="utf-8"?>' + html;
28+
}
29+
2630
// Assert correct return
2731
if (html.indexOf('</body></html') === -1) {
2832
throw new Error(

resources/writer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function writer(buildDir, file, site) {
3333
var data;
3434

3535
// Render JS file
36-
if (endsWith(file.relPath, '.html.js')) {
36+
if (endsWith(file.relPath, '.html.js') || endsWith(file.relPath, '.xml.js')) {
3737
data = renderReactPage({
3838
component: require(path.resolve(file.absPath)),
3939
props: { site, page: file }

site/blog/rss.xml.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
var path = require('path');
10+
var React = require('react');
11+
12+
var BlogRss = React.createClass({
13+
render: function() {
14+
var site = this.props.site;
15+
var posts = site.files.blog
16+
.filter(file => !file.draft && path.extname(file.relPath) === '.md')
17+
.sort((a, b) => a.date < b.date);
18+
return (
19+
<feed xmlns="http://www.w3.org/2005/Atom">
20+
<title>Blog | GraphQL</title>
21+
<link href="http://graphql.org/blog/"/>
22+
23+
{posts.map(post =>
24+
<entry>
25+
<title>{post.title}</title>
26+
<link href={'http://graphql.org' + post.url}/>
27+
<id>{post.permalink}</id>
28+
<updated>{new Date(post.date).toISOString()}</updated>
29+
<summary>{post.title}</summary>
30+
<content>{post.title}</content>
31+
<author>
32+
<name>{post.byline}</name>
33+
</author>
34+
</entry>
35+
)}
36+
</feed>
37+
);
38+
}
39+
});
40+
41+
module.exports = BlogRss;

0 commit comments

Comments
 (0)