From 93ba6939c1412ea684c5e096907c9df14985579e Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 7 Feb 2018 18:22:07 +0100 Subject: [PATCH] Update API --- src/components/About.js | 3 +- src/server.js | 2 +- src/utils/api.js | 14 +++++++- src/utils/compiler.js | 71 ++++++++++++++++++++++++++++++++++------- src/utils/scanner.js | 37 +++++++++++++++------ 5 files changed, 102 insertions(+), 25 deletions(-) diff --git a/src/components/About.js b/src/components/About.js index 49a0706..3780585 100644 --- a/src/components/About.js +++ b/src/components/About.js @@ -13,7 +13,8 @@ export default class About extends Component { return (
- { this.props.about.hello } +
+
) } diff --git a/src/server.js b/src/server.js index 7c5b50d..ed8fe85 100644 --- a/src/server.js +++ b/src/server.js @@ -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)); diff --git a/src/utils/api.js b/src/utils/api.js index 1c995df..87c85b8 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -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) => { diff --git a/src/utils/compiler.js b/src/utils/compiler.js index e87c161..5c7b011 100644 --- a/src/utils/compiler.js +++ b/src/utils/compiler.js @@ -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); diff --git a/src/utils/scanner.js b/src/utils/scanner.js index 60842a5..fc07238 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -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,