katex: copy-paste support for Latex code, upgraded to katex 0.16.0 (#54)

This commit is contained in:
qnxor 2022-11-16 19:39:48 +00:00 committed by GitHub
parent b5d2a8a3ea
commit abe2daf0f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 6 deletions

View File

@ -5,9 +5,13 @@ function ensureKaTeX() {
return loadScript("/plugins/discourse-math/katex/katex.min.js").then(() => { return loadScript("/plugins/discourse-math/katex/katex.min.js").then(() => {
return loadScript("/plugins/discourse-math/katex/katex.min.css", { return loadScript("/plugins/discourse-math/katex/katex.min.css", {
css: true, css: true,
}).then(() => { })
return loadScript("/plugins/discourse-math/katex/mhchem.min.js"); .then(() => {
}); return loadScript("/plugins/discourse-math/katex/mhchem.min.js");
})
.then(() => {
return loadScript("/plugins/discourse-math/katex/copy-tex.min.js");
});
}); });
} }

View File

@ -8,9 +8,10 @@
```bash ```bash
DMPATH=/path/to/discourse-math # set this to your path to the discourse-math repo DMPATH=/path/to/discourse-math # set this to your path to the discourse-math repo
cd /tmp
wget -O- https://github.com/KaTeX/KaTeX/releases/latest/download/katex.tar.gz | tar -zx wget -O- https://github.com/KaTeX/KaTeX/releases/latest/download/katex.tar.gz | tar -zx
cp katex/fonts/*.woff* $DMPATH/public/katex/fonts/ cp katex/fonts/*.woff* $DMPATH/public/katex/fonts/
cp katex/katex.min.* katex/contrib/mhchem.min.js $DMPATH/public/katex/ cp katex/katex.min.* katex/contrib/{mhchem,copy-tex}.min.js $DMPATH/public/katex/
sed -i "s~url(fonts/~url(/plugins/discourse-math/katex/fonts/~g" $DMPATH/public/katex/katex.min.css sed -i "s~url(fonts/~url(/plugins/discourse-math/katex/fonts/~g" $DMPATH/public/katex/katex.min.css
``` ```

1
public/katex/copy-tex.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={},t={inline:["$","$"],display:["$$","$$"]};var n=function(e,n){void 0===n&&(n=t);for(var r=e.querySelectorAll(".katex-mathml + .katex-html"),a=0;a<r.length;a++){var o=r[a];o.remove?o.remove():o.parentNode&&o.parentNode.removeChild(o)}for(var i=e.querySelectorAll(".katex-mathml"),l=0;l<i.length;l++){var f=i[l],c=f.querySelector("annotation");c&&(f.replaceWith?f.replaceWith(c):f.parentNode&&f.parentNode.replaceChild(c,f),c.innerHTML=n.inline[0]+c.innerHTML+n.inline[1])}for(var d=e.querySelectorAll(".katex-display annotation"),s=0;s<d.length;s++){var p=d[s];p.innerHTML=n.display[0]+p.innerHTML.substr(n.inline[0].length,p.innerHTML.length-n.inline[0].length-n.inline[1].length)+n.display[1]}return e};function r(e){var t=e instanceof Element?e:e.parentElement;return t&&t.closest(".katex")}return document.addEventListener("copy",(function(e){var t=window.getSelection();if(!t.isCollapsed&&e.clipboardData){var a=e.clipboardData,o=t.getRangeAt(0),i=r(o.startContainer);i&&o.setStartBefore(i);var l=r(o.endContainer);l&&o.setEndAfter(l);var f=o.cloneContents();if(f.querySelector(".katex-mathml")){var c=Array.prototype.map.call(f.childNodes,(function(e){return e instanceof Text?e.textContent:e.outerHTML})).join("");a.setData("text/html",c),a.setData("text/plain",n(f).textContent),e.preventDefault()}}})),e=e.default}()}));

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long