From 7243e5ec322669998c64a5fba66337a81e7e46d7 Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 27 May 2020 13:16:23 -0700 Subject: [PATCH] inserted directory exists check when rendering md and html, completed index and page html rendering sequence chain --- brain/data/Book.js | 174 +++++++++++++++++----------------- brain/data/Render.js | 64 ++++++++----- brain/routes/dash/settings.js | 2 +- 3 files changed, 129 insertions(+), 111 deletions(-) diff --git a/brain/data/Book.js b/brain/data/Book.js index 160dffe..2479761 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -82,95 +82,97 @@ export default class Book { '/' + moment(body.created).format('MM') + '/' - ); - if (body.layout !== 'page') layout = body.layout; - if (body.layout === null || body.layout === 'null') layout = 'page'; - var pageWrite = - '---\n' + - 'id: ' + - body.id + - '\n' + - 'uuid: ' + - body.uuid + - '\n' + - 'title: ' + - body.title + - '\n' + - 'feature: ' + - body.feature + - '\n' + - 'path: ' + - moment(body.created).format('YYYY') + - '/' + - moment().format('MM') + - '\n' + - 'layout: ' + - layout + - '\n' + - 'tags: ' + - body.tags + - '\n' + - 'author: ' + - user.handle + - '\n' + - 'created: ' + - moment(body.created).format() + - '\n' + - 'updated: ' + - moment(Date.now()).format() + - '\n' + - 'deleted: ' + - body.deleted + - '\n' + - 'menu: ' + - body.menu + - '\n' + - 'featured: ' + - body.featured + - '\n' + - 'published: ' + - body.published + - '\n' + - 'slug: ' + - body.slug + - '\n' + - '---\n' + - body.content; - layout === 'index' - ? (path = 'content/pages/' + body.slug + '.md') - : (path = - 'content/pages/' + - moment(body.created).format('YYYY') + - '/' + - moment(body.created).format('MM') + - '/' + - body.slug + - '.md'); - fs.writeFile(path, pageWrite, err => { - // throws an error, you could also catch it here + ).then(() => { + if (body.layout !== 'page') layout = body.layout; + if (body.layout === null || body.layout === 'null') layout = 'page'; + var pageWrite = + '---\n' + + 'id: ' + + body.id + + '\n' + + 'uuid: ' + + body.uuid + + '\n' + + 'title: ' + + body.title + + '\n' + + 'feature: ' + + body.feature + + '\n' + + 'path: ' + + moment(body.created).format('YYYY') + + '/' + + moment().format('MM') + + '\n' + + 'layout: ' + + layout + + '\n' + + 'tags: ' + + body.tags + + '\n' + + 'author: ' + + user.handle + + '\n' + + 'created: ' + + moment(body.created).format() + + '\n' + + 'updated: ' + + moment(Date.now()).format() + + '\n' + + 'deleted: ' + + body.deleted + + '\n' + + 'menu: ' + + body.menu + + '\n' + + 'featured: ' + + body.featured + + '\n' + + 'published: ' + + body.published + + '\n' + + 'slug: ' + + body.slug + + '\n' + + '---\n' + + body.content; + layout === 'index' + ? (path = 'content/pages/' + body.slug + '.md') + : (path = + 'content/pages/' + + moment(body.created).format('YYYY') + + '/' + + moment(body.created).format('MM') + + '/' + + body.slug + + '.md'); + fs.writeFile(path, pageWrite, err => { + // throws an error, you could also catch it here - if (err) { - response = { type: DataEvent.PAGE_ERROR, message: err }; - reject(response); - } + if (err) { + response = { type: DataEvent.PAGE_ERROR, message: err }; + reject(response); + } - // success case, the file was saved - if (task === DataEvent.API_PAGE_CREATE) { - // if new file, update settings index and page count - response = { - type: DataEvent.PAGE_ADDED, - message: 'New Page Created', - id: body.uuid - }; - resolve(response); - } else { - response = { - type: DataEvent.PAGE_UPDATED, - message: 'Page saved. Nice Work' - }; - resolve(response); - } + // success case, the file was saved + if (task === DataEvent.API_PAGE_CREATE) { + // if new file, update settings index and page count + response = { + type: DataEvent.PAGE_ADDED, + message: 'New Page Created', + id: body.uuid + }; + resolve(response); + } else { + response = { + type: DataEvent.PAGE_UPDATED, + message: 'Page saved. Nice Work' + }; + resolve(response); + } + }); }); + break; case DataEvent.API_PAGE_DELETE: this.getPage(id) diff --git a/brain/data/Render.js b/brain/data/Render.js index 0b36543..a0dbe79 100644 --- a/brain/data/Render.js +++ b/brain/data/Render.js @@ -7,6 +7,7 @@ const tags_list = require('../../site/tags.json'); const pug = require('pug'); const md = require('markdown-it')('commonmark'); const _ = require('lodash'); +const moment = require('moment'); export default class Render { //-------------------------- @@ -28,32 +29,47 @@ export default class Render { let response = []; for (let index = 0; index < pages.length; index++) { const page = pages[index]; + let writeFile, template; + let path = + 'public/' + + moment(page.metadata.created).format('YYYY') + + '/' + + moment(page.metadata.created).format('MM') + + '/'; if (page.metadata.layout === 'index') { - let buffed = sanitize(page.content, { - allowedTags: ['del', 'a', 'iframe', 'img'], - allowedAttributes: { - a: ['href', 'name', 'target'], - img: ['src'], - iframe: [ - 'height', - 'width', - 'src', - 'frameborder', - 'allow', - 'allowfullscreen' - ] - } - }); + template = 'content/themes/' + theme + '/index.pug'; + writeFile = 'public/index.html'; + } else { + writeFile = path + page.metadata.slug + '.html'; + template = 'content/themes/' + theme + '/page.pug'; + } - buffed = new StringUtils().decodeHTML(buffed); - let html = md.render(buffed, { html: true, xhtmlOut: true }); - let file = pug.renderFile('content/themes/' + theme + '/index.pug', { - title: page.metadata.title, - default_bg: page.metadata.feature, - content_index: html - }); + let buffed = sanitize(page.content, { + allowedTags: ['del', 'a', 'iframe', 'img'], + allowedAttributes: { + a: ['href', 'name', 'target'], + img: ['src'], + iframe: [ + 'height', + 'width', + 'src', + 'frameborder', + 'allow', + 'allowfullscreen' + ] + } + }); - fs.writeFile('public/index.html', file, err => { + buffed = new StringUtils().decodeHTML(buffed); + let html = md.render(buffed, { html: true, xhtmlOut: true }); + let file = pug.renderFile(template, { + title: page.metadata.title, + default_bg: page.metadata.feature, + content: html + }); + + fs.ensureDir(path).then(() => { + fs.writeFile(writeFile, file, err => { // throws an error, you could also catch it here if (err) { @@ -68,7 +84,7 @@ export default class Render { }; resolve(response); }); - } + }); } }); } diff --git a/brain/routes/dash/settings.js b/brain/routes/dash/settings.js index ffa273a..8d62951 100644 --- a/brain/routes/dash/settings.js +++ b/brain/routes/dash/settings.js @@ -11,7 +11,7 @@ router.get('/', function (req, res) { .then(obj => { settings = obj; }) - .catch(err => { + .catch(() => { //console.error(err); }); loadThemes().then(themes => {