Transform <br> in Mathjax to newlines (#1866)

* Transform <br> in Mathjax to newlines

* Add missing quantifier
This commit is contained in:
Henrik Giesel 2022-05-16 06:42:10 +02:00 committed by GitHub
parent a070bec03c
commit 1b18005d6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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}>`;
});
}