2017-02-09 01:52:37 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2018-08-19 05:22:59 +00:00
|
|
|
const pageHelper = require('../helpers/page')
|
|
|
|
|
|
|
|
/* global WIKI */
|
2017-07-06 04:10:41 +00:00
|
|
|
|
2018-02-11 05:20:17 +00:00
|
|
|
/**
|
|
|
|
* Create/Edit document
|
|
|
|
*/
|
2018-09-16 22:36:15 +00:00
|
|
|
router.get(['/e', '/e/*'], async (req, res, next) => {
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path)
|
|
|
|
let page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
if (page) {
|
|
|
|
page.mode = 'update'
|
|
|
|
page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
|
|
|
|
page.content = Buffer.from(page.content).toString('base64')
|
|
|
|
} else {
|
|
|
|
page = {
|
|
|
|
path: pageArgs.path,
|
|
|
|
localeCode: pageArgs.locale,
|
|
|
|
editorKey: null,
|
|
|
|
mode: 'create',
|
|
|
|
content: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('editor', { page })
|
2018-02-11 05:20:17 +00:00
|
|
|
})
|
|
|
|
|
2018-02-28 05:54:09 +00:00
|
|
|
/**
|
|
|
|
* Administration
|
|
|
|
*/
|
|
|
|
router.get(['/a', '/a/*'], (req, res, next) => {
|
2018-08-11 22:16:56 +00:00
|
|
|
res.render('admin')
|
2018-02-28 05:54:09 +00:00
|
|
|
})
|
|
|
|
|
2018-05-21 03:27:06 +00:00
|
|
|
/**
|
|
|
|
* Profile
|
|
|
|
*/
|
|
|
|
router.get(['/p', '/p/*'], (req, res, next) => {
|
2018-08-11 22:16:56 +00:00
|
|
|
res.render('profile')
|
2018-05-21 03:27:06 +00:00
|
|
|
})
|
|
|
|
|
2018-10-29 02:09:58 +00:00
|
|
|
/**
|
2018-11-25 06:28:20 +00:00
|
|
|
* History
|
2018-10-29 02:09:58 +00:00
|
|
|
*/
|
|
|
|
router.get(['/h', '/h/*'], async (req, res, next) => {
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path)
|
2018-11-25 06:28:20 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
2018-10-29 02:09:58 +00:00
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
if (page) {
|
|
|
|
res.render('history', { page })
|
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-11-25 06:28:20 +00:00
|
|
|
/**
|
|
|
|
* Source
|
|
|
|
*/
|
|
|
|
router.get(['/s', '/s/*'], async (req, res, next) => {
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path)
|
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
|
|
|
userId: req.user.id,
|
|
|
|
isPrivate: false
|
|
|
|
})
|
|
|
|
if (page) {
|
|
|
|
res.render('source', { page })
|
|
|
|
} else {
|
|
|
|
res.redirect(`/${pageArgs.path}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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
|
|
|
*/
|
2018-08-19 05:22:59 +00:00
|
|
|
router.get('/*', async (req, res, next) => {
|
|
|
|
const pageArgs = pageHelper.parsePath(req.path)
|
|
|
|
const page = await WIKI.models.pages.getPage({
|
|
|
|
path: pageArgs.path,
|
|
|
|
locale: pageArgs.locale,
|
2018-08-20 05:02:57 +00:00
|
|
|
userId: req.user.id,
|
2018-09-16 00:58:23 +00:00
|
|
|
isPrivate: false
|
2018-08-19 05:22:59 +00:00
|
|
|
})
|
|
|
|
if (page) {
|
2018-11-11 18:54:33 +00:00
|
|
|
const sidebar = await WIKI.models.navigation.getTree({ cache: true })
|
|
|
|
res.render('page', { page, sidebar })
|
2018-08-19 05:22:59 +00:00
|
|
|
} else if (pageArgs.path === 'home') {
|
|
|
|
res.render('welcome')
|
|
|
|
} else {
|
2018-09-16 22:36:15 +00:00
|
|
|
res.render('new', { pagePath: req.path })
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
2018-08-06 03:03:46 +00:00
|
|
|
})
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
module.exports = router
|