cleaned up package scripts, added tag sorting to settings

pull/20/head
Ro 5 years ago
parent 4ff6f22e25
commit fad2c4bc8d

@ -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,

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

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

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

@ -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"
},

@ -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';

Loading…
Cancel
Save