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.

155 lines
4.8 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 += " <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. resolve(html)
  73. }).catch(function(error)
  74. {
  75. reject(error);
  76. });
  77. }
  78. catch(ex)
  79. {
  80. reject(ex);
  81. }
  82. });
  83. },
  84. /**
  85. * renderPost() displays a single blog post in it's entirety
  86. *
  87. * @param res result sent to user
  88. * @param post sql data about the blog post
  89. * @return {*|Promise}
  90. */
  91. renderPost: function(post)
  92. {
  93. return new Promise(function (resolve, reject)
  94. {
  95. var html = "<div class=\"blogPost\">";
  96. //image
  97. if(!(post.picture_url === "n/a"))
  98. {
  99. html +="<img src=\"/img/posts/" + post.picture_url +
  100. "\" alt=\"\" class=\"w-100\">";
  101. }
  102. html += "<div class=\"p-4\"><div class=\"\">";
  103. //title
  104. html += "<h3><b>" + post.name + "</b></h3>";
  105. //date
  106. html += "<h5><span class=\"w3-opacity\">" +
  107. post.published.toDateString() + "</span></h5>";
  108. html +="</div>";
  109. html += "<div class=\"\">";
  110. try
  111. {
  112. sql.getCategory(post.category_id).then(function(category)
  113. {
  114. var pathName = "entries/" + category[0].url + "/"
  115. + post.url + ".md";
  116. var markDown = utils.getFileContents(pathName).toString();
  117. markDown = markDown.split("(media/").join("(" + "../entries/"
  118. + category[0].url + "/media/");
  119. html += md.render(markDown);
  120. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  121. html += "</div></div></div><br><br>";
  122. resolve(html);
  123. });
  124. }
  125. catch(ex)
  126. {
  127. reject(ex);
  128. }
  129. });
  130. }
  131. };