Skip to content

Commit ede1cbd

Browse files
committed
add /articles/:article_id to get an article in html
/articles/:article_id/body returns just the body, rendered as html
1 parent a2e2457 commit ede1cbd

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["es2015"]
2+
"presets": ["es2015", "react"]
33
}

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ app.get('/articles/:articleID', function(req, res) {
1010
res.sendFile(path.join(__dirname, 'static/label.html'));
1111
});
1212

13+
app.get('/articles/:articleID/body', function(req, res) {
14+
res.sendFile(path.join(__dirname, 'static/article.html'));
15+
});
16+
1317
app.get('/bookmarklet/js', function(req, res) {
1418
res.redirect(301, '/bookmarklet.js');
1519
});

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "feedreader-website",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
4+
"engines": {
5+
"node": "6.3.1",
6+
"npm": "4.0.5"
7+
},
48
"description": "What happens at feedreader.co",
59
"main": "index.js",
610
"scripts": {
@@ -20,12 +24,14 @@
2024
"homepage": "https://github.com/feedreaderco/web#readme",
2125
"dependencies": {
2226
"express": "^4.13.4",
23-
"webpack": "^1.12.13"
24-
},
25-
"devDependencies": {
27+
"html-to-react": "^1.2.4",
28+
"react": "^15.5.3",
29+
"react-dom": "^15.5.3",
30+
"webpack": "^1.14.0",
2631
"babel-core": "^6.24.1",
2732
"babel-loader": "^6.4.1",
2833
"babel-preset-es2015": "^6.24.1",
29-
"webpack": "^1.14.0"
34+
"babel-preset-react": "^6.24.1",
35+
"json-loader": "^0.5.4"
3036
}
3137
}

src/article.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { Component } from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { Parser } from 'html-to-react';
4+
import api from './api';
5+
6+
const pathname_split = window.location.pathname.split('/');
7+
const hash = pathname_split[pathname_split.length - 2];
8+
const mountNode = document.getElementById('articles');
9+
10+
class Article extends Component {
11+
constructor(props) {
12+
super(props);
13+
this.state = { article: {} };
14+
}
15+
16+
componentWillMount() {
17+
const uri = `articles/${this.props.id}`;
18+
api('GET', uri, ({ article }) => this.setState({ article }));
19+
}
20+
21+
render() {
22+
if (!this.state.article.description) return <div></div>;
23+
const parser = new Parser();
24+
const Body = parser.parse(this.state.article.description);
25+
return <div id="article">{Body}</div>;
26+
}
27+
}
28+
29+
ReactDOM.render(<Article id={hash} />, mountNode);

static/article.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
3+
<meta name="viewport" content="width=device-width, initial-scale=1" />
4+
<link rel='stylesheet' type='text/css' href='https://feedreader.co/style.css'>
5+
<title>Feed Reader</title>
6+
<body>
7+
<div id=articles>
8+
</div>
9+
</body>
10+
<script src='https://feedreader.co/article.js' type='text/javascript'></script>

webpack.config.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ module.exports = {
77
userpage: './src/userpage.js',
88
login: './src/login.js',
99
signup: './src/signup.js',
10-
bookmarklet: './src/bookmarklet.js'
10+
bookmarklet: './src/bookmarklet.js',
11+
article: './src/article.js'
1112
},
1213
output: {
1314
path: path.join(__dirname, 'static'),
1415
filename: '[name].js'
1516
},
1617
module: {
17-
loaders: [{
18-
exclude: /node_modules/,
19-
loader: 'babel-loader'
20-
}]
18+
loaders: [
19+
{
20+
test: /\.js$/,
21+
exclude: /node_modules/,
22+
loader: 'babel-loader'
23+
},
24+
{
25+
test: /\.json$/,
26+
loader: 'json-loader'
27+
},
28+
]
2129
}
2230
};

0 commit comments

Comments
 (0)