From 40c8a2590421f35760f494decd5d39c6fab98370 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Fri, 27 Oct 2017 15:04:45 +0200 Subject: [PATCH] Add compiler --- package.json | 7 +++- src/static/config/config.json | 4 +- src/utils/compiler.js | 70 +++++++++++++++++++++++++++++++++++ yarn.lock | 61 +++++++++++++++++++++++++++++- 4 files changed, 139 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0297a04..6eacc45 100644 --- a/package.json +++ b/package.json @@ -20,12 +20,17 @@ "babel-polyfill": "^6.7.4", "babel-runtime": "^6.6.1", "express": "^4.13.4", + "front-matter": "^2.2.0", "isomorphic-fetch": "^2.2.1", + "jsonfile": "^4.0.0", + "markdown-it": "^8.4.0", + "moment": "^2.19.1", "node-sass": "^4.5.3", "react": "^15.0.1", "react-dom": "^15.0.1", "react-redux": "^4.4.4", - "react-router-dom": "^4.1.1" + "react-router-dom": "^4.1.1", + "step": "^1.0.0" }, "devDependencies": { "babel-core": "^6.7.6", diff --git a/src/static/config/config.json b/src/static/config/config.json index 0838886..72fcf0b 100644 --- a/src/static/config/config.json +++ b/src/static/config/config.json @@ -10,5 +10,7 @@ "github": "https://github.com/LordMathis", "twitter": "https://twitter.com/matus_n", "linkedin": "https://www.linkedin.com/in/mat%C3%BA%C5%A1-n%C3%A1me%C5%A1n%C3%BD-3903b6128/" - } + }, + "contentPath": "./content", + "renderPath": "./renders" } diff --git a/src/utils/compiler.js b/src/utils/compiler.js index d99990f..6296c7f 100644 --- a/src/utils/compiler.js +++ b/src/utils/compiler.js @@ -1,5 +1,75 @@ +const MarkdownIt = require('markdown-it'); +const fs = require('fs'); +const path = require('path'); +const moment = require('moment'); +const jsonfile = require('jsonfile'); +const Step = require('step'); +const fm = require('front-matter'); +const config = require('../static/config.json'); +function readFile(filepath, callback) { + fs.readFile(filepath, 'utf-8', callback); +} + +function writeRenderedFile(renderedpath, result) { + fs.writeFile(renderedpath, result, (err) => { + if (err) throw err; + }); +} + +function writeData(data) { + const dataPath = path.join(process.cwd(), 'server/utils/data.json'); + jsonfile.writeFile(dataPath, data, (err) => { + if (err) throw err; + }); +} + +function render(file) { + const md = new MarkdownIt(); + return md.render(file); +} + +function fileMetadata(filepath) { + const paths = filepath.split('/'); + const basename = path.basename(filepath); + + const metadata = { + basename, + filename: basename.substr(0, basename.lastIndexOf('.')), + parrent: paths[paths.length - 2], + dirname: path.dirname(filepath), + }; + + return metadata; +} const compiler = { + add(filepath) { + Step( + function loadFiles() { + readData(this.parallel()); + readFile(filepath, this.parallel()); + }, + (err, data, fileData) => { + if (err) throw err; + const frontMatter = fm(fileData); + const rendered = render(frontMatter.body); + const metadata = fileMetadata(filepath); + + const post = { + published: moment().format('MMMM Do YYYY'), + filename: metadata.filename, + title: frontMatter.attributes.title, + summary: frontMatter.attributes.summary, + }; + + const renderedpath = path.join(config.renderPath, `${metadata.filename}.html`); + + data.posts.push(post); + writeData(data); + writeRenderedFile(renderedpath, rendered); + } + ); + }, } diff --git a/yarn.lock b/yarn.lock index d0de726..531c576 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1661,6 +1661,10 @@ enhanced-resolve@^3.3.0: object-assign "^4.0.1" tapable "^0.2.7" +entities@~1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0" + errno@^0.1.3: version "0.1.4" resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d" @@ -1691,6 +1695,10 @@ esprima@^2.6.0: version "2.7.3" resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" +esprima@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804" + esutils@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" @@ -1905,6 +1913,12 @@ fresh@0.5.2: version "0.5.2" resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" +front-matter@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-2.2.0.tgz#35205f67522430b1213ef26149ecb068579fe38a" + dependencies: + js-yaml "^3.4.6" + fs-readdir-recursive@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560" @@ -2027,7 +2041,7 @@ globule@^1.0.0: lodash "~4.17.4" minimatch "~3.0.2" -graceful-fs@^4.1.2, graceful-fs@^4.1.4: +graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6: version "4.1.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" @@ -2441,6 +2455,13 @@ js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" +js-yaml@^3.4.6: + version "3.10.0" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + js-yaml@~3.7.0: version "3.7.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80" @@ -2486,6 +2507,12 @@ json5@^0.5.0, json5@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" +jsonfile@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb" + optionalDependencies: + graceful-fs "^4.1.6" + jsonify@~0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" @@ -2531,6 +2558,12 @@ lcid@^1.0.0: dependencies: invert-kv "^1.0.0" +linkify-it@^2.0.0: + version "2.0.3" + resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.0.3.tgz#d94a4648f9b1c179d64fa97291268bdb6ce9434f" + dependencies: + uc.micro "^1.0.1" + load-json-file@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0" @@ -2633,6 +2666,16 @@ map-obj@^1.0.0, map-obj@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d" +markdown-it@^8.4.0: + version "8.4.0" + resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.4.0.tgz#e2400881bf171f7018ed1bd9da441dac8af6306d" + dependencies: + argparse "^1.0.7" + entities "~1.1.1" + linkify-it "^2.0.0" + mdurl "^1.0.1" + uc.micro "^1.0.3" + math-expression-evaluator@^1.2.14: version "1.2.17" resolved "https://registry.yarnpkg.com/math-expression-evaluator/-/math-expression-evaluator-1.2.17.tgz#de819fdbcd84dccd8fae59c6aeb79615b9d266ac" @@ -2644,6 +2687,10 @@ md5.js@^1.3.4: hash-base "^3.0.0" inherits "^2.0.1" +mdurl@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e" + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -2762,6 +2809,10 @@ mixin-object@^2.0.1: dependencies: minimist "0.0.8" +moment@^2.19.1: + version "2.19.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.19.1.tgz#56da1a2d1cbf01d38b7e1afc31c10bcfa1929167" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" @@ -4008,6 +4059,10 @@ stdout-stream@^1.4.0: dependencies: readable-stream "^2.0.1" +step@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/step/-/step-1.0.0.tgz#b300e9d2ae9057d4d78633aae2303813a94bdff2" + stream-browserify@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.1.tgz#66266ee5f9bdb9940a4e4514cafb43bb71e5c9db" @@ -4200,6 +4255,10 @@ ua-parser-js@^0.7.9: version "0.7.14" resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca" +uc.micro@^1.0.1, uc.micro@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.3.tgz#7ed50d5e0f9a9fb0a573379259f2a77458d50192" + uglify-js@^2.8.27: version "2.8.29" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"