diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index ecc0a75..926c99f 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -1,6 +1,7 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; import Auth from '../../data/Auth'; import Render from '../../data/Render'; +import Book from '../../data/Book'; const express = require('express'); const router = express.Router(); const multer = require('multer'); @@ -10,6 +11,8 @@ const _ = require('lodash'); const settings = require('../../../site/settings.json'); const folks = require('../../../site/folks.json'); const auth = new Auth(); +const render = new Render(); +const book = new Book(); const uploadPath = './public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM'); fs.ensureDir(uploadPath, () => { @@ -113,13 +116,23 @@ router.post('/nav-sync', (req, res) => { router.post('/publish-pages', (req, res) => { auth.authCheck(req) .then(() => { - new Render() - .publishAll(settings.global.theme) - .then(response => { - res.json({ - type: response.type, - message: response.message - }); + book.getPage() + .then(pages => { + render + .publishAll(pages, 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({ diff --git a/brain/app.js b/brain/app.js index 3d682de..1dd6319 100644 --- a/brain/app.js +++ b/brain/app.js @@ -7,7 +7,6 @@ var bodyParser = require('body-parser'); var session = require('express-session'); var MemoryStore = require('memorystore')(session); var flash = require('connect-flash'); -var theme = 'default-dark'; var app = express(); //var request = require('request'); // view engine setup @@ -64,7 +63,7 @@ app.use('/@/dashboard/settings', settings); app.use('/@/dashboard/navigation', nav); //app.use('/mailer', mailer); // catch 404 and forward to error handler -app.use(function(req, res, next) { +app.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); @@ -73,7 +72,7 @@ app.use(function(req, res, next) { // development error handler // will print stacktrace if (app.get('env') === 'development') { - app.use(function(err, req, res) { + app.use(function (err, req, res) { res.status(err.status || 500); res.render('error', { message: err.message, @@ -83,7 +82,7 @@ if (app.get('env') === 'development') { } // production error handler // no stacktraces leaked to user -app.use(function(err, req, res) { +app.use(function (err, req, res) { res.status(err.status || 500); res.render('error', { message: err.message, diff --git a/brain/data/Book.js b/brain/data/Book.js index 622b752..018e860 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -6,15 +6,18 @@ import * as DataEvent from '../../src/com/events/DataEvent'; import Navigation from './Navigation'; import Settings from './Settings'; import StringUtils from '../../src/com/utils/StringUtils'; +import Render from './Render'; +const moment = require('moment'); const nav = new Navigation(); const settings = new Settings(); const utils = new StringUtils(); -const moment = require('moment'); +const render = new Render(); + /** * Class for handling blog content pages */ -export default class Pages { +export default class Book { //-------------------------- // constructor //-------------------------- @@ -23,7 +26,6 @@ export default class Pages { // methods //-------------------------- start() {} - /** * Retrieves single page or pages * @parameter id: optional id if requesting a single Page @@ -187,6 +189,7 @@ export default class Pages { * Retrieves single page or pages * @parameter pages: payload of pages */ + organizeTags(pages) { let tags = []; for (let index = 0; index < pages.length; index++) { @@ -194,25 +197,30 @@ export default class Pages { let temp = []; temp = page.metadata.tags.split(','); for (let i = 0; i < temp.length; i++) { - let label = temp[i]; - if (!_.find(tags, { tag_name: label.trim() })) { + let label = temp[i].trim(); + if (!_.find(tags, { tag_name: label })) { tags.push({ - tag_name: label.trim(), - slug: utils.cleanString(label.trim()), + tag_name: label, + slug: utils.cleanString(label), count: 1 }); } else { - _.find(tags, { tag_name: label.trim() }).count++; + _.find(tags, { tag_name: label }).count++; } } } tags = _.orderBy(tags, ['tag_name'], ['asc']); - settings - .saveTags(tags) - .then(result => { - console.log('RESPONSE', result); - }) - .catch(err => {}); + settings.saveTags(tags).then(response => { + console.log('RESPONSE', response); + render + .publishTags(pages) + .then(response => { + console.log(response); + }) + .catch(err => { + console.log(err); + }); + }); } //-------------------------- diff --git a/brain/data/Render.js b/brain/data/Render.js index fde2c80..a481112 100644 --- a/brain/data/Render.js +++ b/brain/data/Render.js @@ -1,9 +1,11 @@ import * as DataEvent from '../../src/com/events/DataEvent'; -import sanitize from 'sanitize-html'; import StringUtils from '../../src/com/utils/StringUtils'; import fs from 'fs-extra'; +import sanitize from 'sanitize-html'; +const config = require('../../site/settings.json'); const pug = require('pug'); const md = require('markdown-it')('commonmark'); +const _ = require('lodash'); export default class Render { //-------------------------- @@ -17,61 +19,101 @@ export default class Render { /** * Renders all pages from markdown to html + * @parameter pages: payload for site pages * @parameter theme: current theme being used as defined in settings */ - publishAll(theme) { + publishAll(pages, theme) { return new Promise((resolve, reject) => { - this.getPage() - .then(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, - default_bg: page.metadata.feature, - content_index: 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); - }); + 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, + default_bg: page.metadata.feature, + content_index: 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); + }); + } + } + }); + } + publishTags(pages) { + return new Promise((resolve, reject) => { + let tags = config.tags; + let renderList = []; + for (let index = 0; index < tags.length; index++) { + let tag = tags[index]; + //console.log('**TAG**', tag.tag_name); + var pageList = []; + for (let i = 0; i < pages.length; i++) { + let page = pages[i]; + + if (_.includes(page.metadata.tags, tag.tag_name)) { + pageList.push({ title: page.metadata.title, slug: page.metadata.slug }); } - }) - .catch(err => { - reject(err); + } + renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug }); + } + let response = []; + for (let index = 0; index < renderList.length; index++) { + let item = renderList[index]; + let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', { + title: item.tag, + default_bg: config.global.background, + content_tags: 'THESE ARE TAGS', + tag_list: item.tag_list }); + + fs.writeFile('public/tags/' + item.slug + '.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); + }); + } }); } diff --git a/brain/data/Settings.js b/brain/data/Settings.js index 4a144f3..407937d 100644 --- a/brain/data/Settings.js +++ b/brain/data/Settings.js @@ -1,6 +1,6 @@ import * as DataEvent from '../../src/com/events/DataEvent'; import fs from 'fs-extra'; -const settings = require('../../site/settings.json'); +const config = require('../../site/settings.json'); const _ = require('lodash'); export default class Settings { @@ -14,9 +14,9 @@ export default class Settings { start() {} saveTags(tags) { return new Promise((resolve, reject) => { - if (!_.isEqual(settings.tags, tags)) { - settings.tags = tags; - fs.writeJson('site/settings.json', settings) + if (!_.isEqual(config.tags, tags)) { + config.tags = tags; + fs.writeJson('site/settings.json', config) .then(() => { let response = { type: DataEvent.SETTINGS_UPDATED, diff --git a/brain/routes/dash/index.js b/brain/routes/dash/index.js index f3785bb..323f51a 100644 --- a/brain/routes/dash/index.js +++ b/brain/routes/dash/index.js @@ -1,13 +1,13 @@ import Book from '../../data/Book'; const express = require('express'); const router = express.Router(); -const settings = require('../../../site/settings.json'); +const config = require('../../../site/settings.json'); +const book = new Book(); //-------------------------- // Index //-------------------------- -router.get('/', function(req, res) { - let book = new Book(); +router.get('/', function (req, res) { book.getPage().then(result => { result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id)); let indexPages = []; @@ -18,9 +18,9 @@ router.get('/', function(req, res) { }); let pageData = []; if (req.session.user) { - pageData = { title: settings.global.title, status: true, pages: indexPages }; + pageData = { title: config.global.title, status: true, pages: indexPages }; } else { - pageData = { title: settings.global.title, status: false, pages: indexPages }; + pageData = { title: config.global.title, status: false, pages: indexPages }; } res.render('index', pageData); @@ -30,7 +30,7 @@ router.get('/', function(req, res) { //-------------------------- // Logout //-------------------------- -router.post('/logout', function(req, res) { +router.post('/logout', function (req, res) { req.logout(); return res.json({ message: 'LOGGED OUT'