Improve inline latex regex matching
Signed-off-by: Sven Mäder <maeder@phys.ethz.ch>
This commit is contained in:
parent
78b3f50bfd
commit
fb57123e25
2 changed files with 20 additions and 11 deletions
|
@ -231,7 +231,7 @@ export const Commands = [
|
||||||
runFn: function(roomId, args) {
|
runFn: function(roomId, args) {
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
||||||
if (args) {
|
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 success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, html));
|
||||||
}
|
}
|
||||||
return reject(this.getUsage());
|
return reject(this.getUsage());
|
||||||
|
|
|
@ -52,18 +52,18 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
|
||||||
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
||||||
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
|
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
|
||||||
|
|
||||||
// conditions for display math detection ($$...$$):
|
// conditions for display math detection $$...$$:
|
||||||
// - left delimiter ($$) is not escaped by a backslash
|
// - pattern starts at beginning of line
|
||||||
// - pattern starts at the beginning of a line
|
// - left delimiter ($$) is not escaped by backslash
|
||||||
// - left delimiter is not followed by a space or tab character
|
// - left delimiter is not followed by space or tab
|
||||||
// - pattern ends at the end of a line
|
// - pattern ends at end of line
|
||||||
const displayPattern = "^(?!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
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
|
// - 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 whitespace character
|
||||||
// - left delimiter is not followed by a whitespace character
|
// - right delimiter is not prefixed with whitespace character
|
||||||
// - right delimiter is not preseeded by a whitespace character
|
|
||||||
const inlinePattern = "(^|\\s)(?!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)*[^\\\\\\s\\$](?:\\\\\\$)?)\\$";
|
const inlinePattern = "(^|\\s)(?!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)*[^\\\\\\s\\$](?:\\\\\\$)?)\\$";
|
||||||
|
|
||||||
md = md.replace(RegExp(displayPattern, "gm"), function(m, p1) {
|
md = md.replace(RegExp(displayPattern, "gm"), function(m, p1) {
|
||||||
|
@ -77,10 +77,19 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
// 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'] ||
|
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'] ||
|
const inlinePattern = (SdkConfig.get()['latex_maths_delims'] || {})['inline_pattern'] ||
|
||||||
"(^|\\s)\\\\\\((.*?)\\\\\\)";
|
"(^|[^\\\\])\\\\\\((.*?)\\\\\\)";
|
||||||
|
|
||||||
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1) {
|
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1) {
|
||||||
const p1e = AllHtmlEntities.encode(p1);
|
const p1e = AllHtmlEntities.encode(p1);
|
||||||
|
|
Loading…
Reference in a new issue