2019-11-23 09:13:53 +01:00
|
|
|
import fh from 'filehound';
|
|
|
|
import fs from 'fs-extra';
|
2020-05-06 23:43:53 +02:00
|
|
|
import sanitize from 'sanitize-html';
|
2019-11-23 09:13:53 +01:00
|
|
|
import metadataParser from 'markdown-yaml-metadata-parser';
|
2019-11-23 19:30:34 +01:00
|
|
|
import _ from 'lodash';
|
2020-03-01 06:01:34 +01:00
|
|
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
2020-03-01 22:13:18 +01:00
|
|
|
import Navigation from './Navigation';
|
2020-05-06 23:43:53 +02:00
|
|
|
import StringUtils from '../../src/com/utils/StringUtils';
|
2020-03-01 22:13:18 +01:00
|
|
|
const nav = new Navigation();
|
2020-03-01 06:01:34 +01:00
|
|
|
const moment = require('moment');
|
2020-05-06 23:43:53 +02:00
|
|
|
const pug = require('pug');
|
|
|
|
const md = require('markdown-it')('commonmark');
|
2019-11-23 09:13:53 +01:00
|
|
|
|
|
|
|
export default class Pages {
|
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {}
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
//--------------------------
|
|
|
|
start() {}
|
|
|
|
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Retrieves single page or pages
|
|
|
|
* @parameter id: optional id if requesting a single Page
|
|
|
|
*/
|
2019-11-23 19:30:34 +01:00
|
|
|
getPage(id) {
|
2019-11-23 09:13:53 +01:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
2019-11-23 19:30:34 +01:00
|
|
|
|
2019-11-23 09:13:53 +01:00
|
|
|
if (id === null || id === null || id === undefined) {
|
|
|
|
setTimeout(() => {
|
|
|
|
//TODO: Duct tape solution until something better created
|
|
|
|
resolve(pages);
|
|
|
|
}, 100);
|
|
|
|
} else {
|
2019-11-23 19:30:34 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
//TODO: Duct tape solution until something better created
|
|
|
|
let page = _.find(pages, list => {
|
|
|
|
return list.metadata.uuid === id;
|
|
|
|
});
|
|
|
|
resolve(page);
|
|
|
|
}, 100);
|
2019-11-23 09:13:53 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-03-01 06:01:34 +01:00
|
|
|
/**
|
|
|
|
* Edits single page based on id and task
|
|
|
|
* @parameter id: id of page being edited
|
|
|
|
* @parameter task: type of task being performed
|
|
|
|
*/
|
|
|
|
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 pageWrite =
|
|
|
|
'---\n' +
|
|
|
|
'id: ' +
|
|
|
|
body.id +
|
|
|
|
'\n' +
|
|
|
|
'uuid: ' +
|
|
|
|
body.uuid +
|
|
|
|
'\n' +
|
|
|
|
'title: ' +
|
|
|
|
body.title +
|
|
|
|
'\n' +
|
|
|
|
'feature: ' +
|
|
|
|
body.feature +
|
|
|
|
'\n' +
|
|
|
|
'layout: ' +
|
|
|
|
'page' +
|
|
|
|
'\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;
|
|
|
|
fs.writeFile('content/pages/' + body.slug + '.md', 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.page_uuid
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGE_UPDATED,
|
2020-03-01 22:13:18 +01:00
|
|
|
message: 'Page saved. Nice Work'
|
2020-03-01 06:01:34 +01:00
|
|
|
};
|
|
|
|
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(() => {
|
2020-03-01 22:13:18 +01:00
|
|
|
let item = {
|
|
|
|
title: body.title,
|
|
|
|
id: body.id,
|
|
|
|
slug: body.slug,
|
|
|
|
uuid: body.uuid
|
|
|
|
};
|
|
|
|
nav.editMenu(DataEvent.MENU_DELETE_ITEM, item);
|
2020-03-01 06:01:34 +01:00
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGE_DELETED,
|
2020-03-01 22:13:18 +01:00
|
|
|
message: 'Page deleted, sport',
|
|
|
|
data: { uuid: body.uuid }
|
2020-03-01 06:01:34 +01:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-06 23:43:53 +02:00
|
|
|
publish(theme) {
|
2020-05-05 21:31:32 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.getPage()
|
|
|
|
.then(pages => {
|
2020-05-06 23:43:53 +02:00
|
|
|
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,
|
|
|
|
content: 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 21:31:32 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-11-23 09:13:53 +01:00
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
}
|