You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/data/Book.js

217 lines
5.4 KiB
JavaScript

5 years ago
import fh from 'filehound';
import fs from 'fs-extra';
import metadataParser from 'markdown-yaml-metadata-parser';
import _ from 'lodash';
import * as DataEvent from '../../src/com/events/DataEvent';
import Navigation from './Navigation';
import Utils from './Utils';
const moment = require('moment');
const nav = new Navigation();
const utils = new Utils();
/**
* Class for handling blog content pages
*/
5 years ago
export default class Book {
5 years ago
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// methods
//--------------------------
start() {}
/**
* Retrieves single page or pages
* @parameter id: optional id if requesting a single Page
*/
getPage(id) {
5 years ago
return new Promise((resolve, reject) => {
fh.create()
.paths('content/pages')
.ext('md')
.find()
.then(files => {
let pages = [];
for (let index = 0; index < files.length; index++) {
fs.readFile(files[index], { encoding: 'utf8' }, (err, file) => {
pages.push(metadataParser(file));
});
}
if (id === null || id === null || id === undefined) {
setTimeout(() => {
//TODO: Duct tape solution until something better created
utils.organizeTags(pages);
5 years ago
resolve(pages);
}, 100);
} else {
setTimeout(() => {
//TODO: Duct tape solution until something better created
let page = _.find(pages, list => {
return list.metadata.uuid === id;
});
resolve(page);
}, 100);
5 years ago
}
})
.catch(err => {
reject(err);
});
});
}
/**
* Edits single page based on id and task
* @parameter body: object that contains all page information
* @parameter id: identifier for page being edited
* @parameter task: type of task being performed - listed in DataEvents Class
* @parameter user: object containin user information
*/
editPage(body, id, task, user) {
return new Promise((resolve, reject) => {
let self = this;
let response = [];
switch (task) {
case DataEvent.API_PAGE_CREATE:
case DataEvent.API_PAGE_WRITE:
var layout = 'page';
var path = '';
fs.ensureDir(
'content/pages/' +
moment(body.created).format('YYYY') +
'/' +
moment(body.created).format('MM') +
'/'
).then(() => {
if (body.layout !== 'page') layout = body.layout;
if (body.layout === null || body.layout === 'null') layout = 'page';
var pageWrite =
'---\n' +
'id: ' +
body.id +
'\n' +
'uuid: ' +
body.uuid +
'\n' +
'title: ' +
body.title +
'\n' +
'feature: ' +
body.feature +
'\n' +
'path: ' +
moment(body.created).format('YYYY') +
'/' +
moment().format('MM') +
'\n' +
'layout: ' +
layout +
'\n' +
'tags: ' +
body.tags +
'\n' +
'author: ' +
user.handle +
'\n' +
'created: ' +
moment(body.created).format() +
'\n' +
'updated: ' +
moment(Date.now()).format() +
'\n' +
'deleted: ' +
body.deleted +
'\n' +
'menu: ' +
body.menu +
'\n' +
'featured: ' +
body.featured +
'\n' +
'published: ' +
body.published +
'\n' +
'slug: ' +
body.slug +
'\n' +
'---\n' +
body.content;
layout === 'index'
? (path = 'content/pages/' + body.slug + '.md')
: (path =
'content/pages/' +
moment(body.created).format('YYYY') +
'/' +
moment(body.created).format('MM') +
'/' +
body.slug +
'.md');
fs.writeFile(path, pageWrite, err => {
// throws an error, you could also catch it here
if (err) {
response = { type: DataEvent.PAGE_ERROR, message: err };
reject(response);
}
// success case, the file was saved
if (task === DataEvent.API_PAGE_CREATE) {
// if new file, update settings index and page count
response = {
type: DataEvent.PAGE_ADDED,
message: 'New Page Created',
id: body.uuid
};
resolve(response);
} else {
response = {
type: DataEvent.PAGE_UPDATED,
message: 'Page saved. Nice Work'
};
resolve(response);
}
});
});
break;
case DataEvent.API_PAGE_DELETE:
this.getPage(id)
.then(page => {
let body = _.mapValues(page.metadata);
body.content = page.content;
body.deleted = moment(Date.now()).format();
self.editPage(body, body.uuid, DataEvent.API_PAGE_WRITE, user)
.then(() => {
let item = {
title: body.title,
id: body.id,
slug: body.slug,
uuid: body.uuid
};
nav.editMenu(DataEvent.MENU_DELETE_ITEM, item);
response = {
type: DataEvent.PAGE_DELETED,
message: 'Page deleted, sport',
data: { uuid: body.uuid }
};
resolve(response);
})
.catch(err => {
response = { type: DataEvent.PAGE_ERROR, message: err };
reject(response);
});
})
.catch(err => {
response = { type: DataEvent.PAGE_ERROR, message: err };
reject(response);
});
break;
}
});
}
5 years ago
//--------------------------
// event handlers
//--------------------------
}