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.

157 lines
4.9 KiB

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