From 38cfc1fc6c313fa2272b9f8f8161843092ef6f9f Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 6 May 2020 14:43:53 -0700 Subject: [PATCH] basic page rendering working index --- brain/api/v1/settings.js | 22 +++++++++--- brain/data/Book.js | 53 +++++++++++++++++++++++++--- brain/routes/dash/settings.js | 9 ++--- src/com/controllers/SettingsIndex.js | 2 +- src/com/events/DataEvent.js | 2 ++ 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index e2b8c18..3508d63 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -1,5 +1,6 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; import Auth from '../../data/Auth'; +import Book from '../../data/Book'; const express = require('express'); const router = express.Router(); const multer = require('multer'); @@ -112,11 +113,21 @@ router.post('/nav-sync', (req, res) => { router.post('/publish-pages', (req, res) => { auth.authCheck(req) .then(() => { - console.log('PUBLISHING'); - res.json({ - type: DataEvent.API_RENDER_PAGES, - message: 'All Pages Rendered and Published' - }); + new Book() + .publish(settings.global.theme) + .then(response => { + res.json({ + type: response.type, + message: response.message + }); + }) + .catch(err => { + res.json({ + type: DataEvent.PAGES_NOT_RENDERED, + message: 'Uh oh. Pages not rendered, sport', + error: err + }); + }); }) .catch(err => { res.json({ @@ -140,6 +151,7 @@ router.post('/add-avatar', avatar_upload, (req, res) => { var image = path.substr(7, path.length); folks[index].avi = '/' + image; fs.writeJson('site/folks.json', folks); + user.avi = '/' + image; res.json({ type: DataEvent.AVATAR_UPLOADED, message: 'Changed avi. You look great.', diff --git a/brain/data/Book.js b/brain/data/Book.js index 897502d..c745d09 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -1,11 +1,15 @@ import fh from 'filehound'; import fs from 'fs-extra'; +import sanitize from 'sanitize-html'; import metadataParser from 'markdown-yaml-metadata-parser'; import _ from 'lodash'; import * as DataEvent from '../../src/com/events/DataEvent'; import Navigation from './Navigation'; +import StringUtils from '../../src/com/utils/StringUtils'; const nav = new Navigation(); const moment = require('moment'); +const pug = require('pug'); +const md = require('markdown-it')('commonmark'); export default class Pages { //-------------------------- @@ -173,13 +177,54 @@ export default class Pages { } }); } - publish() { + publish(theme) { return new Promise((resolve, reject) => { - //let self = this; - //get pages for rendering this.getPage() .then(pages => { - resolve(pages); + let response = []; + for (let index = 0; index < pages.length; index++) { + const page = pages[index]; + 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' + ] + } + }); + + 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, + content: html + }); + + fs.writeFile('public/index.html', file, err => { + // throws an error, you could also catch it here + + if (err) { + response = { type: DataEvent.PAGES_NOT_RENDERED, message: err }; + reject(response); + } + + // success case, the file was saved + response = { + type: DataEvent.PAGES_RENDERED, + message: 'All Pages Rendered. Sweet.' + }; + resolve(response); + }); + } + } }) .catch(err => { reject(err); diff --git a/brain/routes/dash/settings.js b/brain/routes/dash/settings.js index be91ea8..ffa273a 100644 --- a/brain/routes/dash/settings.js +++ b/brain/routes/dash/settings.js @@ -6,13 +6,13 @@ var settings = []; //-------------------------- // SETTINGS //-------------------------- -router.get('/', function(req, res) { +router.get('/', function (req, res) { fs.readJson('site/settings.json') .then(obj => { settings = obj; }) - .catch(() => { - console.error(err); + .catch(err => { + //console.error(err); }); loadThemes().then(themes => { if (req.session.user) { @@ -23,11 +23,12 @@ router.get('/', function(req, res) { email: user.email, avi: user.avi }); - themes.sort(function(a, b) { + themes.sort(function (a, b) { var textA = a.theme.name.toUpperCase(); var textB = b.theme.name.toUpperCase(); return textA < textB ? -1 : textA > textB ? 1 : 0; }); + res.render('settings', { title: 'Dashboard | Settings', welcome: 'Your Settings', diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 4c71c34..88c524a 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -190,7 +190,7 @@ export default class SettingsIndex { ) .then(response => { let r = JSON.parse(response.request['response']); - console.log('RESPONSE', r); + notify.alert(r.message, true); }) .catch(err => { notify.alert(err, false); diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index aa0f6fb..f2db784 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -11,6 +11,8 @@ export const PAGE_ERROR = 'postError'; export const PAGE_ADDED = 'postAdded'; export const PAGE_UPDATED = 'postUpdated'; export const PAGE_DELETED = 'postImageAdded'; +export const PAGES_RENDERED = 'pagesRendered'; +export const PAGES_NOT_RENDERED = 'pagesNotRendered'; export const SETTINGS_UPDATED = 'settingsUpdated'; export const MENU_ADD_ITEM = 'menuAddItem'; export const MENU_DELETE_ITEM = 'menuDeleteItem';