You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const pageHelper = require('../helpers/page')
  4. const _ = require('lodash')
  5. /* global WIKI */
  6. /**
  7. * Create/Edit document
  8. */
  9. router.get(['/e', '/e/*'], async (req, res, next) => {
  10. const pageArgs = pageHelper.parsePath(req.path)
  11. let page = await WIKI.models.pages.getPageFromDb({
  12. path: pageArgs.path,
  13. locale: pageArgs.locale,
  14. userId: req.user.id,
  15. isPrivate: false
  16. })
  17. if (page) {
  18. _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
  19. _.set(res.locals, 'pageMeta.description', page.description)
  20. page.mode = 'update'
  21. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  22. page.content = Buffer.from(page.content).toString('base64')
  23. } else {
  24. _.set(res.locals, 'pageMeta.title', `New Page`)
  25. page = {
  26. path: pageArgs.path,
  27. localeCode: pageArgs.locale,
  28. editorKey: null,
  29. mode: 'create',
  30. content: null
  31. }
  32. }
  33. res.render('editor', { page })
  34. })
  35. /**
  36. * Administration
  37. */
  38. router.get(['/a', '/a/*'], (req, res, next) => {
  39. _.set(res.locals, 'pageMeta.title', 'Admin')
  40. res.render('admin')
  41. })
  42. /**
  43. * Profile
  44. */
  45. router.get(['/p', '/p/*'], (req, res, next) => {
  46. _.set(res.locals, 'pageMeta.title', 'User Profile')
  47. res.render('profile')
  48. })
  49. /**
  50. * History
  51. */
  52. router.get(['/h', '/h/*'], async (req, res, next) => {
  53. const pageArgs = pageHelper.parsePath(req.path)
  54. const page = await WIKI.models.pages.getPageFromDb({
  55. path: pageArgs.path,
  56. locale: pageArgs.locale,
  57. userId: req.user.id,
  58. isPrivate: false
  59. })
  60. if (page) {
  61. _.set(res.locals, 'pageMeta.title', page.title)
  62. _.set(res.locals, 'pageMeta.description', page.description)
  63. res.render('history', { page })
  64. } else {
  65. res.redirect(`/${pageArgs.path}`)
  66. }
  67. })
  68. /**
  69. * Source
  70. */
  71. router.get(['/s', '/s/*'], async (req, res, next) => {
  72. const pageArgs = pageHelper.parsePath(req.path)
  73. const page = await WIKI.models.pages.getPageFromDb({
  74. path: pageArgs.path,
  75. locale: pageArgs.locale,
  76. userId: req.user.id,
  77. isPrivate: false
  78. })
  79. if (page) {
  80. _.set(res.locals, 'pageMeta.title', page.title)
  81. _.set(res.locals, 'pageMeta.description', page.description)
  82. res.render('source', { page })
  83. } else {
  84. res.redirect(`/${pageArgs.path}`)
  85. }
  86. })
  87. /**
  88. * View document
  89. */
  90. router.get('/*', async (req, res, next) => {
  91. const pageArgs = pageHelper.parsePath(req.path)
  92. const page = await WIKI.models.pages.getPage({
  93. path: pageArgs.path,
  94. locale: pageArgs.locale,
  95. userId: req.user.id,
  96. isPrivate: false
  97. })
  98. if (page) {
  99. _.set(res.locals, 'pageMeta.title', page.title)
  100. _.set(res.locals, 'pageMeta.description', page.description)
  101. const sidebar = await WIKI.models.navigation.getTree({ cache: true })
  102. res.render('page', { page, sidebar })
  103. } else if (pageArgs.path === 'home') {
  104. res.render('welcome')
  105. } else {
  106. res.status(404).render('new', { pagePath: req.path })
  107. }
  108. })
  109. module.exports = router