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.

129 lines
4.2 KiB

  1. const pandoc = require('node-pandoc');
  2. const utils = require('../utils/utils.js');
  3. const sql = require('../utils/sql');
  4. const argsFull = '-S --base-header-level=1 --toc --toc-depth=3 -N --normalize -s --mathjax -t html5';
  5. const argsPreview = '-S --normalize -s --mathjax -t html5';
  6. module.exports=
  7. {
  8. generateBlogPost: function(post, blocks)
  9. {
  10. return new Promise(function(resolve, reject)
  11. {
  12. Promise.all([module.exports.generateBlogPostHeader(post),
  13. module.exports.generateBlogPostBody(post, blocks),
  14. module.exports.generateBlogPostFooter()]).then(function(content)
  15. {
  16. resolve(content.join(''));
  17. })
  18. });
  19. },
  20. generateBlogPostHeader: function(post)
  21. {
  22. var htmlHead = "<div class=\"blogPost\">";
  23. //image
  24. if(!(post.picture_url === "n/a"))
  25. {
  26. htmlHead +="<img src=\"/blogContent/headerImages/" + post.picture_url +
  27. "\" alt=\"\" style=\"width:100%; height:10%\">";
  28. }
  29. htmlHead += "<div class=\"p-4\"><div class=\"\">";
  30. //title
  31. htmlHead += "<h3><b>" + post.name + "</b></h3>";
  32. //date
  33. htmlHead += "<h5><span class=\"w3-opacity\">" +
  34. post.published.toDateString() + "</span></h5>";
  35. htmlHead +="</div>" + "<div class=\"\">";
  36. return htmlHead;
  37. },
  38. generateBlogPostBody: function(post, blocks)
  39. {
  40. return new Promise(function(resolve, reject)
  41. {
  42. sql.getCategory(post.category_id).then(function(category)
  43. {
  44. const pathName = "blogContent/posts/" + category[0].url + "/"
  45. + post.url + ".md";
  46. var markDown = utils.getFileContents(pathName).toString();
  47. markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
  48. + category[0].url + "/media/");
  49. module.exports.convertToHTML(markDown, 1).then(function(result)
  50. {
  51. result = result.split("<figcaption>").join("<figcaption style=\"visibility: hidden;\">");
  52. if(blocks == -1)
  53. resolve(result);
  54. var htmlBlocks = result.split("<p>");
  55. var html = "";
  56. for(var i = 0; i < blocks; i++)
  57. {
  58. html += "<p>" + htmlBlocks[i];
  59. }
  60. html += " <div class=\"\">\n" +
  61. " <p class='text-center'><button class=\"btn btn-secondary btn-lg "
  62. + "onclick=\"location.href='" +
  63. "http://jrtechs.net/" + category[0].url + "/" + post.url +
  64. "'\"><b>READ MORE &raquo;</b></button></p>\n" +
  65. " </div>\n";
  66. resolve(html);
  67. }).catch(function(error)
  68. {
  69. reject(error);
  70. })
  71. });
  72. })
  73. },
  74. generateBlogPostFooter: function()
  75. {
  76. return "</div></div></div><br><br>";
  77. },
  78. convertToHTML: function(markdownContents, type)
  79. {
  80. return new Promise(function(resolve, reject)
  81. {
  82. // Set your callback function
  83. callback = function (err, html)
  84. {
  85. if (err)
  86. {
  87. reject(err);
  88. }
  89. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  90. html = html.split("<code>").join("<code class='hljs cpp'>");
  91. resolve(html);
  92. };
  93. if(type == -1)
  94. {
  95. pandoc(markdownContents, argsFull, callback);
  96. }
  97. else
  98. {
  99. pandoc(markdownContents, argsPreview, callback);
  100. }
  101. });
  102. },
  103. }