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.

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