From fad2c4bc8d61fe41952c8100e4e4c1ae05f78e27 Mon Sep 17 00:00:00 2001 From: Ro Date: Tue, 19 May 2020 13:51:04 -0700 Subject: [PATCH] cleaned up package scripts, added tag sorting to settings --- brain/api/v1/settings.js | 6 +-- brain/data/Book.js | 92 +++++++++++++------------------------ brain/data/Render.js | 81 ++++++++++++++++++++++++++++++++ brain/data/Settings.js | 43 +++++++++++++++++ package.json | 2 - src/com/events/DataEvent.js | 1 + 6 files changed, 159 insertions(+), 66 deletions(-) create mode 100644 brain/data/Render.js create mode 100644 brain/data/Settings.js diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 3508d63..ecc0a75 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -1,6 +1,6 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; import Auth from '../../data/Auth'; -import Book from '../../data/Book'; +import Render from '../../data/Render'; const express = require('express'); const router = express.Router(); const multer = require('multer'); @@ -113,8 +113,8 @@ router.post('/nav-sync', (req, res) => { router.post('/publish-pages', (req, res) => { auth.authCheck(req) .then(() => { - new Book() - .publish(settings.global.theme) + new Render() + .publishAll(settings.global.theme) .then(response => { res.json({ type: response.type, diff --git a/brain/data/Book.js b/brain/data/Book.js index 75907ea..500ab04 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -1,15 +1,16 @@ 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'; +import Settings from './Settings'; const nav = new Navigation(); +const settings = new Settings(); const moment = require('moment'); -const pug = require('pug'); -const md = require('markdown-it')('commonmark'); +/** + * Class for handling blog content pages + */ export default class Pages { //-------------------------- @@ -26,6 +27,7 @@ export default class Pages { * @parameter id: optional id if requesting a single Page */ getPage(id) { + var self = this; return new Promise((resolve, reject) => { fh.create() .paths('content/pages') @@ -38,10 +40,10 @@ export default class Pages { pages.push(metadataParser(file)); }); } - if (id === null || id === null || id === undefined) { setTimeout(() => { //TODO: Duct tape solution until something better created + self.organizeTags(pages); resolve(pages); }, 100); } else { @@ -180,64 +182,32 @@ export default class Pages { }); } /** - * Renders all pages from markdown to html - * @parameter theme: current theme being used as defined in settings + * Retrieves single page or pages + * @parameter pages: payload of pages */ - publish(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); - }); - } - } - }) - .catch(err => { - reject(err); - }); - }); + organizeTags(pages) { + let tags = []; + for (let index = 0; index < pages.length; index++) { + const page = pages[index]; + let temp = []; + temp = page.metadata.tags.split(','); + for (let i = 0; i < temp.length; i++) { + if (!_.find(tags, { tag_name: temp[i] })) { + tags.push({ tag_name: temp[i], count: 1 }); + } else { + _.find(tags, { tag_name: temp[i] }).count++; + } + } + } + tags = _.orderBy(tags, ['tag_name'], ['asc']); + settings + .saveTags(tags) + .then(result => { + console.log('RESPONSE', result); + }) + .catch(err => {}); } + //-------------------------- // event handlers //-------------------------- diff --git a/brain/data/Render.js b/brain/data/Render.js new file mode 100644 index 0000000..fde2c80 --- /dev/null +++ b/brain/data/Render.js @@ -0,0 +1,81 @@ +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'; +const pug = require('pug'); +const md = require('markdown-it')('commonmark'); + +export default class Render { + //-------------------------- + // constructor + //-------------------------- + constructor() {} + //-------------------------- + // methods + //-------------------------- + start() {} + + /** + * Renders all pages from markdown to html + * @parameter theme: current theme being used as defined in settings + */ + publishAll(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); + }); + } + } + }) + .catch(err => { + reject(err); + }); + }); + } + + //-------------------------- + // event handlers + //-------------------------- +} diff --git a/brain/data/Settings.js b/brain/data/Settings.js new file mode 100644 index 0000000..4a144f3 --- /dev/null +++ b/brain/data/Settings.js @@ -0,0 +1,43 @@ +import * as DataEvent from '../../src/com/events/DataEvent'; +import fs from 'fs-extra'; +const settings = require('../../site/settings.json'); +const _ = require('lodash'); + +export default class Settings { + //-------------------------- + // constructor + //-------------------------- + constructor() {} + //-------------------------- + // methods + //-------------------------- + start() {} + saveTags(tags) { + return new Promise((resolve, reject) => { + if (!_.isEqual(settings.tags, tags)) { + settings.tags = tags; + fs.writeJson('site/settings.json', settings) + .then(() => { + let response = { + type: DataEvent.SETTINGS_UPDATED, + message: 'Settings Saved' + }; + resolve(response); + }) + .catch(err => { + reject(err); + }); + } else { + let response = { + type: DataEvent.SETTINGS_NOT_UPDATED, + message: 'Settings Already Saved' + }; + resolve(response); + } + }); + } + + //-------------------------- + // event handlers + //-------------------------- +} diff --git a/package.json b/package.json index be678d8..0c6de05 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,6 @@ "dev": "nodemon -r esm init.js --ignore node_modules/ -e js", "debug": "nodemon inspect -r esm init.js --ignore node_modules/ -e js", "prettier-watch": "npx onchange '**/*.js' -- npx prettier --write {{changed}}", - "watch-front": "stylus -w -m -o themes/$npm_package_theme/assets/css themes/$npm_package_theme/src/styles/base.styl & parcel watch themes/$npm_package_theme/src/com/Start.js --out-dir themes/$npm_package_theme/assets/js --out-file start.min.js --public-url /$npm_package_theme/assets/js", - "build-front-kit": "uglifyjs node_modules/scramble-text/dist/ScrambleText.min.js node_modules/animejs/anime.min.js node_modules/reframe.js/dist/reframe.min.js -c -o themes/$npm_package_theme/assets/js/toolkit.min.js", "watch-back": "stylus -w -m -o public/assets/css src/styles/dash.styl & parcel watch src/com/Start.js --out-dir public/assets/scripts --out-file dash.min.js --public-url /assets/scripts", "build-back-kit": "uglifyjs src/libraries/highlight.pack.js node_modules/sortablejs/Sortable.min.js node_modules/scramble-text/dist/ScrambleText.min.js node_modules/reframe.js/dist/reframe.min.js -c -o public/assets/scripts/dashkit.min.js" }, diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index f2db784..d40c605 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -14,6 +14,7 @@ export const PAGE_DELETED = 'postImageAdded'; export const PAGES_RENDERED = 'pagesRendered'; export const PAGES_NOT_RENDERED = 'pagesNotRendered'; export const SETTINGS_UPDATED = 'settingsUpdated'; +export const SETTINGS_NOT_UPDATED = 'settingsNotUpdated'; export const MENU_ADD_ITEM = 'menuAddItem'; export const MENU_DELETE_ITEM = 'menuDeleteItem'; export const MENU_UPDATED = 'menuUpdated';