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.

211 lines
4.0 KiB

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