Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip <em> tags before rendering math blocks #2

Merged
merged 5 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions markdown/test.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# For testing purposes

Following should not render properly:
Following *should not* render properly:

$\text{H}_h + X_\text{x}$
Some text before, the math: $\text{H}_h + X_\text{x}$, and some *italics after*.

An `inline block` and $\text{H}_h + X_\text{x}$.

$$
\begin{aligned}
&\frac{C}{C^*} \leq \rho(n) \rightarrow \text{ for minimisation, } \rho(n) \geq 1 \\
&\frac{C}{C^*} \geq \rho(n) \rightarrow \text{ for maximisation, } \rho(n) \leq 1
\end{aligned}
$$

```js
// test code block
test();
```

- A list
- Another problematic $\text{H}_h + X_\text{x}$
40 changes: 39 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import renderMathInElement from "katex/dist/contrib/auto-render";
import { Messages } from "./utils/constants";

function stripEmTags() {
const readme = document.getElementById("readme");

if (!readme) {
return;
}

let pos = 0;
while (pos >= 0) {
// find first pos of "}<em>"
pos = readme.innerHTML.indexOf("}<em>", pos);
// replace "}<em>" and "</em>" after pos
readme.innerHTML =
readme.innerHTML.slice(0, pos) +
readme.innerHTML
.slice(pos)
.replace("}<em>", "}_")
.replace("</em>", "_");
}

pos = 0;
while (pos >= 0) {
// find first pos of "^<em>"
pos = readme.innerHTML.indexOf("^<em>", pos);
// replace "^<em>" and "</em>" after pos
readme.innerHTML =
readme.innerHTML.slice(0, pos) +
readme.innerHTML
.slice(pos)
.replace("^<em>", "^*")
.replace("</em>", "*");
}
}

function renderMath() {
const readme = document.getElementById("readme");

Expand Down Expand Up @@ -47,7 +81,9 @@ const readmeObserver = new MutationObserver((mutations) => {
for (const addedNode of mutation.addedNodes) {
if (addedNode.id === "readme") {
console.log("readme dom change detected");
return renderMath();
stripEmTags();
renderMath();
return; // break from all loops
}
}
}
Expand All @@ -58,10 +94,12 @@ readmeObserver.observe(document.body, { childList: true, subtree: true });
chrome.runtime.onMessage.addListener((request) => {
if (request.type === Messages.ROUTE_CHANGED) {
console.log("route change detected");
stripEmTags();
renderMath();
}
});

// on initial page load
console.log("initial page load detected");
stripEmTags();
renderMath();