2016-08-17 03:56:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2016-08-28 01:46:10 +00:00
|
|
|
var _ = require('lodash');
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
// ==========================================
|
|
|
|
// EDIT MODE
|
|
|
|
// ==========================================
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit document in Markdown
|
|
|
|
*/
|
2016-08-27 05:04:08 +00:00
|
|
|
router.get('/edit/*', (req, res, next) => {
|
2016-08-28 01:46:10 +00:00
|
|
|
|
|
|
|
let safePath = entries.parsePath(_.replace(req.path, '/edit', ''));
|
|
|
|
|
|
|
|
entries.fetchOriginal(safePath, {
|
|
|
|
parseMarkdown: false,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: false,
|
|
|
|
includeMarkdown: true,
|
|
|
|
includeParentInfo: false,
|
|
|
|
cache: false
|
|
|
|
}).then((pageData) => {
|
|
|
|
if(pageData) {
|
2016-10-16 05:34:34 +00:00
|
|
|
res.render('pages/edit', { pageData });
|
2016-08-28 01:46:10 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Invalid page path.');
|
|
|
|
}
|
2016-10-16 05:34:34 +00:00
|
|
|
return true;
|
2016-08-28 01:46:10 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
});
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
router.put('/edit/*', (req, res, next) => {
|
|
|
|
|
|
|
|
let safePath = entries.parsePath(_.replace(req.path, '/edit', ''));
|
|
|
|
|
|
|
|
entries.update(safePath, req.body.markdown).then(() => {
|
|
|
|
res.json({
|
|
|
|
ok: true
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
// CREATE MODE
|
|
|
|
// ==========================================
|
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
router.get('/create/*', (req, res, next) => {
|
|
|
|
|
|
|
|
if(_.some(['create','edit','account','source','history','mk'], (e) => { return _.startsWith(req.path, '/create/' + e); })) {
|
|
|
|
return res.render('error', {
|
|
|
|
message: 'You cannot create a document with this name as it is reserved by the system.',
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let safePath = entries.parsePath(_.replace(req.path, '/create', ''));
|
|
|
|
|
|
|
|
entries.exists(safePath).then((docExists) => {
|
|
|
|
if(!docExists) {
|
2016-09-05 23:23:49 +00:00
|
|
|
return entries.getStarter(safePath).then((contents) => {
|
2016-08-30 02:19:47 +00:00
|
|
|
|
|
|
|
let pageData = {
|
|
|
|
markdown: contents,
|
|
|
|
meta: {
|
|
|
|
title: _.startCase(safePath),
|
|
|
|
path: safePath
|
|
|
|
}
|
|
|
|
};
|
2016-09-05 23:23:49 +00:00
|
|
|
res.render('pages/create', { pageData });
|
|
|
|
|
|
|
|
return true;
|
2016-08-30 02:19:47 +00:00
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
throw new Error('Could not load starter content!');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error('This entry already exists!');
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
router.put('/create/*', (req, res, next) => {
|
|
|
|
|
|
|
|
let safePath = entries.parsePath(_.replace(req.path, '/create', ''));
|
|
|
|
|
|
|
|
entries.create(safePath, req.body.markdown).then(() => {
|
|
|
|
res.json({
|
|
|
|
ok: true
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
});
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
// ==========================================
|
|
|
|
// VIEW MODE
|
|
|
|
// ==========================================
|
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
/**
|
|
|
|
* View source of a document
|
|
|
|
*/
|
|
|
|
router.get('/source/*', (req, res, next) => {
|
|
|
|
|
|
|
|
let safePath = entries.parsePath(_.replace(req.path, '/source', ''));
|
|
|
|
|
|
|
|
entries.fetchOriginal(safePath, {
|
|
|
|
parseMarkdown: false,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: false,
|
|
|
|
includeMarkdown: true,
|
|
|
|
includeParentInfo: false,
|
|
|
|
cache: false
|
|
|
|
}).then((pageData) => {
|
|
|
|
if(pageData) {
|
|
|
|
return res.render('pages/source', { pageData });
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid page path.');
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
/**
|
2016-08-29 05:21:35 +00:00
|
|
|
* View document
|
2016-08-17 03:56:08 +00:00
|
|
|
*/
|
2016-08-27 05:04:08 +00:00
|
|
|
router.get('/*', (req, res, next) => {
|
2016-08-20 04:50:29 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
let safePath = entries.parsePath(req.path);
|
2016-08-20 21:20:53 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
entries.fetch(safePath).then((pageData) => {
|
|
|
|
if(pageData) {
|
2016-10-16 05:34:34 +00:00
|
|
|
res.render('pages/view', { pageData });
|
2016-08-27 05:04:08 +00:00
|
|
|
} else {
|
2016-10-09 05:26:25 +00:00
|
|
|
res.render('error-notexist', {
|
|
|
|
newpath: safePath
|
2016-08-30 02:19:47 +00:00
|
|
|
});
|
2016-08-20 21:20:53 +00:00
|
|
|
}
|
2016-10-16 05:34:34 +00:00
|
|
|
return true;
|
2016-10-09 05:26:25 +00:00
|
|
|
}).error((err) => {
|
2016-12-09 03:44:31 +00:00
|
|
|
|
|
|
|
if(safePath === 'home') {
|
|
|
|
res.render('pages/welcome');
|
|
|
|
} else {
|
|
|
|
res.render('error-notexist', {
|
|
|
|
message: err.message,
|
|
|
|
newpath: safePath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
}).catch((err) => {
|
2016-08-30 02:19:47 +00:00
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
});
|
2016-08-27 05:04:08 +00:00
|
|
|
});
|
2016-08-20 04:50:29 +00:00
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
});
|
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
/**
|
|
|
|
* Move document
|
|
|
|
*/
|
|
|
|
router.put('/*', (req, res, next) => {
|
|
|
|
|
|
|
|
let safePath = entries.parsePath(req.path);
|
|
|
|
|
|
|
|
if(_.isEmpty(req.body.move)) {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: 'Invalid document action call.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let safeNewPath = entries.parsePath(req.body.move);
|
|
|
|
|
|
|
|
entries.move(safePath, safeNewPath).then(() => {
|
|
|
|
res.json({
|
|
|
|
ok: true
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
module.exports = router;
|