Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

105 lines
2.9 KiB

  1. const routes = require('express').Router();
  2. const pageBuilder = require('../../utils/pageBuilder');
  3. const renderBlogPost = require('../../blog/renderBlogPost');
  4. const utils = require('../../utils/utils');
  5. const whiskers = require('whiskers');
  6. const url = require('url');
  7. const fs = require('fs');
  8. // routes.get('/', (request, result) =>
  9. // {
  10. // //do something later
  11. // result.write("Not implemented yet.");
  12. // result.end();
  13. // });
  14. const photosBaseDir = "blogContent/photos";
  15. photoPageBuilder = function(filename, request, templateContext)
  16. {
  17. const imagePath = "/" + photosBaseDir + filename + "/";
  18. return new Promise((resolve, reject)=>
  19. {
  20. if(fs.existsSync(photosBaseDir + filename + "/post.md"))
  21. {
  22. var markdownContent = utils.getFileContents(
  23. photosBaseDir + filename + "/post.md");
  24. renderBlogPost.pandocWrapper(markdownContent, "-t html5")
  25. .then((html)=>
  26. {
  27. templateContext.mainPost = html;
  28. templateContext.images = [];
  29. fs.readdirSync(photosBaseDir + filename).forEach(file=>
  30. {
  31. if(file.includes('.jpg')) //doesn't pick up mark down files
  32. {
  33. templateContext.images.push({full:imagePath+ file,
  34. preview:imagePath + "preview/" + file});
  35. }
  36. });
  37. resolve();
  38. }).catch((error)=>
  39. {
  40. reject(error);
  41. })
  42. }
  43. else if(fs.existsSync(photosBaseDir + filename + "/dir.md"))
  44. {
  45. templateContext.folders = [];
  46. templateContext.directory = true;
  47. fs.readdirSync(photosBaseDir + filename).forEach(file=>
  48. {
  49. if(!file.includes('.md') && !file.includes('.jpg')) //only pick up folders
  50. {
  51. templateContext.folders.push({url: "/photos" + filename + "/" + file,
  52. img:imagePath + file + "/1.jpg"});
  53. }
  54. });
  55. renderBlogPost.pandocWrapper(utils.getFileContents(
  56. photosBaseDir + filename + "/dir.md"), "-t html5")
  57. .then((html)=>
  58. {
  59. templateContext.mainPost = html;
  60. resolve();
  61. }).catch((error)=>
  62. {
  63. console.log(error);
  64. templateContext.error = true;
  65. resolve();
  66. });
  67. }
  68. else
  69. {
  70. templateContext.error = true;
  71. resolve();
  72. }
  73. })
  74. };
  75. routes.get('*', (request, result) =>
  76. {
  77. pageBuilder.buildPageWithTemplate(request, result,
  78. photoPageBuilder, "photos/photosEntry.html");
  79. });
  80. routes.get('/', (request, result) =>
  81. {
  82. console.log("wham");
  83. pageBuilder.buildPageWithTemplate(request, result,
  84. photoPageBuilder, "photos/photosEntry.html");
  85. });
  86. module.exports = routes;