copy-tex.mjs 3.6 KB

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