Skip to content

Commit

Permalink
fix: #37 LaTex bugs
Browse files Browse the repository at this point in the history
change: from katex to mathjax
  • Loading branch information
xiangyu committed Jun 3, 2022
1 parent fcc1060 commit bb68f1f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 56 deletions.
20 changes: 0 additions & 20 deletions addon/chrome/content/lib/css/katex.min.css

This file was deleted.

1 change: 0 additions & 1 deletion addon/chrome/content/lib/js/copy-tex.min.js

This file was deleted.

1 change: 1 addition & 0 deletions addon/chrome/content/lib/js/tex-svg.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@syncfusion/ej2-navigations": "^20.1.51",
"compressing": "^1.5.1",
"esbuild": "^0.14.34",
"katex": "^0.15.6",
"replace-in-file": "^6.3.2",
"tree-model": "^1.0.7"
},
Expand Down
67 changes: 33 additions & 34 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import renderMathInElement from "katex/contrib/auto-render";
import { AddonBase, EditorMessage } from "./base";

class AddonEvents extends AddonBase {
Expand Down Expand Up @@ -392,28 +391,39 @@ class AddonEvents extends AddonBase {
`;
_window.document.body.append(style);

const texStyle = _window.document.createElement("link");
texStyle.setAttribute("rel", "stylesheet");
texStyle.setAttribute(
"href",
"chrome://Knowledge4Zotero/content/lib/css/katex.min.css"
);
_window.document.body.append(texStyle);

const copytexStyle = _window.document.createElement("link");
copytexStyle.setAttribute("rel", "stylesheet");
copytexStyle.setAttribute(
"href",
"chrome://Knowledge4Zotero/content/lib/css/copy-tex.css"
);
_window.document.body.append(copytexStyle);
if (!_window.document.getElementById("MathJax-script")) {
const messageScript = _window.document.createElement("script");
messageScript.innerHTML = `
window.addEventListener('message', (e)=>{
if(e.data.type === "renderLaTex"){
console.log("renderLaTex");
MathJax.typeset([window.document.getElementById("texView")])
}
}, false)
MathJax = {
tex: {
inlineMath: [ // start/end delimiter pairs for in-line math
['$', '$']
],
displayMath: [ // start/end delimiter pairs for display math
['$$', '$$']
],
},
startup: {
typeset: false, // Perform initial typeset?
}
};
`;
_window.document.head.append(messageScript);

const copytexScript = _window.document.createElement("script");
copytexScript.setAttribute(
"src",
"chrome://Knowledge4Zotero/content/lib/js/copy-tex.min.js"
);
_window.document.head.append(copytexScript);
const mathScript = _window.document.createElement("script");
mathScript.setAttribute("id", "MathJax-script");
mathScript.setAttribute(
"src",
"chrome://Knowledge4Zotero/content/lib/js/tex-svg.js"
);
_window.document.head.append(mathScript);
}

message.content.editorInstance._knowledgeUIInitialized = true;

Expand Down Expand Up @@ -524,18 +534,7 @@ class AddonEvents extends AddonBase {
}
Zotero.debug("Knowledge4Zotero: latex view on.");
const viewNode = editorElement.cloneNode(true) as HTMLElement;
renderMathInElement(viewNode, {
// customised options
// • auto-render specific keys, e.g.:
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true },
],
// • rendering keys, e.g.:
throwOnError: false,
});

this._Addon.views.switchEditorTexView(instance, true, viewNode);
this._Addon.views.changeEditorButtonView(
instance._iframeWindow.document.getElementById("knowledge-switchTex"),
Expand Down
1 change: 1 addition & 0 deletions src/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class AddonViews extends AddonBase {
editorCore.after(viewNode);
editorCore.style.visibility = "hidden";
viewNode.scrollTop = editorCore.scrollTop;
instance._iframeWindow.postMessage({ type: "renderLaTex" }, "*");
} else {
if (this._texNotes.includes(instance._item.id)) {
this._texNotes.splice(this._texNotes.indexOf(instance._item.id), 1);
Expand Down

0 comments on commit bb68f1f

Please sign in to comment.