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.

251 lines
5.3 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 list view of all pages
  120. */
  121. router.get('/all', (req, res, next) => {
  122. entries.getFromTree('/').then((pageData) => {
  123. res.render('pages/all', { pageData })
  124. return true
  125. }).catch((err) => {
  126. res.render('error', {
  127. message: err.message,
  128. error: {}
  129. })
  130. })
  131. })
  132. // ==========================================
  133. // VIEW MODE
  134. // ==========================================
  135. /**
  136. * View source of a document
  137. */
  138. router.get('/source/*', (req, res, next) => {
  139. let safePath = entries.parsePath(_.replace(req.path, '/source', ''))
  140. entries.fetchOriginal(safePath, {
  141. parseMarkdown: false,
  142. parseMeta: true,
  143. parseTree: false,
  144. includeMarkdown: true,
  145. includeParentInfo: false,
  146. cache: false
  147. }).then((pageData) => {
  148. if (pageData) {
  149. res.render('pages/source', { pageData })
  150. } else {
  151. throw new Error('Invalid page path.')
  152. }
  153. return true
  154. }).catch((err) => {
  155. res.render('error', {
  156. message: err.message,
  157. error: {}
  158. })
  159. })
  160. })
  161. /**
  162. * View document
  163. */
  164. router.get('/*', (req, res, next) => {
  165. let safePath = entries.parsePath(req.path)
  166. entries.fetch(safePath).then((pageData) => {
  167. if (pageData) {
  168. res.render('pages/view', { pageData })
  169. } else {
  170. res.render('error-notexist', {
  171. newpath: safePath
  172. })
  173. }
  174. return true
  175. }).error((err) => {
  176. if (safePath === 'home') {
  177. res.render('pages/welcome')
  178. } else {
  179. res.render('error-notexist', {
  180. message: err.message,
  181. newpath: safePath
  182. })
  183. }
  184. return true
  185. }).catch((err) => {
  186. res.render('error', {
  187. message: err.message,
  188. error: {}
  189. })
  190. })
  191. })
  192. /**
  193. * Move document
  194. */
  195. router.put('/*', (req, res, next) => {
  196. if (!res.locals.rights.write) {
  197. return res.json({
  198. ok: false,
  199. error: 'Forbidden'
  200. })
  201. }
  202. let safePath = entries.parsePath(req.path)
  203. if (_.isEmpty(req.body.move)) {
  204. return res.json({
  205. ok: false,
  206. error: 'Invalid document action call.'
  207. })
  208. }
  209. let safeNewPath = entries.parsePath(req.body.move)
  210. entries.move(safePath, safeNewPath).then(() => {
  211. res.json({
  212. ok: true
  213. })
  214. }).catch((err) => {
  215. res.json({
  216. ok: false,
  217. error: err.message
  218. })
  219. })
  220. })
  221. module.exports = router