forked from kriasoft/react-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
48 lines (38 loc) · 1.36 KB
/
content.js
File metadata and controls
48 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*! React Starter Kit | MIT License | http://www.reactstarterkit.com/ */
import { join } from 'path';
import { Router } from 'express';
import jade from 'jade';
import fm from 'front-matter';
import fs from '../utils/fs';
// A folder with Jade/Markdown/HTML content pages
const CONTENT_DIR = join(__dirname, './content');
// Extract 'front matter' metadata and generate HTML
const parseJade = (path, jadeContent) => {
const fmContent = fm(jadeContent);
const htmlContent = jade.render(fmContent.body);
return Object.assign({ path, content: htmlContent }, fmContent.attributes);
};
const router = new Router();
router.get('/', async (req, res, next) => {
try {
const path = req.query.path;
if (!path || path === 'undefined') {
res.status(400).send({error: `The 'path' query parameter cannot be empty.`});
return;
}
let fileName = join(CONTENT_DIR, (path === '/' ? '/index' : path) + '.jade');
if (!await fs.exists(fileName)) {
fileName = join(CONTENT_DIR, path + '/index.jade');
}
if (!await fs.exists(fileName)) {
res.status(404).send({error: `The page '${path}' is not found.`});
} else {
const source = await fs.readFile(fileName, { encoding: 'utf8' });
const content = parseJade(path, source);
res.status(200).send(content);
}
} catch (err) {
next(err);
}
});
export default router;