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.

82 lines
2.4 KiB

6 years ago
  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. * renderPost() displays a single blog post in it's entirety
  26. *
  27. * @param res result sent to user
  28. * @param post sql data about the blog post
  29. * @return {*|Promise}
  30. */
  31. renderPost: function(res, post)
  32. {
  33. return new Promise(function (resolve, reject)
  34. {
  35. var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
  36. //image
  37. if(!(post.picture_url === "n/a"))
  38. {
  39. html +="<img src=\"/img/posts/" + post.picture_url + "\" 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\">" + post.published.toDateString() + "</span></h5>";
  46. html +="</div>";
  47. html += "<div class=\"w3-container\">";
  48. try
  49. {
  50. sql.getCategory(post.category_id).then(function(category)
  51. {
  52. var pathName = "entries/" + category[0].url + "/" + post.url + ".md";
  53. var markDown = utils.getFileContents(pathName).toString();
  54. markDown = markDown.split("![](media/").join("![](" + "../entries/" + category[0].url + "/media/");
  55. html += md.render(markDown);
  56. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  57. html += "</div></div>";
  58. res.write(html);
  59. resolve()
  60. });
  61. }
  62. catch(ex)
  63. {
  64. //console.log(ex);
  65. //utils.include(res, "includes/404.html");
  66. }
  67. });
  68. }
  69. };