Skip to content

Commit a205155

Browse files
Merge pull request #7 from YabamenoTate/main
Multiline support + \(\) \[\] support
2 parents 6c51296 + 862cc0c commit a205155

File tree

5 files changed

+42
-151
lines changed

5 files changed

+42
-151
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
# LaTeX Renderer
22
This is a [BetterDiscord](https://betterdiscord.app/) plugin using [MathJax](https://www.mathjax.org/) to natively render LaTeX math equations inside Discord.
33

4-
# Usage
5-
You need to use simple or double dollars inside a Discord inline code (this is to prevent markdown interpretation of special characters).
4+
## Usage
5+
You need to use simple or double dollars inside a Discord inline or mutiline code block (this is to prevent markdown interpretation of special characters).
6+
You can also use `\( \)` and `\[ \]`.
67

78
Examples (just copy-paste)
89
- Inline: `` I want to say `$\forall x\in\mathbb{N}, x\ge 0$` ! ``
10+
- Inline: `` I want to say `\(\forall x\in\mathbb{N}, x\ge 0\)` ! ``
11+
- Inline: `` I want to say ```$\forall x\in\mathbb{N}, x\ge 0$``` ! ``
12+
- Inline: `` I want to say ```\(\forall x\in\mathbb{N}, x\ge 0\)``` ! ``
913
- Block: `` Voilà: `$$\int_0^\infty e^{-x^2}dx=\frac{\sqrt{\pi}}{2}$$` ``
14+
- Block: `` Voilà: `\[\int_0^\infty e^{-x^2}dx=\frac{\sqrt{\pi}}{2}\]` ``
15+
- Block: `` Voilà: ```$$\int_0^\infty e^{-x^2}dx=\frac{\sqrt{\pi}}{2}$$``` ``
16+
- Block: `` Voilà: ```\[\int_0^\infty e^{-x^2}dx=\frac{\sqrt{\pi}}{2}\]``` ``
1017

11-
# Installation
12-
You can manually download the plugin here
13-
- Remote: [src/LaTeX-remote.plugin.js](src/LaTeX-remote.plugin.js)
14-
- Bundled: [dist/LaTeX.plugin.js](dist/LaTeX.plugin.js)
18+
## Installation
19+
Download the plugin on [BetterDiscord's website](https://betterdiscord.app/plugin/LaTeX%20Renderer).
20+
21+
You can also download latest here [dist/LaTeX.plugin.js](dist/LaTeX.plugin.js)
1522

1623
Simply put it in BetterDiscord's plugin folder
1724

18-
## Building local version
19-
Run `npm install` and `npm run build`
25+
## Building the plugin
26+
Simply clone the repo then run `npm install` and `npm run build`
2027

21-
Then put `dist/LaTeX.plugin.js` in BetterDiscord's plugin folder.
28+
Then put `dist/LaTeX.plugin.js` in BetterDiscord's plugin folder.

dist/LaTeX.plugin.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": "https://github.com/BinaryQuantumSoul/discord-latex",
77
"description": "Renders LaTeX equations using MathJax",
88
"keywords": ["LaTeX","MathJax","Better","Discord"],
9-
"version": "1.0.4",
9+
"version": "1.0.5",
1010

1111
"main": "./src/LaTeX.js",
1212
"scripts": {

src/LaTeX-remote.plugin.js

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/LaTeX.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,20 @@ export default class Plugin {
9090
const codeText = codeElement.innerHTML;
9191
const sanitize = x => x.replace('\\unicode', '');
9292

93-
if (codeText.startsWith("$$") && codeText.endsWith("$$")) {
94-
codeElement.outerHTML = "<span>mthjxblock" + sanitize(codeText).slice(2, -2) + "mthjxblockend</span>";
93+
// handles multiline code blocks
94+
function findCodeElement(codeElement) {
95+
const lineblockElem = codeElement.closest("pre");
96+
return lineblockElem ? lineblockElem : codeElement;
97+
};
98+
99+
if ((codeText.startsWith("$$") && codeText.endsWith("$$")) || (codeText.startsWith("\\[") && codeText.endsWith("\\]"))) {
100+
findCodeElement(codeElement).outerHTML = "<span>mthjxblock" + sanitize(codeText).slice(2, -2) + "mthjxblockend</span>";
95101
containsTex = true;
96102
} else if (codeText.startsWith("$") && codeText.endsWith("$")) {
97-
codeElement.outerHTML = "<span>mthjxinline" + sanitize(codeText).slice(1, -1) + "mthjxinlineend</span>";
103+
findCodeElement(codeElement).outerHTML = "<span>mthjxinline" + sanitize(codeText).slice(1, -1) + "mthjxinlineend</span>";
104+
containsTex = true;
105+
} else if (codeText.startsWith("\\(") && codeText.endsWith("\\)")) {
106+
findCodeElement(codeElement).outerHTML = "<span>mthjxinline" + sanitize(codeText).slice(2, -2) + "mthjxinlineend</span>";
98107
containsTex = true;
99108
}
100109
});

0 commit comments

Comments
 (0)