forked from projects/fipamo
added tag rendering events, added data utilty class
This commit is contained in:
parent
3f4089a6d3
commit
d257d94a80
4 changed files with 56 additions and 49 deletions
|
@ -4,14 +4,10 @@ import metadataParser from 'markdown-yaml-metadata-parser';
|
|||
import _ from 'lodash';
|
||||
import * as DataEvent from '../../src/com/events/DataEvent';
|
||||
import Navigation from './Navigation';
|
||||
import Settings from './Settings';
|
||||
import StringUtils from '../../src/com/utils/StringUtils';
|
||||
import Render from './Render';
|
||||
import Utils from './Utils';
|
||||
const moment = require('moment');
|
||||
const nav = new Navigation();
|
||||
const settings = new Settings();
|
||||
const utils = new StringUtils();
|
||||
const render = new Render();
|
||||
const utils = new Utils();
|
||||
|
||||
/**
|
||||
* Class for handling blog content pages
|
||||
|
@ -31,7 +27,6 @@ export default class Book {
|
|||
* @parameter id: optional id if requesting a single Page
|
||||
*/
|
||||
getPage(id) {
|
||||
var self = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
fh.create()
|
||||
.paths('content/pages')
|
||||
|
@ -47,7 +42,7 @@ export default class Book {
|
|||
if (id === null || id === null || id === undefined) {
|
||||
setTimeout(() => {
|
||||
//TODO: Duct tape solution until something better created
|
||||
self.organizeTags(pages);
|
||||
utils.organizeTags(pages);
|
||||
resolve(pages);
|
||||
}, 100);
|
||||
} 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
|
||||
|
|
|
@ -100,16 +100,14 @@ export default class Render {
|
|||
|
||||
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 };
|
||||
response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
|
||||
reject(response);
|
||||
}
|
||||
|
||||
// success case, the file was saved
|
||||
response = {
|
||||
type: DataEvent.PAGES_RENDERED,
|
||||
message: 'All Pages Rendered. Sweet.'
|
||||
type: DataEvent.TAG_PAGES_RENDERED,
|
||||
message: 'Tag Pages ready to go. Good job.'
|
||||
};
|
||||
resolve(response);
|
||||
});
|
||||
|
|
48
brain/data/Utils.js
Normal file
48
brain/data/Utils.js
Normal 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -13,6 +13,8 @@ export const PAGE_UPDATED = 'postUpdated';
|
|||
export const PAGE_DELETED = 'postImageAdded';
|
||||
export const PAGES_RENDERED = 'pagesRendered';
|
||||
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_NOT_UPDATED = 'settingsNotUpdated';
|
||||
export const MENU_ADD_ITEM = 'menuAddItem';
|
||||
|
|
Loading…
Reference in a new issue