import Book from '../../data/Book'; 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'); const book = new Book(); const uuidv4 = require('uuid/v4'); const fs = require('fs-extra'); hljs.registerLanguage('markdown', hljs_md); //-------------------------- // POSTS //-------------------------- router.get('/list/:filter?/:page?', function(req, res) { var pageNum = req.params.page; var filter = req.params.filter; if (pageNum == '' || pageNum == null) pageNum = 1; if (filter == '' || filter == null) filter = 'all'; /* if (req.session.user) { .then(() => { //console.log(value); }) .catch(() => { //next(err); }); } else { res.redirect('/@/dashboard'); } */ }); //-------------------------- // BLOG POST ADD DISPLAY //-------------------------- router.get('/add/new', function(req, res) { if (req.session.user) { 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; fs.writeJson('site/settings.json') .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 => { res.render('error', { error: err }); }); }) .catch(err => { res.render('error', { error: err }); }); } else { res.redirect('/@/dashboard'); } }); //-------------------------- // BLOG POST EDIT DISPLAY //-------------------------- router.get('/edit/:id', function(req, res) { var id = req.params.id; if (req.session.user) { book.getPage(id) .then(page => { let pretty = hljs.highlight('markdown', page.content).value; res.render('page-edit', { id: page.metadata.id, uuid: page.metadata.uuid, title: 'Edit Page', welcome: 'Edit Page', page: page.metadata, date: moment(page.metadata.created).format('YYYY MMM DD'), rawDate: page.metadata.created, colored: pretty, feature: page.metadata.feature, status: [ String(page.metadata.menu), String(page.metadata.featured), String(page.metadata.published) ], edit: true }); }) .catch(err => { res.json({ type: err }); }); } else { res.redirect('/@/dashboard'); } }); module.exports = router;