2019-11-23 19:30:34 +01:00
|
|
|
import Book from '../../data/Book';
|
2019-11-24 01:09:37 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const hljs = require('highlight.js/lib/highlight');
|
|
|
|
const hljs_md = require('highlight.js/lib/languages/markdown');
|
|
|
|
const moment = require('moment');
|
2019-11-23 19:30:34 +01:00
|
|
|
const book = new Book();
|
2019-11-25 22:58:34 +01:00
|
|
|
const uuidv4 = require('uuid/v4');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
hljs.registerLanguage('markdown', hljs_md);
|
2018-11-11 21:22:01 +01:00
|
|
|
//--------------------------
|
|
|
|
// POSTS
|
|
|
|
//--------------------------
|
2019-03-02 03:51:35 +01:00
|
|
|
router.get('/list/:filter?/:page?', function(req, res) {
|
2019-02-27 17:17:51 +01:00
|
|
|
var pageNum = req.params.page;
|
2019-03-01 23:48:05 +01:00
|
|
|
var filter = req.params.filter;
|
2019-02-27 17:17:51 +01:00
|
|
|
if (pageNum == '' || pageNum == null) pageNum = 1;
|
2019-03-01 23:48:05 +01:00
|
|
|
if (filter == '' || filter == null) filter = 'all';
|
2019-11-16 18:10:05 +01:00
|
|
|
/*
|
2019-02-27 17:17:51 +01:00
|
|
|
if (req.session.user) {
|
2019-11-10 21:07:29 +01:00
|
|
|
|
2019-02-27 17:17:51 +01:00
|
|
|
.then(() => {
|
|
|
|
//console.log(value);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//next(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2019-11-16 18:10:05 +01:00
|
|
|
*/
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST ADD DISPLAY
|
|
|
|
//--------------------------
|
2019-02-27 17:17:51 +01:00
|
|
|
router.get('/add/new', function(req, res) {
|
|
|
|
if (req.session.user) {
|
2019-11-25 22:58:34 +01:00
|
|
|
fs.readJSON('site/settings.json')
|
|
|
|
.then(settings => {
|
|
|
|
//use current index as id, then updated current index and page count
|
|
|
|
let pageID = settings.library_stats.current_index;
|
|
|
|
settings.library_stats.current_index = ++settings.library_stats.current_index;
|
|
|
|
settings.library_stats.total_pages = ++settings.library_stats.total_pages;
|
2019-11-26 01:51:35 +01:00
|
|
|
fs.writeJson('site/settings.json')
|
2019-11-25 22:58:34 +01:00
|
|
|
.then(() => {
|
|
|
|
res.render('page-edit', {
|
|
|
|
id: pageID,
|
|
|
|
uuid: uuidv4(),
|
|
|
|
title: 'Add New Page',
|
|
|
|
user_status: true,
|
|
|
|
welcome: 'Add New Page',
|
|
|
|
date: moment(Date.now()).format('YYYY MMM DD'),
|
|
|
|
page: [],
|
|
|
|
rawDate: Date.now(),
|
|
|
|
status: ['false', 'false', 'false'],
|
|
|
|
edit: false
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-11-26 01:51:35 +01:00
|
|
|
res.render('error', { error: err });
|
2019-11-25 22:58:34 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-11-26 01:51:35 +01:00
|
|
|
res.render('error', { error: err });
|
2019-11-25 22:58:34 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST EDIT DISPLAY
|
|
|
|
//--------------------------
|
2019-02-27 17:17:51 +01:00
|
|
|
router.get('/edit/:id', function(req, res) {
|
2019-11-23 19:30:34 +01:00
|
|
|
var id = req.params.id;
|
2019-02-27 17:17:51 +01:00
|
|
|
if (req.session.user) {
|
2019-11-23 19:30:34 +01:00
|
|
|
book.getPage(id)
|
|
|
|
.then(page => {
|
|
|
|
let pretty = hljs.highlight('markdown', page.content).value;
|
|
|
|
res.render('page-edit', {
|
|
|
|
id: page.metadata.id,
|
2019-11-25 01:11:12 +01:00
|
|
|
uuid: page.metadata.uuid,
|
2019-11-23 19:30:34 +01:00
|
|
|
title: 'Edit Page',
|
|
|
|
welcome: 'Edit Page',
|
|
|
|
page: page.metadata,
|
2019-11-24 01:09:37 +01:00
|
|
|
date: moment(page.metadata.created).format('YYYY MMM DD'),
|
2019-11-25 01:11:12 +01:00
|
|
|
rawDate: page.metadata.created,
|
2019-11-23 19:30:34 +01:00
|
|
|
colored: pretty,
|
|
|
|
feature: page.metadata.feature,
|
|
|
|
status: [
|
2019-11-25 22:58:34 +01:00
|
|
|
String(page.metadata.menu),
|
2019-11-23 19:30:34 +01:00
|
|
|
String(page.metadata.featured),
|
|
|
|
String(page.metadata.published)
|
|
|
|
],
|
|
|
|
edit: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
res.json({ type: err });
|
|
|
|
});
|
2019-11-16 18:10:05 +01:00
|
|
|
} else {
|
2019-02-27 17:17:51 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
module.exports = router;
|