copy-tex.mjs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Set these to how you want inline and display math to be delimited.
  2. var 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. function katexReplaceWithTex(fragment, copyDelimiters) {
  11. if (copyDelimiters === void 0) {
  12. copyDelimiters = defaultCopyDelimiters;
  13. }
  14. // Remove .katex-html blocks that are preceded by .katex-mathml blocks
  15. // (which will get replaced below).
  16. var katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
  17. for (var i = 0; i < katexHtml.length; i++) {
  18. var element = katexHtml[i];
  19. if (element.remove) {
  20. element.remove();
  21. } else if (element.parentNode) {
  22. element.parentNode.removeChild(element);
  23. }
  24. }
  25. // Replace .katex-mathml elements with their annotation (TeX source)
  26. // descendant, with inline delimiters.
  27. var katexMathml = fragment.querySelectorAll('.katex-mathml');
  28. for (var _i = 0; _i < katexMathml.length; _i++) {
  29. var _element = katexMathml[_i];
  30. var texSource = _element.querySelector('annotation');
  31. if (texSource) {
  32. if (_element.replaceWith) {
  33. _element.replaceWith(texSource);
  34. } else if (_element.parentNode) {
  35. _element.parentNode.replaceChild(texSource, _element);
  36. }
  37. texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1];
  38. }
  39. }
  40. // Switch display math to display delimiters.
  41. var displays = fragment.querySelectorAll('.katex-display annotation');
  42. for (var _i2 = 0; _i2 < displays.length; _i2++) {
  43. var _element2 = displays[_i2];
  44. _element2.innerHTML = copyDelimiters.display[0] + _element2.innerHTML.substr(copyDelimiters.inline[0].length, _element2.innerHTML.length - copyDelimiters.inline[0].length - copyDelimiters.inline[1].length) + copyDelimiters.display[1];
  45. }
  46. return fragment;
  47. }
  48. // Return <div class="katex"> element containing node, or null if not found.
  49. function closestKatex(node) {
  50. // If node is a Text Node, for example, go up to containing Element,
  51. // where we can apply the `closest` method.
  52. var element = node instanceof Element ? node : node.parentElement;
  53. return element && element.closest('.katex');
  54. }
  55. // Global copy handler to modify behavior on/within .katex elements.
  56. document.addEventListener('copy', function (event) {
  57. var selection = window.getSelection();
  58. if (!selection || selection.isCollapsed || !event.clipboardData) {
  59. return; // default action OK if selection is empty or unchangeable
  60. }
  61. var clipboardData = event.clipboardData;
  62. var range = selection.getRangeAt(0);
  63. // When start point is within a formula, expand to entire formula.
  64. var startKatex = closestKatex(range.startContainer);
  65. if (startKatex) {
  66. range.setStartBefore(startKatex);
  67. }
  68. // Similarly, when end point is within a formula, expand to entire formula.
  69. var endKatex = closestKatex(range.endContainer);
  70. if (endKatex) {
  71. range.setEndAfter(endKatex);
  72. }
  73. var fragment = range.cloneContents();
  74. if (!fragment.querySelector('.katex-mathml')) {
  75. return; // default action OK if no .katex-mathml elements
  76. }
  77. var htmlContents = Array.prototype.map.call(fragment.childNodes, el => el instanceof Text ? el.textContent : el.outerHTML).join('');
  78. // Preserve usual HTML copy/paste behavior.
  79. clipboardData.setData('text/html', htmlContents);
  80. // Rewrite plain-text version.
  81. clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent);
  82. // Prevent normal copy handling.
  83. event.preventDefault();
  84. });