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.

70 lines
2.8 KiB

4 years ago
  1. My blog uses Pandoc to convert markdown into HTML documents. However, the code highlighting that Pandoc does is dull.
  2. The image below is what a Pandoc code block looked like:
  3. ![Before Using Prism](media/prism/before.png)
  4. This is the same block but rendered using [Prism](https://prismjs.com/):
  5. ![After Using Prism](media/prism/after.png)
  6. Following the [PrismJS](https://prismjs.com/) website, all you need to do is link a CSS file and some javascript files to highlight the code chunks. EZ.
  7. ```html
  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/themes/prism-coy.min.css" integrity="sha512-m/Sn0Ay9ynzYIZZbbw5Jy2QEJhXXeppOimbFNz+5qj1wUOnrzt9Q2a4fRMFqp8SOFNZ3ZwVf+Zm/ezabpqlXXQ==" crossorigin="anonymous" />
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/prism.min.js" integrity="sha512-rYNMWcr8EuYc/6mSBu0wD+hSoA4KkHvYRlmJEJGQI4bsho0OiX8fPOVB822QQZizkTUdkUCnJLnN8SAUBg9y9w==" crossorigin="anonymous"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/plugins/autoloader/prism-autoloader.min.js" integrity="sha512-X4dQSI7eXvU12QcGK4YiwB30fIdLL7bxJbpC8149YrjO/3nSLLDFZNWBol5hBYPLePVHr0IBBNKKtw9zfULPOw==" crossorigin="anonymous"></script>
  11. ```
  12. On the Pandoc side, you need to disable Pandoc from highlighting the code using the "--no-highlight" command-line argument.
  13. ```bash
  14. pandoc --from markdown-markdown_in_html_blocks+raw_html --toc --toc-depth=3 -N --mathjax -t html5 --no-highlight file.md
  15. ```
  16. The HTML code that Pandoc produces will look something like this:
  17. ```html
  18. <pre class="javascript">
  19. <code>
  20. var i = 12;
  21. </code>
  22. </pre>
  23. ```
  24. The language tags that Pandoc produces is incorrect for most javascript highlighting libraries -- documented in Pandoc [issue 2858](https://github.com/jgm/pandoc/issues/3858).
  25. The correct HTML standard for code blocks put the language tag on the code tag rather than in the pre tag.
  26. ```html
  27. <pre>
  28. <code class="language-javascript">
  29. var i = 12;
  30. </code>
  31. </pre>
  32. ```
  33. I ended up using some hacky regular expressions to convert from the Pandoc code format to the desired output.
  34. ```javascript
  35. var re = /\<pre class=".*?"><code>/;
  36. while (result.search(re) != -1)
  37. {
  38. var preTag = result.match(/\<pre class=".*?"><code>/g)[0];
  39. var finishIndex = preTag.split('"', 2).join('"').length;
  40. lang = preTag.substring(12, finishIndex);
  41. var newHTML = `<pre><code class="language-${lang}">`;
  42. var original = `<pre class="${lang}"><code>`;
  43. result = result.split(original).join(newHTML);
  44. }
  45. ```
  46. Although not greatly documented on their website, if you want to re-highlight code because you have a dynamic component of your website, you can use the "Prism.highlightAll()" function.
  47. ```javascript
  48. Prism.highlightAll();
  49. ```
  50. I am mesmerized by how fabulous code snippets look with Prism.
  51. ![After example 2 prism usage](media/prism/example2.png)