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.

162 lines
5.0 KiB

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