added tag rendering events, added data utilty class

This commit is contained in:
Ro 2020-05-21 14:13:03 -07:00
parent 3f4089a6d3
commit d257d94a80
4 changed files with 56 additions and 49 deletions

View file

@ -4,14 +4,10 @@ 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 Settings from './Settings'; import Utils from './Utils';
import StringUtils from '../../src/com/utils/StringUtils';
import Render from './Render';
const moment = require('moment'); const moment = require('moment');
const nav = new Navigation(); const nav = new Navigation();
const settings = new Settings(); const utils = new Utils();
const utils = new StringUtils();
const render = new Render();
/** /**
* Class for handling blog content pages * Class for handling blog content pages
@ -31,7 +27,6 @@ export default class Book {
* @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')
@ -47,7 +42,7 @@ export default class Book {
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); utils.organizeTags(pages);
resolve(pages); resolve(pages);
}, 100); }, 100);
} else { } else {
@ -188,42 +183,6 @@ export default class Book {
} }
}); });
} }
/**
* Retrieves single page or pages
* @parameter pages: payload of pages
*/
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++) {
let label = temp[i].trim();
if (!_.find(tags, { tag_name: label })) {
tags.push({
tag_name: label,
slug: utils.cleanString(label),
count: 1
});
} else {
_.find(tags, { tag_name: label }).count++;
}
}
}
tags = _.orderBy(tags, ['tag_name'], ['asc']);
settings.saveTags(tags).then(() => {
render
.publishTags(pages)
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
});
}
//-------------------------- //--------------------------
// event handlers // event handlers

View file

@ -100,16 +100,14 @@ export default class Render {
fs.writeFile('public/tags/' + item.slug + '.html', file, err => { fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
if (err) { if (err) {
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err }; response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
reject(response); reject(response);
} }
// success case, the file was saved // success case, the file was saved
response = { response = {
type: DataEvent.PAGES_RENDERED, type: DataEvent.TAG_PAGES_RENDERED,
message: 'All Pages Rendered. Sweet.' message: 'Tag Pages ready to go. Good job.'
}; };
resolve(response); resolve(response);
}); });

48
brain/data/Utils.js Normal file
View file

@ -0,0 +1,48 @@
import Settings from './Settings';
import Render from './Render';
import StringUtils from '../../src/com/utils/StringUtils';
import _ from 'lodash';
const settings = new Settings();
const render = new Render();
const stringUtils = new StringUtils();
export default class Utils {
constructor() {}
/**
* Retrieves single page or pages
* @parameter pages: payload of pages
*/
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++) {
let label = temp[i].trim();
if (!_.find(tags, { tag_name: label })) {
tags.push({
tag_name: label,
slug: stringUtils.cleanString(label),
count: 1
});
} else {
_.find(tags, { tag_name: label }).count++;
}
}
}
tags = _.orderBy(tags, ['tag_name'], ['asc']);
settings.saveTags(tags).then(() => {
render
.publishTags(pages)
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
});
}
}

View file

@ -13,6 +13,8 @@ export const PAGE_UPDATED = 'postUpdated';
export const PAGE_DELETED = 'postImageAdded'; 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 TAG_PAGES_RENDERED = 'tagPagesRendered';
export const TAG_PAGES_NOT_RENDERED = 'tagPagesNotRendered';
export const SETTINGS_UPDATED = 'settingsUpdated'; export const SETTINGS_UPDATED = 'settingsUpdated';
export const SETTINGS_NOT_UPDATED = 'settingsNotUpdated'; export const SETTINGS_NOT_UPDATED = 'settingsNotUpdated';
export const MENU_ADD_ITEM = 'menuAddItem'; export const MENU_ADD_ITEM = 'menuAddItem';