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.

243 lines
5.1 KiB

  1. 'use strict'
  2. const express = require('express')
  3. const router = express.Router()
  4. const _ = require('lodash')
  5. // ==========================================
  6. // EDIT MODE
  7. // ==========================================
  8. /**
  9. * Edit document in Markdown
  10. */
  11. router.get('/edit/*', (req, res, next) => {
  12. if (!res.locals.rights.write) {
  13. return res.render('error-forbidden')
  14. }
  15. let safePath = entries.parsePath(_.replace(req.path, '/edit', ''))
  16. entries.fetchOriginal(safePath, {
  17. parseMarkdown: false,
  18. parseMeta: true,
  19. parseTree: false,
  20. includeMarkdown: true,
  21. includeParentInfo: false,
  22. cache: false
  23. }).then((pageData) => {
  24. if (pageData) {
  25. res.render('pages/edit', { pageData })
  26. } else {
  27. throw new Error('Invalid page path.')
  28. }
  29. return true
  30. }).catch((err) => {
  31. res.render('error', {
  32. message: err.message,
  33. error: {}
  34. })
  35. })
  36. })
  37. router.put('/edit/*', (req, res, next) => {
  38. if (!res.locals.rights.write) {
  39. return res.json({
  40. ok: false,
  41. error: 'Forbidden'
  42. })
  43. }
  44. let safePath = entries.parsePath(_.replace(req.path, '/edit', ''))
  45. entries.update(safePath, req.body.markdown).then(() => {
  46. return res.json({
  47. ok: true
  48. }) || true
  49. }).catch((err) => {
  50. res.json({
  51. ok: false,
  52. error: err.message
  53. })
  54. })
  55. })
  56. // ==========================================
  57. // CREATE MODE
  58. // ==========================================
  59. router.get('/create/*', (req, res, next) => {
  60. if (!res.locals.rights.write) {
  61. return res.render('error-forbidden')
  62. }
  63. if (_.some(['create', 'edit', 'account', 'source', 'history', 'mk', 'all'], (e) => { return _.startsWith(req.path, '/create/' + e) })) {
  64. return res.render('error', {
  65. message: 'You cannot create a document with this name as it is reserved by the system.',
  66. error: {}
  67. })
  68. }
  69. let safePath = entries.parsePath(_.replace(req.path, '/create', ''))
  70. entries.exists(safePath).then((docExists) => {
  71. if (!docExists) {
  72. return entries.getStarter(safePath).then((contents) => {
  73. let pageData = {
  74. markdown: contents,
  75. meta: {
  76. title: _.startCase(safePath),
  77. path: safePath
  78. }
  79. }
  80. res.render('pages/create', { pageData })
  81. return true
  82. }).catch((err) => {
  83. winston.warn(err)
  84. throw new Error('Could not load starter content!')
  85. })
  86. } else {
  87. throw new Error('This entry already exists!')
  88. }
  89. }).catch((err) => {
  90. res.render('error', {
  91. message: err.message,
  92. error: {}
  93. })
  94. })
  95. })
  96. router.put('/create/*', (req, res, next) => {
  97. if (!res.locals.rights.write) {
  98. return res.json({
  99. ok: false,
  100. error: 'Forbidden'
  101. })
  102. }
  103. let safePath = entries.parsePath(_.replace(req.path, '/create', ''))
  104. entries.create(safePath, req.body.markdown).then(() => {
  105. return res.json({
  106. ok: true
  107. }) || true
  108. }).catch((err) => {
  109. return res.json({
  110. ok: false,
  111. error: err.message
  112. })
  113. })
  114. })
  115. // ==========================================
  116. // LIST ALL PAGES
  117. // ==========================================
  118. /**
  119. * View tree view of all pages
  120. */
  121. router.get('/all', (req, res, next) => {
  122. res.render('pages/all')
  123. })
  124. // ==========================================
  125. // VIEW MODE
  126. // ==========================================
  127. /**
  128. * View source of a document
  129. */
  130. router.get('/source/*', (req, res, next) => {
  131. let safePath = entries.parsePath(_.replace(req.path, '/source', ''))
  132. entries.fetchOriginal(safePath, {
  133. parseMarkdown: false,
  134. parseMeta: true,
  135. parseTree: false,
  136. includeMarkdown: true,
  137. includeParentInfo: false,
  138. cache: false
  139. }).then((pageData) => {
  140. if (pageData) {
  141. res.render('pages/source', { pageData })
  142. } else {
  143. throw new Error('Invalid page path.')
  144. }
  145. return true
  146. }).catch((err) => {
  147. res.render('error', {
  148. message: err.message,
  149. error: {}
  150. })
  151. })
  152. })
  153. /**
  154. * View document
  155. */
  156. router.get('/*', (req, res, next) => {
  157. let safePath = entries.parsePath(req.path)
  158. entries.fetch(safePath).then((pageData) => {
  159. if (pageData) {
  160. res.render('pages/view', { pageData })
  161. } else {
  162. res.render('error-notexist', {
  163. newpath: safePath
  164. })
  165. }
  166. return true
  167. }).error((err) => {
  168. if (safePath === 'home') {
  169. res.render('pages/welcome')
  170. } else {
  171. res.render('error-notexist', {
  172. message: err.message,
  173. newpath: safePath
  174. })
  175. }
  176. return true
  177. }).catch((err) => {
  178. res.render('error', {
  179. message: err.message,
  180. error: {}
  181. })
  182. })
  183. })
  184. /**
  185. * Move document
  186. */
  187. router.put('/*', (req, res, next) => {
  188. if (!res.locals.rights.write) {
  189. return res.json({
  190. ok: false,
  191. error: 'Forbidden'
  192. })
  193. }
  194. let safePath = entries.parsePath(req.path)
  195. if (_.isEmpty(req.body.move)) {
  196. return res.json({
  197. ok: false,
  198. error: 'Invalid document action call.'
  199. })
  200. }
  201. let safeNewPath = entries.parsePath(req.body.move)
  202. entries.move(safePath, safeNewPath).then(() => {
  203. res.json({
  204. ok: true
  205. })
  206. }).catch((err) => {
  207. res.json({
  208. ok: false,
  209. error: err.message
  210. })
  211. })
  212. })
  213. module.exports = router