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.

174 lines
3.5 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. entries.getStarter(safePath).then((contents) => {
  60. let pageData = {
  61. markdown: contents,
  62. meta: {
  63. title: _.startCase(safePath),
  64. path: safePath
  65. }
  66. };
  67. return res.render('pages/create', { pageData });
  68. }).catch((err) => {
  69. throw new Error('Could not load starter content!');
  70. });
  71. } else {
  72. throw new Error('This entry already exists!');
  73. }
  74. }).catch((err) => {
  75. res.render('error', {
  76. message: err.message,
  77. error: {}
  78. });
  79. });
  80. });
  81. router.put('/create/*', (req, res, next) => {
  82. let safePath = entries.parsePath(_.replace(req.path, '/create', ''));
  83. entries.create(safePath, req.body.markdown).then(() => {
  84. res.json({
  85. ok: true
  86. });
  87. }).catch((err) => {
  88. res.json({
  89. ok: false,
  90. error: err.message
  91. });
  92. });
  93. });
  94. // ==========================================
  95. // VIEW MODE
  96. // ==========================================
  97. /**
  98. * View source of a document
  99. */
  100. router.get('/source/*', (req, res, next) => {
  101. let safePath = entries.parsePath(_.replace(req.path, '/source', ''));
  102. entries.fetchOriginal(safePath, {
  103. parseMarkdown: false,
  104. parseMeta: true,
  105. parseTree: false,
  106. includeMarkdown: true,
  107. includeParentInfo: false,
  108. cache: false
  109. }).then((pageData) => {
  110. if(pageData) {
  111. return res.render('pages/source', { pageData });
  112. } else {
  113. throw new Error('Invalid page path.');
  114. }
  115. }).catch((err) => {
  116. res.render('error', {
  117. message: err.message,
  118. error: {}
  119. });
  120. });
  121. });
  122. /**
  123. * View document
  124. */
  125. router.get('/*', (req, res, next) => {
  126. let safePath = entries.parsePath(req.path);
  127. entries.fetch(safePath).then((pageData) => {
  128. if(pageData) {
  129. return res.render('pages/view', { pageData });
  130. } else {
  131. res.render('error', {
  132. message: err.message,
  133. error: {}
  134. });
  135. }
  136. }).catch((err) => {
  137. res.render('error', {
  138. message: err.message,
  139. error: {}
  140. });
  141. });
  142. });
  143. module.exports = router;