123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /*************************************************
- * Wowchemy
- * https://github.com/wowchemy/wowchemy-hugo-modules
- *
- * Wowchemy Utilities
- **************************************************/
- /**
- * Fix Mermaid.js clash with Highlight.js.
- * Refactor Mermaid code blocks as divs to prevent Highlight parsing them and enable Mermaid to parse them.
- */
- function fixMermaid() {
- let mermaids = [];
- // Note that `language-mermaid` class is applied to <code> block within <pre>, so we wish to replace parent node.
- [].push.apply(mermaids, document.getElementsByClassName('language-mermaid'));
- for (let i = 0; i < mermaids.length; i++) {
- // Convert <pre><code></code></pre> block to <div> and add `mermaid` class so that Mermaid will parse it.
- let mermaidCodeElement = mermaids[i];
- let newElement = document.createElement('div');
- newElement.innerHTML = mermaidCodeElement.innerHTML;
- newElement.classList.add('mermaid');
- mermaidCodeElement.parentNode.replaceWith(newElement);
- }
- }
- /**
- * @param {Element} parent
- * @param {Element} child
- */
- function scrollParentToChild(parent, child) {
- // Where is the parent on the page?
- const parentRect = parent.getBoundingClientRect();
- // What can the client see?
- const parentViewableArea = {
- height: parent.clientHeight,
- width: parent.clientWidth,
- };
- // Where is the child?
- const childRect = child.getBoundingClientRect();
- // Is the child in view?
- const isChildInView =
- childRect.top >= parentRect.top && childRect.bottom <= parentRect.top + parentViewableArea.height;
- // If the child isn't in view, attempt to scroll the parent to it.
- if (!isChildInView) {
- // Scroll by offset relative to parent.
- parent.scrollTop = childRect.top + parent.scrollTop - parentRect.top;
- }
- }
- export {fixMermaid, scrollParentToChild};
|