wowchemy-utils.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*************************************************
  2. * Wowchemy
  3. * https://github.com/wowchemy/wowchemy-hugo-themes
  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. * @param {boolean} render
  11. */
  12. function fixMermaid(render = false) {
  13. let mermaids = [];
  14. // Note that `language-mermaid` class is applied to <code> block within <pre>, so we wish to replace parent node.
  15. [].push.apply(mermaids, document.getElementsByClassName('language-mermaid'));
  16. for (let i = 0; i < mermaids.length; i++) {
  17. // Convert <pre><code></code></pre> block to <div> and add `mermaid` class so that Mermaid will parse it.
  18. let mermaidCodeElement = mermaids[i];
  19. let newElement = document.createElement('div');
  20. newElement.innerHTML = mermaidCodeElement.innerHTML;
  21. newElement.classList.add('mermaid');
  22. if (render) {
  23. window.mermaid.mermaidAPI.render(`mermaid-${i}`, newElement.textContent, function (svgCode) {
  24. newElement.innerHTML = svgCode;
  25. });
  26. }
  27. mermaidCodeElement.parentNode.replaceWith(newElement);
  28. }
  29. console.debug(`Processed ${mermaids.length} Mermaid code blocks`);
  30. }
  31. /**
  32. * @param {Element} parent
  33. * @param {Element} child
  34. */
  35. function scrollParentToChild(parent, child) {
  36. // Where is the parent on the page?
  37. const parentRect = parent.getBoundingClientRect();
  38. // What can the client see?
  39. const parentViewableArea = {
  40. height: parent.clientHeight,
  41. width: parent.clientWidth,
  42. };
  43. // Where is the child?
  44. const childRect = child.getBoundingClientRect();
  45. // Is the child in view?
  46. const isChildInView =
  47. childRect.top >= parentRect.top && childRect.bottom <= parentRect.top + parentViewableArea.height;
  48. // If the child isn't in view, attempt to scroll the parent to it.
  49. if (!isChildInView) {
  50. // Scroll by offset relative to parent.
  51. parent.scrollTop = childRect.top + parent.scrollTop - parentRect.top;
  52. }
  53. }
  54. export {fixMermaid, scrollParentToChild};