Improve inline latex regex matching

Signed-off-by: Sven Mäder <maeder@phys.ethz.ch>
This commit is contained in:
Sven Mäder 2020-12-22 12:18:38 +01:00
parent 78b3f50bfd
commit fb57123e25
2 changed files with 20 additions and 11 deletions

View file

@ -231,7 +231,7 @@ export const Commands = [
runFn: function(roomId, args) {
if (SettingsStore.getValue("feature_latex_maths")) {
if (args) {
let html = markdownSerializeIfNeeded(args, {forceHTML: false}, {forceTEX: true});
const html = markdownSerializeIfNeeded(args, {forceHTML: false}, {forceTEX: true});
return success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, html));
}
return reject(this.getUsage());

View file

@ -52,18 +52,18 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
// conditions for display math detection ($$...$$):
// - left delimiter ($$) is not escaped by a backslash
// - pattern starts at the beginning of a line
// - left delimiter is not followed by a space or tab character
// - pattern ends at the end of a line
// conditions for display math detection $$...$$:
// - pattern starts at beginning of line
// - left delimiter ($$) is not escaped by backslash
// - left delimiter is not followed by space or tab
// - pattern ends at end of line
const displayPattern = "^(?!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
// conditions for inline math detection ($...$):
// conditions for inline math detection $...$:
// - pattern starts at beginning of line or follows whitespace character
// - left and right delimiters ($) are not escaped by backslashes
// - pattern starts at the beginning of a line or follows a whitespace character
// - left delimiter is not followed by a whitespace character
// - right delimiter is not preseeded by a whitespace character
// - left delimiter is not followed by whitespace character
// - right delimiter is not prefixed with whitespace character
const inlinePattern = "(^|\\s)(?!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)*[^\\\\\\s\\$](?:\\\\\\$)?)\\$";
md = md.replace(RegExp(displayPattern, "gm"), function(m, p1) {
@ -77,10 +77,19 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
});
} else {
// detect math with latex delimiters, inline: \(...\), display \[...\]
// conditions for display math detection \[...\]:
// - pattern starts at beginning of line
// - pattern ends at end of line
const displayPattern = (SdkConfig.get()['latex_maths_delims'] || {})['display_pattern'] ||
"^\\\\\\[(.*?)\\\\\\]$";
// conditions for inline math detection \(...\):
// - pattern starts at beginning of line or is not prefixed with backslash
// (this allows escaping and requires that multiple consecutive
// patterns are separated by at least one character)
const inlinePattern = (SdkConfig.get()['latex_maths_delims'] || {})['inline_pattern'] ||
"(^|\\s)\\\\\\((.*?)\\\\\\)";
"(^|[^\\\\])\\\\\\((.*?)\\\\\\)";
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1) {
const p1e = AllHtmlEntities.encode(p1);