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.

161 lines
5.0 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(res, 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 += " <div class=\"\">\n" +
  66. " <p class='text-center'><button class=\"btn btn-secondary btn-lg " +
  67. "w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
  68. "http://jrtechs.net/" + category[0].url + "/" + post.url +
  69. "'\"><b>READ MORE &raquo;</b></button></p>\n" +
  70. " </div>\n";
  71. html += "</div></div></div><br><br>";
  72. res.write(html);
  73. resolve()
  74. }).catch(function(error)
  75. {
  76. console.log(error);
  77. reject(error);
  78. });
  79. }
  80. catch(ex)
  81. {
  82. reject(ex);
  83. console.log(ex);
  84. }
  85. });
  86. },
  87. /**
  88. * renderPost() displays a single blog post in it's entirety
  89. *
  90. * @param res result sent to user
  91. * @param post sql data about the blog post
  92. * @return {*|Promise}
  93. */
  94. renderPost: function(res, post)
  95. {
  96. return new Promise(function (resolve, reject)
  97. {
  98. var html = "<div class=\"blogPost\">";
  99. //image
  100. if(!(post.picture_url === "n/a"))
  101. {
  102. html +="<img src=\"/img/posts/" + post.picture_url +
  103. "\" alt=\"\" class=\"w-100\">";
  104. }
  105. html += "<div class=\"p-4\"><div class=\"\">";
  106. //title
  107. html += "<h3><b>" + post.name + "</b></h3>";
  108. //date
  109. html += "<h5><span class=\"w3-opacity\">" +
  110. post.published.toDateString() + "</span></h5>";
  111. html +="</div>";
  112. html += "<div class=\"\">";
  113. try
  114. {
  115. sql.getCategory(post.category_id).then(function(category)
  116. {
  117. var pathName = "entries/" + category[0].url + "/"
  118. + post.url + ".md";
  119. var markDown = utils.getFileContents(pathName).toString();
  120. markDown = markDown.split("![](media/").join("![](" + "../entries/"
  121. + category[0].url + "/media/");
  122. html += md.render(markDown);
  123. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  124. html += "</div></div></div><br><br>";
  125. res.write(html);
  126. resolve()
  127. });
  128. }
  129. catch(ex)
  130. {
  131. //console.log(ex);
  132. //utils.include(res, "includes/404.html");
  133. }
  134. });
  135. }
  136. };