www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

copy-tex.mjs (2817B)


      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 
     11 var 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   var katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
     19 
     20   for (var i = 0; i < katexHtml.length; i++) {
     21     var 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   var katexMathml = fragment.querySelectorAll('.katex-mathml');
     33 
     34   for (var _i = 0; _i < katexMathml.length; _i++) {
     35     var _element = katexMathml[_i];
     36 
     37     var texSource = _element.querySelector('annotation');
     38 
     39     if (texSource) {
     40       if (_element.replaceWith) {
     41         _element.replaceWith(texSource);
     42       } else {
     43         _element.parentNode.replaceChild(texSource, _element);
     44       }
     45 
     46       texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1];
     47     }
     48   } // Switch display math to display delimiters.
     49 
     50 
     51   var displays = fragment.querySelectorAll('.katex-display annotation');
     52 
     53   for (var _i2 = 0; _i2 < displays.length; _i2++) {
     54     var _element2 = displays[_i2];
     55     _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];
     56   }
     57 
     58   return fragment;
     59 };
     60 
     61 document.addEventListener('copy', function (event) {
     62   var selection = window.getSelection();
     63 
     64   if (selection.isCollapsed) {
     65     return; // default action OK if selection is empty
     66   }
     67 
     68   var fragment = selection.getRangeAt(0).cloneContents();
     69 
     70   if (!fragment.querySelector('.katex-mathml')) {
     71     return; // default action OK if no .katex-mathml elements
     72   } // Preserve usual HTML copy/paste behavior.
     73 
     74 
     75   var html = [];
     76 
     77   for (var i = 0; i < fragment.childNodes.length; i++) {
     78     html.push(fragment.childNodes[i].outerHTML);
     79   }
     80 
     81   event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version.
     82 
     83   event.clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
     84 
     85   event.preventDefault();
     86 });