1// Set these to how you want inline and display math to be delimited. 2const defaultCopyDelimiters = { 3 inline: ['$', '$'], 4 // alternative: ['\(', '\)'] 5 display: ['$$', '$$'] // alternative: ['\[', '\]'] 6 7}; // Replace .katex elements with their TeX source (<annotation> element). 8// Modifies fragment in-place. Useful for writing your own 'copy' handler, 9// as in copy-tex.js. 10 11const katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters) { 12 if (copyDelimiters === void 0) { 13 copyDelimiters = defaultCopyDelimiters; 14 } 15 16 // Remove .katex-html blocks that are preceded by .katex-mathml blocks 17 // (which will get replaced below). 18 const katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html'); 19 20 for (let i = 0; i < katexHtml.length; i++) { 21 const element = katexHtml[i]; 22 23 if (element.remove) { 24 element.remove(null); 25 } else { 26 element.parentNode.removeChild(element); 27 } 28 } // Replace .katex-mathml elements with their annotation (TeX source) 29 // descendant, with inline delimiters. 30 31 32 const katexMathml = fragment.querySelectorAll('.katex-mathml'); 33 34 for (let i = 0; i < katexMathml.length; i++) { 35 const element = katexMathml[i]; 36 const texSource = element.querySelector('annotation'); 37 38 if (texSource) { 39 if (element.replaceWith) { 40 element.replaceWith(texSource); 41 } else { 42 element.parentNode.replaceChild(texSource, element); 43 } 44 45 texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1]; 46 } 47 } // Switch display math to display delimiters. 48 49 50 const displays = fragment.querySelectorAll('.katex-display annotation'); 51 52 for (let i = 0; i < displays.length; i++) { 53 const element = displays[i]; 54 element.innerHTML = copyDelimiters.display[0] + element.innerHTML.substr(copyDelimiters.inline[0].length, element.innerHTML.length - copyDelimiters.inline[0].length - copyDelimiters.inline[1].length) + copyDelimiters.display[1]; 55 } 56 57 return fragment; 58}; 59 60document.addEventListener('copy', function (event) { 61 const selection = window.getSelection(); 62 63 if (selection.isCollapsed) { 64 return; // default action OK if selection is empty 65 } 66 67 const fragment = selection.getRangeAt(0).cloneContents(); 68 69 if (!fragment.querySelector('.katex-mathml')) { 70 return; // default action OK if no .katex-mathml elements 71 } // Preserve usual HTML copy/paste behavior. 72 73 74 const html = []; 75 76 for (let i = 0; i < fragment.childNodes.length; i++) { 77 html.push(fragment.childNodes[i].outerHTML); 78 } 79 80 event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version. 81 82 event.clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling. 83 84 event.preventDefault(); 85}); 86