2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
|
|
|
const _ = require('lodash')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
const entryHelper = require('../helpers/entry')
|
|
|
|
|
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) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.write) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/edit', ''))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
entries.fetchOriginal(safePath, {
|
|
|
|
parseMarkdown: false,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: false,
|
|
|
|
includeMarkdown: true,
|
|
|
|
includeParentInfo: false,
|
|
|
|
cache: false
|
|
|
|
}).then((pageData) => {
|
|
|
|
if (pageData) {
|
|
|
|
res.render('pages/edit', { pageData })
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid page path.')
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).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) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.write) {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: 'Forbidden'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/edit', ''))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
entries.update(safePath, req.body.markdown, req.user).then(() => {
|
2017-02-09 01:52:37 +00:00
|
|
|
return res.json({
|
|
|
|
ok: true
|
|
|
|
}) || true
|
|
|
|
}).catch((err) => {
|
|
|
|
res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-29 05:21:35 +00:00
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
// CREATE MODE
|
|
|
|
// ==========================================
|
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
router.get('/create/*', (req, res, next) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.write) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-04-09 15:48:37 +00:00
|
|
|
if (_.some(['create', 'edit', 'account', 'source', 'history', 'mk', 'all'], (e) => { return _.startsWith(req.path, '/create/' + e) })) {
|
2017-02-09 01:52:37 +00:00
|
|
|
return res.render('error', {
|
|
|
|
message: 'You cannot create a document with this name as it is reserved by the system.',
|
|
|
|
error: {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/create', ''))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
entries.exists(safePath).then((docExists) => {
|
|
|
|
if (!docExists) {
|
|
|
|
return entries.getStarter(safePath).then((contents) => {
|
|
|
|
let pageData = {
|
|
|
|
markdown: contents,
|
|
|
|
meta: {
|
|
|
|
title: _.startCase(safePath),
|
|
|
|
path: safePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('pages/create', { pageData })
|
|
|
|
|
|
|
|
return true
|
|
|
|
}).catch((err) => {
|
|
|
|
winston.warn(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: {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-30 02:19:47 +00:00
|
|
|
|
|
|
|
router.put('/create/*', (req, res, next) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.write) {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: 'Forbidden'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/create', ''))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
entries.create(safePath, req.body.markdown, req.user).then(() => {
|
2017-02-09 01:52:37 +00:00
|
|
|
return res.json({
|
|
|
|
ok: true
|
|
|
|
}) || true
|
|
|
|
}).catch((err) => {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-27 05:04:08 +00:00
|
|
|
|
2017-04-09 15:48:37 +00:00
|
|
|
// ==========================================
|
|
|
|
// LIST ALL PAGES
|
|
|
|
// ==========================================
|
|
|
|
|
|
|
|
/**
|
2017-04-18 02:44:04 +00:00
|
|
|
* View tree view of all pages
|
2017-04-09 15:48:37 +00:00
|
|
|
*/
|
2017-04-23 14:56:14 +00:00
|
|
|
router.use((req, res, next) => {
|
|
|
|
if (_.endsWith(req.url, '/all')) {
|
|
|
|
res.render('pages/all')
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
2017-04-09 15:48:37 +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) => {
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/source', ''))
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
entries.fetchOriginal(safePath, {
|
|
|
|
parseMarkdown: false,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: false,
|
|
|
|
includeMarkdown: true,
|
|
|
|
includeParentInfo: false,
|
|
|
|
cache: false
|
|
|
|
}).then((pageData) => {
|
|
|
|
if (pageData) {
|
|
|
|
res.render('pages/source', { pageData })
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid page path.')
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).catch((err) => {
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-30 02:19:47 +00:00
|
|
|
|
2017-04-30 20:37:10 +00:00
|
|
|
/**
|
|
|
|
* View history of a document
|
|
|
|
*/
|
|
|
|
router.get('/hist/*', (req, res, next) => {
|
|
|
|
let safePath = entryHelper.parsePath(_.replace(req.path, '/hist', ''))
|
|
|
|
|
|
|
|
entries.getHistory(safePath).then((pageData) => {
|
|
|
|
if (pageData) {
|
|
|
|
res.render('pages/history', { pageData })
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid page path.')
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).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) => {
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(req.path)
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
entries.fetch(safePath).then((pageData) => {
|
|
|
|
if (pageData) {
|
|
|
|
res.render('pages/view', { pageData })
|
|
|
|
} else {
|
|
|
|
res.render('error-notexist', {
|
|
|
|
newpath: safePath
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}).error((err) => {
|
|
|
|
if (safePath === 'home') {
|
|
|
|
res.render('pages/welcome')
|
|
|
|
} else {
|
|
|
|
res.render('error-notexist', {
|
|
|
|
message: err.message,
|
|
|
|
newpath: safePath
|
|
|
|
})
|
|
|
|
}
|
2017-02-12 03:54:27 +00:00
|
|
|
return true
|
2017-02-09 01:52:37 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
/**
|
|
|
|
* Move document
|
|
|
|
*/
|
|
|
|
router.put('/*', (req, res, next) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.write) {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: 'Forbidden'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safePath = entryHelper.parsePath(req.path)
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
if (_.isEmpty(req.body.move)) {
|
|
|
|
return res.json({
|
|
|
|
ok: false,
|
|
|
|
error: 'Invalid document action call.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:22:03 +00:00
|
|
|
let safeNewPath = entryHelper.parsePath(req.body.move)
|
2017-02-09 01:52:37 +00:00
|
|
|
|
2017-04-28 21:58:38 +00:00
|
|
|
entries.move(safePath, safeNewPath, req.user).then(() => {
|
2017-02-09 01:52:37 +00:00
|
|
|
res.json({
|
|
|
|
ok: true
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
res.json({
|
|
|
|
ok: false,
|
|
|
|
error: err.message
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = router
|