cleaned up package scripts, added tag sorting to settings

This commit is contained in:
Ro 2020-05-19 13:51:04 -07:00
parent 4ff6f22e25
commit fad2c4bc8d
6 changed files with 159 additions and 66 deletions

View file

@ -1,6 +1,6 @@
import * as DataEvent from '../../../src/com/events/DataEvent'; import * as DataEvent from '../../../src/com/events/DataEvent';
import Auth from '../../data/Auth'; import Auth from '../../data/Auth';
import Book from '../../data/Book'; import Render from '../../data/Render';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const multer = require('multer'); const multer = require('multer');
@ -113,8 +113,8 @@ router.post('/nav-sync', (req, res) => {
router.post('/publish-pages', (req, res) => { router.post('/publish-pages', (req, res) => {
auth.authCheck(req) auth.authCheck(req)
.then(() => { .then(() => {
new Book() new Render()
.publish(settings.global.theme) .publishAll(settings.global.theme)
.then(response => { .then(response => {
res.json({ res.json({
type: response.type, type: response.type,

View file

@ -1,15 +1,16 @@
import fh from 'filehound'; import fh from 'filehound';
import fs from 'fs-extra'; import fs from 'fs-extra';
import sanitize from 'sanitize-html';
import metadataParser from 'markdown-yaml-metadata-parser'; import metadataParser from 'markdown-yaml-metadata-parser';
import _ from 'lodash'; import _ from 'lodash';
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import Navigation from './Navigation'; import Navigation from './Navigation';
import StringUtils from '../../src/com/utils/StringUtils'; import Settings from './Settings';
const nav = new Navigation(); const nav = new Navigation();
const settings = new Settings();
const moment = require('moment'); const moment = require('moment');
const pug = require('pug'); /**
const md = require('markdown-it')('commonmark'); * Class for handling blog content pages
*/
export default class Pages { export default class Pages {
//-------------------------- //--------------------------
@ -26,6 +27,7 @@ export default class Pages {
* @parameter id: optional id if requesting a single Page * @parameter id: optional id if requesting a single Page
*/ */
getPage(id) { getPage(id) {
var self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fh.create() fh.create()
.paths('content/pages') .paths('content/pages')
@ -38,10 +40,10 @@ export default class Pages {
pages.push(metadataParser(file)); pages.push(metadataParser(file));
}); });
} }
if (id === null || id === null || id === undefined) { if (id === null || id === null || id === undefined) {
setTimeout(() => { setTimeout(() => {
//TODO: Duct tape solution until something better created //TODO: Duct tape solution until something better created
self.organizeTags(pages);
resolve(pages); resolve(pages);
}, 100); }, 100);
} else { } else {
@ -180,64 +182,32 @@ export default class Pages {
}); });
} }
/** /**
* Renders all pages from markdown to html * Retrieves single page or pages
* @parameter theme: current theme being used as defined in settings * @parameter pages: payload of pages
*/ */
publish(theme) { organizeTags(pages) {
return new Promise((resolve, reject) => { let tags = [];
this.getPage()
.then(pages => {
let response = [];
for (let index = 0; index < pages.length; index++) { for (let index = 0; index < pages.length; index++) {
const page = pages[index]; const page = pages[index];
if (page.metadata.layout === 'index') { let temp = [];
let buffed = sanitize(page.content, { temp = page.metadata.tags.split(',');
allowedTags: ['del', 'a', 'iframe', 'img'], for (let i = 0; i < temp.length; i++) {
allowedAttributes: { if (!_.find(tags, { tag_name: temp[i] })) {
a: ['href', 'name', 'target'], tags.push({ tag_name: temp[i], count: 1 });
img: ['src'], } else {
iframe: [ _.find(tags, { tag_name: temp[i] }).count++;
'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);
});
} }
} }
}
tags = _.orderBy(tags, ['tag_name'], ['asc']);
settings
.saveTags(tags)
.then(result => {
console.log('RESPONSE', result);
}) })
.catch(err => { .catch(err => {});
reject(err);
});
});
} }
//-------------------------- //--------------------------
// event handlers // event handlers
//-------------------------- //--------------------------

81
brain/data/Render.js Normal file
View file

@ -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
//--------------------------
}

43
brain/data/Settings.js Normal file
View file

@ -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
//--------------------------
}

View file

@ -11,8 +11,6 @@
"dev": "nodemon -r esm init.js --ignore node_modules/ -e js", "dev": "nodemon -r esm init.js --ignore node_modules/ -e js",
"debug": "nodemon inspect -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}}", "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", "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" "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"
}, },

View file

@ -14,6 +14,7 @@ export const PAGE_DELETED = 'postImageAdded';
export const PAGES_RENDERED = 'pagesRendered'; export const PAGES_RENDERED = 'pagesRendered';
export const PAGES_NOT_RENDERED = 'pagesNotRendered'; export const PAGES_NOT_RENDERED = 'pagesNotRendered';
export const SETTINGS_UPDATED = 'settingsUpdated'; export const SETTINGS_UPDATED = 'settingsUpdated';
export const SETTINGS_NOT_UPDATED = 'settingsNotUpdated';
export const MENU_ADD_ITEM = 'menuAddItem'; export const MENU_ADD_ITEM = 'menuAddItem';
export const MENU_DELETE_ITEM = 'menuDeleteItem'; export const MENU_DELETE_ITEM = 'menuDeleteItem';
export const MENU_UPDATED = 'menuUpdated'; export const MENU_UPDATED = 'menuUpdated';