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.

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