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.

68 lines
1.5 KiB

  1. const express = require('express')
  2. const router = express.Router()
  3. const pageHelper = require('../helpers/page')
  4. /* global WIKI */
  5. /**
  6. * Create/Edit document
  7. */
  8. router.get(['/e', '/e/*'], async (req, res, next) => {
  9. const pageArgs = pageHelper.parsePath(req.path)
  10. let page = await WIKI.models.pages.getPageFromDb({
  11. path: pageArgs.path,
  12. locale: pageArgs.locale,
  13. userId: req.user.id,
  14. isPrivate: false
  15. })
  16. if (page) {
  17. page.mode = 'update'
  18. page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
  19. page.content = Buffer.from(page.content).toString('base64')
  20. } else {
  21. page = {
  22. path: pageArgs.path,
  23. localeCode: pageArgs.locale,
  24. editorKey: null,
  25. mode: 'create',
  26. content: null
  27. }
  28. }
  29. res.render('editor', { page })
  30. })
  31. /**
  32. * Administration
  33. */
  34. router.get(['/a', '/a/*'], (req, res, next) => {
  35. res.render('admin')
  36. })
  37. /**
  38. * Profile
  39. */
  40. router.get(['/p', '/p/*'], (req, res, next) => {
  41. res.render('profile')
  42. })
  43. /**
  44. * View document
  45. */
  46. router.get('/*', async (req, res, next) => {
  47. const pageArgs = pageHelper.parsePath(req.path)
  48. const page = await WIKI.models.pages.getPage({
  49. path: pageArgs.path,
  50. locale: pageArgs.locale,
  51. userId: req.user.id,
  52. isPrivate: false
  53. })
  54. if (page) {
  55. res.render('page', { page })
  56. } else if (pageArgs.path === 'home') {
  57. res.render('welcome')
  58. } else {
  59. res.render('new', { pagePath: req.path })
  60. }
  61. })
  62. module.exports = router