Update API

This commit is contained in:
LordMathis 2018-02-07 18:22:07 +01:00
parent 397d3f2811
commit 93ba6939c1
5 changed files with 102 additions and 25 deletions

View File

@ -13,7 +13,8 @@ export default class About extends Component {
return (
<div className="content-wrapper">
{ this.props.about.hello }
<div className="content" dangerouslySetInnerHTML={{__html: this.props.about.body}}>
</div>
</div>
)
}

View File

@ -9,7 +9,7 @@ require('css-modules-require-hook')({
var fs = require('fs');
var filename = './src/utils/data.json';
var dataStub = {"posts": []};
var dataStub = {"posts": [], "other": []};
fs.writeFileSync(filename, JSON.stringify(dataStub));

View File

@ -2,13 +2,25 @@ const data = require('./data.json');
const api = require('express').Router();
const fs = require('fs');
const path = require('path');
const config = require('./config.json');
api.get('/blog', (req, res) => {
res.json(data.posts);
});
api.get('/about', (req, res) => {
res.json({"hello": "hello"});
const renderPath = path.join(process.cwd(), '/renders', 'about.html');
fs.readFile(renderPath, 'utf8', (err, data) => {
if (err) {
res.json({
error: 404
});
} else {
res.json({
body: data,
});
}
});
});
api.get('/post/:postname', (req, res) => {

View File

@ -7,11 +7,17 @@ const async = require('async');
const fm = require('front-matter');
const config = require('../utils/config.json');
/**
* Renders file using MarkdownIt
*/
function render(file) {
const md = new MarkdownIt({html: true});
return md.render(file);
}
/**
* Extracts file metadata such as parent directory
*/
function fileMetadata(filepath) {
const paths = filepath.split('/');
const basename = path.basename(filepath);
@ -26,7 +32,10 @@ function fileMetadata(filepath) {
return metadata;
}
function compile(filepath, data, fileData, callback) {
/**
* Compiles file that is a blog post
*/
function compilePost(filepath, data, fileData, callback) {
const frontMatter = fm(fileData);
const rendered = render(frontMatter.body);
const metadata = fileMetadata(filepath);
@ -54,26 +63,64 @@ function compile(filepath, data, fileData, callback) {
});
}
/**
* Compiles other types of files such as resumes, about me and so on.
*/
function compileOther(filepath, data, fileData, callback) {
const frontMatter = fm(fileData);
const rendered = render(frontMatter.body);
const metadata = fileMetadata(filepath);
const post = {
filename: metadata.filename
}
const renderedpath = path.join(process.cwd(), config.renderPath, `${metadata.filename}.html`);
fs.writeFile(renderedpath, rendered, (err) => {
if (err) callback(err);
else callback(null, post);
});
}
function Compiler(data) {
this.data = data;
}
Compiler.prototype.addFile = function(filepath, addToData, callback) {
async.waterfall([
fs.readFile.bind(fs, filepath, 'utf8'),
compile.bind(compile, filepath, this.data),
], (err, result) => {
if (err) throw err;
/**
*
*/
Compiler.prototype.addFile = function(filepath, isPost, callback) {
if (isPost) {
async.waterfall([
fs.readFile.bind(fs, filepath, 'utf8'),
compilePost.bind(compilePost, filepath, this.data),
], (err, result) => {
if (err) throw err;
if (addToData) {
this.data.posts.push(result);
}
console.log("[Compiler] File %s compiled", filepath);
callback();
});
console.log("[Compiler] File %s compiled", filepath);
callback();
});
} else {
async.waterfall([
fs.readFile.bind(fs, filepath, 'utf8'),
compileOther.bind(compileOther, filepath, this.data),
], (err, result) => {
if (err) throw err;
this.data.other.push(result);
console.log("[Compiler] File %s compiled", filepath);
callback();
});
}
};
/**
* Writes updated data to the data file
*/
Compiler.prototype.writeData = function(callback) {
const dataPath = path.join(process.cwd(), 'src/utils/data.json');
jsonfile.writeFile(dataPath, this.data, callback);

View File

@ -9,20 +9,16 @@ module.exports = function() {
var compiler = new Compiler(data);
/**
* Reads the directory and returns it's content
*/
function readdir(callback) {
fs.readdir(config.contentPath, callback);
}
function compileFile(file, callback) {
const filePath = path.join(process.cwd(), config.contentPath, file);
if (config.files.indexOf(file) !== -1) {
compiler.addFile(filePath, false, callback);
} else {
compiler.addFile(filePath, true, callback);
}
}
/**
* Calls compile on each file in the directory
*/
function compile(files, callback) {
console.log("[Scanner] Discovered files: " + files);
async.each(files, compileFile, (err) => {
@ -31,10 +27,31 @@ module.exports = function() {
});
}
/**
* Helper function which calls compile in the Compiler module
*/
function compileFile(file, callback) {
const filePath = path.join(process.cwd(), config.contentPath, file);
// config.files contains list of file names which are not considered blog posts
if (config.files.indexOf(file) == -1) {
compiler.addFile(filePath, true, callback);
} else {
compiler.addFile(filePath, false, callback);
}
}
/**
* Writes updated data into the data file
*/
function writeData(callback) {
compiler.writeData(callback);
}
/**
* Main function. Scans the directory for files and compiles them into html
* using the Compiler module
*/
async.waterfall([
readdir,
compile,