fixed page rendering bug after switching methodology to class, TAG PAGES ARE RENDERING BITCH

pull/20/head
Ro 5 years ago
parent 8e365216ea
commit 050dfb0b66

@ -1,6 +1,7 @@
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 Render from '../../data/Render'; import Render from '../../data/Render';
import Book from '../../data/Book';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const multer = require('multer'); const multer = require('multer');
@ -10,6 +11,8 @@ const _ = require('lodash');
const settings = require('../../../site/settings.json'); const settings = require('../../../site/settings.json');
const folks = require('../../../site/folks.json'); const folks = require('../../../site/folks.json');
const auth = new Auth(); const auth = new Auth();
const render = new Render();
const book = new Book();
const uploadPath = const uploadPath =
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM'); './public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
fs.ensureDir(uploadPath, () => { fs.ensureDir(uploadPath, () => {
@ -113,8 +116,10 @@ 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 Render() book.getPage()
.publishAll(settings.global.theme) .then(pages => {
render
.publishAll(pages, settings.global.theme)
.then(response => { .then(response => {
res.json({ res.json({
type: response.type, type: response.type,
@ -129,6 +134,14 @@ router.post('/publish-pages', (req, res) => {
}); });
}); });
}) })
.catch(err => {
res.json({
type: DataEvent.PAGES_NOT_RENDERED,
message: 'Uh oh. Pages not rendered, sport',
error: err
});
});
})
.catch(err => { .catch(err => {
res.json({ res.json({
type: err.type, type: err.type,

@ -7,7 +7,6 @@ var bodyParser = require('body-parser');
var session = require('express-session'); var session = require('express-session');
var MemoryStore = require('memorystore')(session); var MemoryStore = require('memorystore')(session);
var flash = require('connect-flash'); var flash = require('connect-flash');
var theme = 'default-dark';
var app = express(); var app = express();
//var request = require('request'); //var request = require('request');
// view engine setup // view engine setup
@ -64,7 +63,7 @@ app.use('/@/dashboard/settings', settings);
app.use('/@/dashboard/navigation', nav); app.use('/@/dashboard/navigation', nav);
//app.use('/mailer', mailer); //app.use('/mailer', mailer);
// catch 404 and forward to error handler // 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'); var err = new Error('Not Found');
err.status = 404; err.status = 404;
next(err); next(err);
@ -73,7 +72,7 @@ app.use(function(req, res, next) {
// development error handler // development error handler
// will print stacktrace // will print stacktrace
if (app.get('env') === 'development') { if (app.get('env') === 'development') {
app.use(function(err, req, res) { app.use(function (err, req, res) {
res.status(err.status || 500); res.status(err.status || 500);
res.render('error', { res.render('error', {
message: err.message, message: err.message,
@ -83,7 +82,7 @@ if (app.get('env') === 'development') {
} }
// production error handler // production error handler
// no stacktraces leaked to user // no stacktraces leaked to user
app.use(function(err, req, res) { app.use(function (err, req, res) {
res.status(err.status || 500); res.status(err.status || 500);
res.render('error', { res.render('error', {
message: err.message, message: err.message,

@ -6,15 +6,18 @@ import * as DataEvent from '../../src/com/events/DataEvent';
import Navigation from './Navigation'; import Navigation from './Navigation';
import Settings from './Settings'; import Settings from './Settings';
import StringUtils from '../../src/com/utils/StringUtils'; import StringUtils from '../../src/com/utils/StringUtils';
import Render from './Render';
const moment = require('moment');
const nav = new Navigation(); const nav = new Navigation();
const settings = new Settings(); const settings = new Settings();
const utils = new StringUtils(); const utils = new StringUtils();
const moment = require('moment'); const render = new Render();
/** /**
* Class for handling blog content pages * Class for handling blog content pages
*/ */
export default class Pages { export default class Book {
//-------------------------- //--------------------------
// constructor // constructor
//-------------------------- //--------------------------
@ -23,7 +26,6 @@ export default class Pages {
// methods // methods
//-------------------------- //--------------------------
start() {} start() {}
/** /**
* Retrieves single page or pages * Retrieves single page or pages
* @parameter id: optional id if requesting a single Page * @parameter id: optional id if requesting a single Page
@ -187,6 +189,7 @@ export default class Pages {
* Retrieves single page or pages * Retrieves single page or pages
* @parameter pages: payload of pages * @parameter pages: payload of pages
*/ */
organizeTags(pages) { organizeTags(pages) {
let tags = []; let tags = [];
for (let index = 0; index < pages.length; index++) { for (let index = 0; index < pages.length; index++) {
@ -194,25 +197,30 @@ export default class Pages {
let temp = []; let temp = [];
temp = page.metadata.tags.split(','); temp = page.metadata.tags.split(',');
for (let i = 0; i < temp.length; i++) { for (let i = 0; i < temp.length; i++) {
let label = temp[i]; let label = temp[i].trim();
if (!_.find(tags, { tag_name: label.trim() })) { if (!_.find(tags, { tag_name: label })) {
tags.push({ tags.push({
tag_name: label.trim(), tag_name: label,
slug: utils.cleanString(label.trim()), slug: utils.cleanString(label),
count: 1 count: 1
}); });
} else { } else {
_.find(tags, { tag_name: label.trim() }).count++; _.find(tags, { tag_name: label }).count++;
} }
} }
} }
tags = _.orderBy(tags, ['tag_name'], ['asc']); tags = _.orderBy(tags, ['tag_name'], ['asc']);
settings settings.saveTags(tags).then(response => {
.saveTags(tags) console.log('RESPONSE', response);
.then(result => { render
console.log('RESPONSE', result); .publishTags(pages)
.then(response => {
console.log(response);
}) })
.catch(err => {}); .catch(err => {
console.log(err);
});
});
} }
//-------------------------- //--------------------------

@ -1,9 +1,11 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import sanitize from 'sanitize-html';
import StringUtils from '../../src/com/utils/StringUtils'; import StringUtils from '../../src/com/utils/StringUtils';
import fs from 'fs-extra'; import fs from 'fs-extra';
import sanitize from 'sanitize-html';
const config = require('../../site/settings.json');
const pug = require('pug'); const pug = require('pug');
const md = require('markdown-it')('commonmark'); const md = require('markdown-it')('commonmark');
const _ = require('lodash');
export default class Render { export default class Render {
//-------------------------- //--------------------------
@ -17,12 +19,11 @@ export default class Render {
/** /**
* Renders all pages from markdown to html * Renders all pages from markdown to html
* @parameter pages: payload for site pages
* @parameter theme: current theme being used as defined in settings * @parameter theme: current theme being used as defined in settings
*/ */
publishAll(theme) { publishAll(pages, theme) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.getPage()
.then(pages => {
let response = []; 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];
@ -68,10 +69,51 @@ export default class Render {
}); });
} }
} }
})
.catch(err => {
reject(err);
}); });
}
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 });
}
}
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);
});
}
}); });
} }

@ -1,6 +1,6 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import fs from 'fs-extra'; import fs from 'fs-extra';
const settings = require('../../site/settings.json'); const config = require('../../site/settings.json');
const _ = require('lodash'); const _ = require('lodash');
export default class Settings { export default class Settings {
@ -14,9 +14,9 @@ export default class Settings {
start() {} start() {}
saveTags(tags) { saveTags(tags) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!_.isEqual(settings.tags, tags)) { if (!_.isEqual(config.tags, tags)) {
settings.tags = tags; config.tags = tags;
fs.writeJson('site/settings.json', settings) fs.writeJson('site/settings.json', config)
.then(() => { .then(() => {
let response = { let response = {
type: DataEvent.SETTINGS_UPDATED, type: DataEvent.SETTINGS_UPDATED,

@ -1,13 +1,13 @@
import Book from '../../data/Book'; import Book from '../../data/Book';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const settings = require('../../../site/settings.json'); const config = require('../../../site/settings.json');
const book = new Book();
//-------------------------- //--------------------------
// Index // Index
//-------------------------- //--------------------------
router.get('/', function(req, res) { router.get('/', function (req, res) {
let book = new Book();
book.getPage().then(result => { book.getPage().then(result => {
result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id)); result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
let indexPages = []; let indexPages = [];
@ -18,9 +18,9 @@ router.get('/', function(req, res) {
}); });
let pageData = []; let pageData = [];
if (req.session.user) { if (req.session.user) {
pageData = { title: settings.global.title, status: true, pages: indexPages }; pageData = { title: config.global.title, status: true, pages: indexPages };
} else { } else {
pageData = { title: settings.global.title, status: false, pages: indexPages }; pageData = { title: config.global.title, status: false, pages: indexPages };
} }
res.render('index', pageData); res.render('index', pageData);
@ -30,7 +30,7 @@ router.get('/', function(req, res) {
//-------------------------- //--------------------------
// Logout // Logout
//-------------------------- //--------------------------
router.post('/logout', function(req, res) { router.post('/logout', function (req, res) {
req.logout(); req.logout();
return res.json({ return res.json({
message: 'LOGGED OUT' message: 'LOGGED OUT'

Loading…
Cancel
Save