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.

272 lines
5.8 KiB

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