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.

159 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(res, post)
  31. {
  32. return new Promise(function(resolve, reject)
  33. {
  34. var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
  35. //image
  36. if(!(post.picture_url === "n/a"))
  37. {
  38. html +="<img src=\"/img/posts/" + post.picture_url +
  39. "\" alt=\"\" style=\"width:100%\">";
  40. }
  41. html += "<div class=\"w3-container\">";
  42. //title
  43. html += "<h3><b>" + post.name + "</b></h3>";
  44. //date
  45. html += "<h5><span class=\"w3-opacity\">" +
  46. post.published.toDateString() + "</span></h5>";
  47. html +="</div>";
  48. html += "<div class=\"w3-container\">";
  49. try
  50. {
  51. sql.getCategory(post.category_id).then(function(category)
  52. {
  53. var pathName = "entries/" + category[0].url + "/"
  54. + post.url + ".md";
  55. var markDown = utils.getFileContents(pathName).toString();
  56. markDown = markDown.split("![](media/").join("![](" + "../entries/"
  57. + category[0].url + "/media/");
  58. var htmlPost = md.render(markDown).split("<p>");
  59. for(var i = 0; i < 3; i++)
  60. {
  61. html+= "<p>" + htmlPost[i];
  62. }
  63. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  64. html += " <div class=\"w3-row\">\n" +
  65. " <p class='w3-center'><button class=\"w3-button " +
  66. "w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
  67. "http://jrtechs.net/" + category[0].url + "/" + post.url +
  68. "'\"><b>READ MORE &raquo;</b></button></p>\n" +
  69. " </div>\n";
  70. html += "</div></div>";
  71. res.write(html);
  72. resolve()
  73. }).catch(function(error)
  74. {
  75. console.log(error);
  76. reject(error);
  77. });
  78. }
  79. catch(ex)
  80. {
  81. reject(ex);
  82. console.log(ex);
  83. }
  84. });
  85. },
  86. /**
  87. * renderPost() displays a single blog post in it's entirety
  88. *
  89. * @param res result sent to user
  90. * @param post sql data about the blog post
  91. * @return {*|Promise}
  92. */
  93. renderPost: function(res, post)
  94. {
  95. return new Promise(function (resolve, reject)
  96. {
  97. var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
  98. //image
  99. if(!(post.picture_url === "n/a"))
  100. {
  101. html +="<img src=\"/img/posts/" + post.picture_url +
  102. "\" alt=\"\" style=\"width:100%\">";
  103. }
  104. html += "<div class=\"w3-container\">";
  105. //title
  106. html += "<h3><b>" + post.name + "</b></h3>";
  107. //date
  108. html += "<h5><span class=\"w3-opacity\">" +
  109. post.published.toDateString() + "</span></h5>";
  110. html +="</div>";
  111. html += "<div class=\"w3-container\">";
  112. try
  113. {
  114. sql.getCategory(post.category_id).then(function(category)
  115. {
  116. var pathName = "entries/" + category[0].url + "/"
  117. + post.url + ".md";
  118. var markDown = utils.getFileContents(pathName).toString();
  119. markDown = markDown.split("![](media/").join("![](" + "../entries/"
  120. + category[0].url + "/media/");
  121. html += md.render(markDown);
  122. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  123. html += "</div></div>";
  124. res.write(html);
  125. resolve()
  126. });
  127. }
  128. catch(ex)
  129. {
  130. //console.log(ex);
  131. //utils.include(res, "includes/404.html");
  132. }
  133. });
  134. }
  135. };