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.

171 lines
5.3 KiB

  1. const utils = require('../utils/utils.js');
  2. const sql = require('../utils/sql');
  3. const Remarkable = require('remarkable');
  4. const hljs = require('highlight.js');
  5. const pandoc = require('../utils/markdownToHTML.js');
  6. const md = new Remarkable(
  7. {
  8. html: true,
  9. highlight: function (str, lang)
  10. {
  11. if (lang && hljs.getLanguage(lang))
  12. {
  13. try
  14. {
  15. return hljs.highlight(lang, str).value;
  16. }
  17. catch (err) {}
  18. }
  19. try
  20. {
  21. return hljs.highlightAuto(str).value;
  22. }
  23. catch (err) {}
  24. return ''; // use external default escaping
  25. }
  26. });
  27. module.exports=
  28. {
  29. /**
  30. * Renders a preview of the post with a link to view more
  31. *
  32. * @param res
  33. * @param post
  34. */
  35. renderPreview: function(post)
  36. {
  37. return new Promise(function(resolve, reject)
  38. {
  39. //var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
  40. var html = "<div class=\"blogPost\">";
  41. //image
  42. if(!(post.picture_url === "n/a"))
  43. {
  44. html +="<img src=\"/blogContent/headerImages/" + post.picture_url +
  45. "\" alt=\"\" style=\"width:100%; height:10%\">";
  46. }
  47. html += "<div class=\"p-4\"><div class=\"\">";
  48. //title
  49. html += "<h3><b>" + post.name + "</b></h3>";
  50. //date
  51. html += "<h5><span class=\"w3-opacity\">" +
  52. post.published.toDateString() + "</span></h5>";
  53. html +="</div>";
  54. html += "<div class=\"\">";
  55. try
  56. {
  57. sql.getCategory(post.category_id).then(function(category)
  58. {
  59. var pathName = "blogContent/posts/" + category[0].url + "/"
  60. + post.url + ".md";
  61. var markDown = utils.getFileContents(pathName).toString();
  62. markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
  63. + category[0].url + "/media/");
  64. var htmlPost = md.render(markDown).split("<p>");
  65. for(var i = 0; i < 3; i++)
  66. {
  67. html+= "<p>" + htmlPost[i];
  68. }
  69. html = html.split("<img").join("<img style=\"width: 100%; height:10%\" ");
  70. html = html.split("<code>").join("<code class='hljs cpp'>");
  71. html += " <div class=\"\">\n" +
  72. " <p class='text-center'><button class=\"btn btn-secondary btn-lg " +
  73. "w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
  74. "http://jrtechs.net/" + category[0].url + "/" + post.url +
  75. "'\"><b>READ MORE &raquo;</b></button></p>\n" +
  76. " </div>\n";
  77. html += "</div></div></div><br><br>";
  78. resolve(html)
  79. }).catch(function(error)
  80. {
  81. reject(error);
  82. });
  83. }
  84. catch(ex)
  85. {
  86. reject(ex);
  87. }
  88. });
  89. },
  90. /**
  91. * renderPost() displays a single blog post in it's entirety
  92. *
  93. * @param res result sent to user
  94. * @param post sql data about the blog post
  95. * @return {*|Promise}
  96. */
  97. renderPost: function(post)
  98. {
  99. return new Promise(function (resolve, reject)
  100. {
  101. var htmlHead = "<div class=\"blogPost\">";
  102. //image
  103. if(!(post.picture_url === "n/a"))
  104. {
  105. htmlHead +="<img src=\"/blogContent/headerImages/" + post.picture_url +
  106. "\" alt=\"\" style=\"width:100%; height:10%\">";
  107. }
  108. htmlHead += "<div class=\"p-4\"><div class=\"\">";
  109. //title
  110. htmlHead += "<h3><b>" + post.name + "</b></h3>";
  111. //date
  112. htmlHead += "<h5><span class=\"w3-opacity\">" +
  113. post.published.toDateString() + "</span></h5>";
  114. htmlHead +="</div>";
  115. var html = "<div class=\"\">";
  116. try
  117. {
  118. sql.getCategory(post.category_id).then(function(category)
  119. {
  120. const pathName = "blogContent/posts/" + category[0].url + "/"
  121. + post.url + ".md";
  122. var markDown = utils.getFileContents(pathName).toString();
  123. markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
  124. + category[0].url + "/media/");
  125. //html += md.render(markDown);
  126. pandoc.convertToHTML(markDown).then(function(result)
  127. {
  128. html +=result;
  129. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  130. html = html.split("<code>").join("<code class='hljs cpp'>");
  131. html += "</div></div></div><br><br>";
  132. resolve(htmlHead + html);
  133. }).catch(function(error)
  134. {
  135. reject(error);
  136. })
  137. });
  138. }
  139. catch(ex)
  140. {
  141. reject(ex);
  142. }
  143. });
  144. }
  145. };