mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Transform <br> in Mathjax to newlines (#1866)
* Transform <br> in Mathjax to newlines * Add missing quantifier
This commit is contained in:
parent
a070bec03c
commit
1b18005d6e
1 changed files with 14 additions and 4 deletions
|
@ -13,6 +13,13 @@ const mathjaxTagPattern =
|
||||||
const mathjaxBlockDelimiterPattern = /\\\[(.*?)\\\]/gsu;
|
const mathjaxBlockDelimiterPattern = /\\\[(.*?)\\\]/gsu;
|
||||||
const mathjaxInlineDelimiterPattern = /\\\((.*?)\\\)/gsu;
|
const mathjaxInlineDelimiterPattern = /\\\((.*?)\\\)/gsu;
|
||||||
|
|
||||||
|
function trimBreaks(text: string): string {
|
||||||
|
return text
|
||||||
|
.replace(/<br[ ]*\/?>/gsu, "\n")
|
||||||
|
.replace(/^\n*/, "")
|
||||||
|
.replace(/\n*$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
export const Mathjax: DecoratedElementConstructor = class Mathjax
|
export const Mathjax: DecoratedElementConstructor = class Mathjax
|
||||||
extends HTMLElement
|
extends HTMLElement
|
||||||
implements DecoratedElement
|
implements DecoratedElement
|
||||||
|
@ -23,9 +30,10 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
|
||||||
const stored = undecorated.replace(
|
const stored = undecorated.replace(
|
||||||
mathjaxTagPattern,
|
mathjaxTagPattern,
|
||||||
(_match: string, block: string | undefined, text: string) => {
|
(_match: string, block: string | undefined, text: string) => {
|
||||||
|
const trimmed = trimBreaks(text);
|
||||||
return typeof block === "string" && block !== "false"
|
return typeof block === "string" && block !== "false"
|
||||||
? `\\[${text}\\]`
|
? `\\[${trimmed}\\]`
|
||||||
: `\\(${text}\\)`;
|
: `\\(${trimmed}\\)`;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -35,10 +43,12 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
|
||||||
static toUndecorated(stored: string): string {
|
static toUndecorated(stored: string): string {
|
||||||
return stored
|
return stored
|
||||||
.replace(mathjaxBlockDelimiterPattern, (_match: string, text: string) => {
|
.replace(mathjaxBlockDelimiterPattern, (_match: string, text: string) => {
|
||||||
return `<${Mathjax.tagName} block="true">${text}</${Mathjax.tagName}>`;
|
const trimmed = trimBreaks(text);
|
||||||
|
return `<${Mathjax.tagName} block="true">${trimmed}</${Mathjax.tagName}>`;
|
||||||
})
|
})
|
||||||
.replace(mathjaxInlineDelimiterPattern, (_match: string, text: string) => {
|
.replace(mathjaxInlineDelimiterPattern, (_match: string, text: string) => {
|
||||||
return `<${Mathjax.tagName}>${text}</${Mathjax.tagName}>`;
|
const trimmed = trimBreaks(text);
|
||||||
|
return `<${Mathjax.tagName}>${trimmed}</${Mathjax.tagName}>`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue