contrast-hugo/static/katex/contrib/copy-tex.mjs

87 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2021-02-28 15:34:43 -05:00
// Set these to how you want inline and display math to be delimited.
2021-03-11 15:55:04 -05:00
var defaultCopyDelimiters = {
2021-02-28 15:34:43 -05:00
inline: ['$', '$'],
// alternative: ['\(', '\)']
display: ['$$', '$$'] // alternative: ['\[', '\]']
}; // Replace .katex elements with their TeX source (<annotation> element).
// Modifies fragment in-place. Useful for writing your own 'copy' handler,
// as in copy-tex.js.
2021-03-11 15:55:04 -05:00
var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters) {
2021-02-28 15:34:43 -05:00
if (copyDelimiters === void 0) {
copyDelimiters = defaultCopyDelimiters;
}
// Remove .katex-html blocks that are preceded by .katex-mathml blocks
// (which will get replaced below).
2021-03-11 15:55:04 -05:00
var katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
2021-02-28 15:34:43 -05:00
2021-03-11 15:55:04 -05:00
for (var i = 0; i < katexHtml.length; i++) {
var element = katexHtml[i];
2021-02-28 15:34:43 -05:00
if (element.remove) {
element.remove(null);
} else {
element.parentNode.removeChild(element);
}
} // Replace .katex-mathml elements with their annotation (TeX source)
// descendant, with inline delimiters.
2021-03-11 15:55:04 -05:00
var katexMathml = fragment.querySelectorAll('.katex-mathml');
2021-02-28 15:34:43 -05:00
2021-03-11 15:55:04 -05:00
for (var _i = 0; _i < katexMathml.length; _i++) {
var _element = katexMathml[_i];
var texSource = _element.querySelector('annotation');
2021-02-28 15:34:43 -05:00
if (texSource) {
2021-03-11 15:55:04 -05:00
if (_element.replaceWith) {
_element.replaceWith(texSource);
2021-02-28 15:34:43 -05:00
} else {
2021-03-11 15:55:04 -05:00
_element.parentNode.replaceChild(texSource, _element);
2021-02-28 15:34:43 -05:00
}
texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1];
}
} // Switch display math to display delimiters.
2021-03-11 15:55:04 -05:00
var displays = fragment.querySelectorAll('.katex-display annotation');
2021-02-28 15:34:43 -05:00
2021-03-11 15:55:04 -05:00
for (var _i2 = 0; _i2 < displays.length; _i2++) {
var _element2 = displays[_i2];
_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];
2021-02-28 15:34:43 -05:00
}
return fragment;
};
document.addEventListener('copy', function (event) {
2021-03-11 15:55:04 -05:00
var selection = window.getSelection();
2021-02-28 15:34:43 -05:00
if (selection.isCollapsed) {
return; // default action OK if selection is empty
}
2021-03-11 15:55:04 -05:00
var fragment = selection.getRangeAt(0).cloneContents();
2021-02-28 15:34:43 -05:00
if (!fragment.querySelector('.katex-mathml')) {
return; // default action OK if no .katex-mathml elements
} // Preserve usual HTML copy/paste behavior.
2021-03-11 15:55:04 -05:00
var html = [];
2021-02-28 15:34:43 -05:00
2021-03-11 15:55:04 -05:00
for (var i = 0; i < fragment.childNodes.length; i++) {
2021-02-28 15:34:43 -05:00
html.push(fragment.childNodes[i].outerHTML);
}
event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version.
event.clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
event.preventDefault();
});