You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8 KiB

  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. var katexReplaceWithTex = 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(null);
  20. } else {
  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 {
  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. document.addEventListener('copy', function (event) {
  46. var selection = window.getSelection();
  47. if (selection.isCollapsed) {
  48. return; // default action OK if selection is empty
  49. }
  50. var fragment = selection.getRangeAt(0).cloneContents();
  51. if (!fragment.querySelector('.katex-mathml')) {
  52. return; // default action OK if no .katex-mathml elements
  53. } // Preserve usual HTML copy/paste behavior.
  54. var html = [];
  55. for (var i = 0; i < fragment.childNodes.length; i++) {
  56. html.push(fragment.childNodes[i].outerHTML);
  57. }
  58. event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version.
  59. event.clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
  60. event.preventDefault();
  61. });