Skip to content

Commit

Permalink
Merge pull request #15 from AaronCQL/feat-support-wiki
Browse files Browse the repository at this point in the history
feat: support wiki
  • Loading branch information
AaronCQL authored May 19, 2021
2 parents 2cb7ea8 + 53fb9d6 commit 2ce81c6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "katex-github-chrome-extension",
"version": "0.3.4",
"version": "0.3.5",
"main": "src/manifest.json",
"license": "MIT",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import renderMath from "./utils/mathRenderer";
const readmeObserver = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (!addedNode.tagName) {
continue;
}
if (addedNode.id === "readme") {
console.log("readme dom change detected");
renderMath();
Expand Down
37 changes: 20 additions & 17 deletions src/utils/mathRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import renderMathInElement from "katex/dist/contrib/auto-render";
/**
* Transforms all problematic tags to custom tags.
*
* @param {HTMLElement} readmeEl the readme element to transform
* @param {HTMLElement} renderEl the element to transform
*/
function transformProblematicTags(readmeEl) {
readmeEl.innerHTML = readmeEl.innerHTML
function transformProblematicTags(renderEl) {
renderEl.innerHTML = renderEl.innerHTML
// replace "}<em> ... </em>" with "}\@@em@start@@_ ... \@@em@end@@_"
.replace(/}<em.*?>([\s\S]*?)<\/em>/g, "}\\@@em@start@@_$1\\@@em@end@@_")
// replace ")<em> ... </em>" with ")\@@em@start@@_ ... \@@em@end@@_"
Expand All @@ -23,10 +23,10 @@ function transformProblematicTags(readmeEl) {
/**
* Renders all math blocks by calling Katex's {@function renderMathInElement}.
*
* @param {HTMLElement} readmeEl
* @param {HTMLElement} renderEl
*/
function renderMathViaKatex(readmeEl) {
renderMathInElement(readmeEl, {
function renderMathViaKatex(renderEl) {
renderMathInElement(renderEl, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
Expand Down Expand Up @@ -68,10 +68,10 @@ function renderMathViaKatex(readmeEl) {
* Removes the custom tags introduced in {@function transformProblematicTags}
* for all KaTeX nodes.
*
* @param {HTMLElement} readmeEl
* @param {HTMLElement} renderEl
*/
function removeTagsFromKatexElements(readmeEl) {
const katexElements = readmeEl.getElementsByClassName("katex");
function removeTagsFromKatexElements(renderEl) {
const katexElements = renderEl.getElementsByClassName("katex");
for (const el of katexElements) {
el.innerHTML = el.innerHTML
.replace(/\\@@em@start@@/g, "")
Expand All @@ -87,10 +87,10 @@ function removeTagsFromKatexElements(readmeEl) {
* This function must be called after {@function removeTagsFromKatexElements}
* to avoid changing the custom tags within the katex rendered blocks.
*
* @param {HTMLElement} readmeEl
* @param {HTMLElement} renderEl
*/
function revertNonProblematicTags(readmeEl) {
readmeEl.innerHTML = readmeEl.innerHTML
function revertNonProblematicTags(renderEl) {
renderEl.innerHTML = renderEl.innerHTML
.replace(/\\@@em@start@@_([\s\S]*?)\\@@em@end@@_/g, "<em>$1</em>")
.replace(/\\@@em@start@@\*([\s\S]*?)\\@@em@end@@\*/g, "<em>$1</em>")
.replace(/\\@@br@@/g, "<br>");
Expand All @@ -102,15 +102,18 @@ function revertNonProblematicTags(readmeEl) {
*/
function renderMath() {
const readmeEl = document.getElementById("readme");
const wikiEl = document.getElementById("wiki-body");

if (!readmeEl) {
if (!readmeEl && !wikiEl) {
return;
}

transformProblematicTags(readmeEl);
renderMathViaKatex(readmeEl);
removeTagsFromKatexElements(readmeEl);
revertNonProblematicTags(readmeEl);
const renderEl = readmeEl || wikiEl;

transformProblematicTags(renderEl);
renderMathViaKatex(renderEl);
removeTagsFromKatexElements(renderEl);
revertNonProblematicTags(renderEl);
}

export default renderMath;

0 comments on commit 2ce81c6

Please sign in to comment.