mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02: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 mathjaxInlineDelimiterPattern = /\\\((.*?)\\\)/gsu;
|
||||
|
||||
function trimBreaks(text: string): string {
|
||||
return text
|
||||
.replace(/<br[ ]*\/?>/gsu, "\n")
|
||||
.replace(/^\n*/, "")
|
||||
.replace(/\n*$/, "");
|
||||
}
|
||||
|
||||
export const Mathjax: DecoratedElementConstructor = class Mathjax
|
||||
extends HTMLElement
|
||||
implements DecoratedElement
|
||||
|
@ -23,9 +30,10 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
|
|||
const stored = undecorated.replace(
|
||||
mathjaxTagPattern,
|
||||
(_match: string, block: string | undefined, text: string) => {
|
||||
const trimmed = trimBreaks(text);
|
||||
return typeof block === "string" && block !== "false"
|
||||
? `\\[${text}\\]`
|
||||
: `\\(${text}\\)`;
|
||||
? `\\[${trimmed}\\]`
|
||||
: `\\(${trimmed}\\)`;
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -35,10 +43,12 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
|
|||
static toUndecorated(stored: string): string {
|
||||
return stored
|
||||
.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) => {
|
||||
return `<${Mathjax.tagName}>${text}</${Mathjax.tagName}>`;
|
||||
const trimmed = trimBreaks(text);
|
||||
return `<${Mathjax.tagName}>${trimmed}</${Mathjax.tagName}>`;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue