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.

279 lines
10 KiB

  1. const pandoc = require('node-pandoc');
  2. const utils = require('../utils/utils.js');
  3. const sql = require('../utils/sql');
  4. const argsFull = '--from markdown-markdown_in_html_blocks+raw_html --toc --toc-depth=3 -N --mathjax -t html5 --no-highlight';
  5. const argsPreview = '--mathjax -t html5';
  6. module.exports=
  7. {
  8. /**
  9. * Renders the entire blog post based on the sql data pulled
  10. * from the database.
  11. *
  12. * @param post sql data which has title, date, and header img location
  13. * @param blocks number of blocks to display for a preview or -1 for
  14. * all the blocks
  15. * @returns {Promise} async call which renders the entire blog post.
  16. */
  17. generateBlogPost: function(post, blocks)
  18. {
  19. return new Promise(function(resolve, reject)
  20. {
  21. Promise.all([module.exports.generateBlogPostHeader(post),
  22. module.exports.generateBlogPostBody(post, blocks)])
  23. .then(function()
  24. {
  25. resolve(post);
  26. }).catch(function(error)
  27. {
  28. reject(error);
  29. })
  30. });
  31. },
  32. /**
  33. * Renders the header of the blog post which contains the header image, and date
  34. * published.
  35. *
  36. * @param post sql data
  37. * @returns {string}
  38. */
  39. generateBlogPostHeader: function(post)
  40. {
  41. if(post.picture_url !== "n/a")
  42. post.hasPicture = true;
  43. post.published = post.published.toDateString();
  44. return;
  45. },
  46. /**
  47. * Method which renders the body of the blog post. This is responsible for getting
  48. * the contents of the markdown/latex file and rendering it into beautiful html.
  49. *
  50. * @param post stuff from the SQL table
  51. * @param blocks
  52. * @returns {Promise}
  53. */
  54. generateBlogPostBody: function(post, blocks)
  55. {
  56. return new Promise(function(resolve, reject)
  57. {
  58. sql.getCategory(post.category_id).then(function(category)
  59. {
  60. module.exports.generateBlogPostComponent(category[0].url, post.url, blocks).then(function(html)
  61. {
  62. post.categoryURL = category[0].url;
  63. post.blogBody = html;
  64. resolve();
  65. });
  66. });
  67. })
  68. },
  69. /**
  70. * Decomposition from Generate Blog Post used for the
  71. * blog previewer.
  72. *
  73. * @param categoryURL
  74. * @param postURL
  75. * @param blocks
  76. * @returns {Promise}
  77. */
  78. generateBlogPostComponent: function(categoryURL, postURL, blocks)
  79. {
  80. return new Promise(function(resolve, reject)
  81. {
  82. const pathName = "blogContent/posts/" + categoryURL + "/"
  83. + postURL + ".md";
  84. var markDown = utils.getFileContents(pathName).toString();
  85. markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
  86. + categoryURL + "/media/");
  87. module.exports.convertToHTML(markDown, blocks).then(function(result)
  88. {
  89. // hackey stuff to fix this open issue on pandoc https://github.com/jgm/pandoc/issues/3858
  90. //search for pattern <pre class="LANG"><code> and replace with <code class="language-LANG">
  91. var re = /\<pre class=".*?"><code>/;
  92. while (result.search(re) != -1)
  93. {
  94. var preTag = result.match(/\<pre class=".*?"><code>/g)[0];
  95. var finishIndex = preTag.split('"', 2).join('"').length;
  96. lang = preTag.substring(12, finishIndex);
  97. var newHTML = `<pre><code class="language-${lang}">`
  98. var original = `<pre class="${lang}"><code>`;
  99. result = result.split(original).join(newHTML);
  100. }
  101. result = result.split("<figcaption>").join("<figcaption style=\"visibility: hidden;\">");
  102. //this line prevents older versions of pandoc from including invalid cdm scripts
  103. result = result.split("<script src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML-full\" type=\"text/javascript\"></script>").join("");
  104. result = result.split("<script src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\" type=\"text/javascript\"></script>").join("");
  105. //stuff for youtube videos
  106. var re = /\<youtube .*?>/;
  107. //<youtube src="" />
  108. while (result.search(re) != -1)
  109. {
  110. var ytid = result.substring(result.search(re) + 14, result.search(re)+ 11 + 14);
  111. var youtubeHTML = "<div class=\"wrapper\">\n" +
  112. "\t<div class=\"youtube\" data-embed=\"" +
  113. ytid +
  114. "\" />\n" +
  115. "\t\t<div class=\"play-button\"></div>\n" +
  116. "\t</div>\n" +
  117. "</div>\n";
  118. var original = "<youtube src=\"" + ytid + "\" />";
  119. result = result.split(original).join(youtubeHTML);
  120. }
  121. var regExp = /\<customHTML .*?>/;
  122. while (result.search(regExp) != -1)
  123. {
  124. const pathName = "blogContent/posts/" + categoryURL + "/html/"
  125. + postURL + ".html";
  126. var htmlContent = utils.getFileContents(pathName).toString();
  127. result = result.split("<customHTML />").join(htmlContent);
  128. }
  129. if(blocks == -1)
  130. resolve(result);
  131. const htmlBlocks = result.split("<p>");
  132. var html = "";
  133. for(var i = 0; i < blocks; i++)
  134. {
  135. html += "<p>" + htmlBlocks[i];
  136. }
  137. resolve(html);
  138. }).catch(function(error)
  139. {
  140. reject(error);
  141. })
  142. })
  143. },
  144. /**
  145. * Converts markdown into html.
  146. *
  147. * @param markdownContents
  148. * @param type
  149. * @returns {Promise}
  150. */
  151. convertToHTML: function(markdownContents, type)
  152. {
  153. if(type == -1)
  154. {
  155. return module.exports.pandocWrapper(markdownContents, argsFull);
  156. }
  157. else
  158. {
  159. return module.exports.pandocWrapper(markdownContents, argsFull);
  160. }
  161. },
  162. pandocWrapper: function(markdownContents, pandocArgs)
  163. {
  164. return new Promise((resolve, reject)=>
  165. {
  166. // Set your callback function
  167. callback = function (err, html)
  168. {
  169. if (err)
  170. {
  171. reject(err);
  172. }
  173. if(html === undefined)
  174. {
  175. resolve("");
  176. }
  177. else
  178. {
  179. html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
  180. // html = html.split("<code>").join("<code class='hljs cpp'>");
  181. resolve(html);
  182. }
  183. };
  184. pandoc(markdownContents, pandocArgs, callback);
  185. });
  186. },
  187. /**
  188. * Renders a bunch of blog post previews to the user
  189. *
  190. * @param baseURL-- url of the page
  191. * @param posts -- sql data about the blog to render
  192. * @param currentPage -- the current page to render
  193. * @param numOfPosts -- number of blog to render
  194. * @returns {Promise} renders the html of the blog
  195. */
  196. renderBatchOfPosts: function(baseURL, posts, currentPage, numOfPosts, templateContext)
  197. {
  198. if(typeof currentPage == "undefined")
  199. {
  200. currentPage = 1;
  201. }
  202. else
  203. {
  204. currentPage = Number(currentPage);
  205. }
  206. return new Promise(function(resolve, reject)
  207. {
  208. const promises = [];
  209. for(var i = (currentPage-1) * numOfPosts; i < (currentPage-1) * numOfPosts + numOfPosts; i++)
  210. {
  211. if(i < posts.length)
  212. {
  213. promises.push(new Promise(function(res, rej)
  214. {
  215. module.exports.generateBlogPost(posts[i], posts.length === 1 ? -1: 3).then(function(tempContext)
  216. {
  217. if(posts.length != 1)
  218. {
  219. templateContext.preview = true
  220. }
  221. res(tempContext);
  222. }).catch(function(error)
  223. {
  224. rej();
  225. })
  226. }));
  227. }
  228. }
  229. Promise.all(promises).then(function(posts)
  230. {
  231. templateContext.posts = posts;
  232. if(posts.length == 1)
  233. templateContext.title = posts[0].name;
  234. else if(currentPage != 1 && baseURL === "/")
  235. templateContext.title = "page " + currentPage;
  236. resolve();
  237. }).catch(function(error)
  238. {
  239. reject(error);
  240. });
  241. });
  242. }
  243. };