wowchemy-utils.js 1.0 KB

12345678910111213141516171819202122232425262728
  1. /*************************************************
  2. * Wowchemy
  3. * https://github.com/wowchemy/wowchemy-hugo-modules
  4. *
  5. * Wowchemy Utilities
  6. **************************************************/
  7. /**
  8. * Fix Mermaid.js clash with Highlight.js.
  9. * Refactor Mermaid code blocks as divs to prevent Highlight parsing them and enable Mermaid to parse them.
  10. */
  11. function fixMermaid() {
  12. let mermaids = [];
  13. // Note that `language-mermaid` class is applied to <code> block within <pre>, so we wish to replace parent node.
  14. [].push.apply(mermaids, document.getElementsByClassName('language-mermaid'));
  15. for (let i = 0; i < mermaids.length; i++) {
  16. // Convert <pre><code></code></pre> block to <div> and add `mermaid` class so that Mermaid will parse it.
  17. let mermaidCodeElement = mermaids[i];
  18. let newElement = document.createElement('div');
  19. newElement.innerHTML = mermaidCodeElement.innerHTML;
  20. newElement.classList.add('mermaid');
  21. mermaidCodeElement.parentNode.replaceWith(newElement);
  22. }
  23. }
  24. export {
  25. fixMermaid,
  26. };