Use TeX and LaTeX delimiters by default for serialize
This commit is contained in:
parent
d5c72b9d70
commit
c4f0987487
3 changed files with 43 additions and 32 deletions
|
@ -227,7 +227,7 @@ export const Commands = [
|
||||||
new Command({
|
new Command({
|
||||||
command: 'tex',
|
command: 'tex',
|
||||||
args: '<message>',
|
args: '<message>',
|
||||||
description: _td('Sends a message in TeX mode, using $ and $$ delimiters for maths'),
|
description: _td('Sends a message in TeX mode, without restrictions'),
|
||||||
runFn: function(roomId, args) {
|
runFn: function(roomId, args) {
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
||||||
if (args) {
|
if (args) {
|
||||||
|
|
|
@ -47,28 +47,12 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
|
||||||
|
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
||||||
if (forceTEX) {
|
if (forceTEX) {
|
||||||
// detect math with tex delimiters, inline: $...$, display $$...$$
|
const displayPattern = "(^|[^\\\\$])\\$\\$(([^$]|\\\\\\$)+?)\\$\\$";
|
||||||
// preferably use negative lookbehinds, not supported in all major browsers:
|
const inlinePattern = "(^|[^\\\\$])\\$(([^$]|\\\\\\$)*([^\\\\\\$]|\\\\\\$))\\$";
|
||||||
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
|
||||||
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
|
|
||||||
|
|
||||||
// conditions for display math detection $$...$$:
|
md = md.replace(RegExp(displayPattern, "gm"), function(m, p1, p2) {
|
||||||
// - pattern starts at beginning of line
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
// - left delimiter ($$) is not escaped by backslash
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
||||||
// - left delimiter is not followed by space or tab
|
|
||||||
// - pattern ends at end of line
|
|
||||||
const displayPattern = "^(?!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
|
||||||
|
|
||||||
// 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 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) {
|
|
||||||
const p1e = AllHtmlEntities.encode(p1);
|
|
||||||
return `<div data-mx-maths="${p1e}">\n\n</div>\n\n`;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
md = md.replace(RegExp(inlinePattern, "gm"), function(m, p1, p2) {
|
md = md.replace(RegExp(inlinePattern, "gm"), function(m, p1, p2) {
|
||||||
|
@ -76,24 +60,51 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
|
||||||
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// detect math with tex delimiters, inline: $...$, display $$...$$
|
||||||
|
// preferably use negative lookbehinds, not supported in all major browsers:
|
||||||
|
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
||||||
|
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
|
||||||
|
|
||||||
|
// conditions for display math detection $$...$$:
|
||||||
|
// - pattern starts at beginning of line or is not prefixed with backslash or dollar
|
||||||
|
// - left delimiter ($$) is not escaped by backslash
|
||||||
|
const displayPatternDollar = "(^|[^\\\\$])\\$\\$(([^$]|\\\\\\$)+?)\\$\\$";
|
||||||
|
|
||||||
|
// conditions for inline math detection $...$:
|
||||||
|
// - pattern starts at beginning of line, follows whitespace character or punctuation
|
||||||
|
// - pattern is on a single line
|
||||||
|
// - left and right delimiters ($) are not escaped by backslashes
|
||||||
|
// - left delimiter is not followed by whitespace character
|
||||||
|
// - right delimiter is not prefixed with whitespace character
|
||||||
|
const inlinePatternDollar = "(^|\\s|[.,!?:;])(?!\\\\)\\$(?!\\s)(([^$\\n]|\\\\\\$)*([^\\\\\\s\\$]|\\\\\\$)(?:\\\\\\$)?)\\$";
|
||||||
|
|
||||||
|
md = md.replace(RegExp(displayPatternDollar, "gm"), function(m, p1, p2) {
|
||||||
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
||||||
|
});
|
||||||
|
|
||||||
|
md = md.replace(RegExp(inlinePatternDollar, "gm"), function(m, p1, p2) {
|
||||||
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
|
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
||||||
|
});
|
||||||
|
|
||||||
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
||||||
|
|
||||||
// conditions for display math detection \[...\]:
|
// conditions for display math detection \[...\]:
|
||||||
// - pattern starts at beginning of line
|
// - pattern starts at beginning of line or is not prefixed with backslash
|
||||||
// - pattern ends at end of line
|
// - pattern is not empty
|
||||||
const displayPattern = (SdkConfig.get()['latex_maths_delims'] || {})['display_pattern'] ||
|
const displayPattern = (SdkConfig.get()['latex_maths_delims'] || {})['display_pattern'] ||
|
||||||
"^\\\\\\[(.*?)\\\\\\]$";
|
"(^|[^\\\\])\\\\\\[(?!\\\\\\])(.*?)\\\\\\]";
|
||||||
|
|
||||||
// conditions for inline math detection \(...\):
|
// conditions for inline math detection \(...\):
|
||||||
// - pattern starts at beginning of line or is not prefixed with backslash
|
// - pattern starts at beginning of line or is not prefixed with backslash
|
||||||
// (this allows escaping and requires that multiple consecutive
|
// - pattern is not empty
|
||||||
// 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'] ||
|
||||||
"(^|[^\\\\])\\\\\\((.*?)\\\\\\)";
|
"(^|[^\\\\])\\\\\\((?!\\\\\\))(.*?)\\\\\\)";
|
||||||
|
|
||||||
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1) {
|
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1, p2) {
|
||||||
const p1e = AllHtmlEntities.encode(p1);
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
return `<div data-mx-maths="${p1e}">\n\n</div>\n\n`;
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
md = md.replace(RegExp(inlinePattern, "gms"), function(m, p1, p2) {
|
md = md.replace(RegExp(inlinePattern, "gms"), function(m, p1, p2) {
|
||||||
|
|
|
@ -416,7 +416,7 @@
|
||||||
"Prepends ( ͡° ͜ʖ ͡°) to a plain-text message": "Prepends ( ͡° ͜ʖ ͡°) to a plain-text message",
|
"Prepends ( ͡° ͜ʖ ͡°) to a plain-text message": "Prepends ( ͡° ͜ʖ ͡°) to a plain-text message",
|
||||||
"Sends a message as plain text, without interpreting it as markdown": "Sends a message as plain text, without interpreting it as markdown",
|
"Sends a message as plain text, without interpreting it as markdown": "Sends a message as plain text, without interpreting it as markdown",
|
||||||
"Sends a message as html, without interpreting it as markdown": "Sends a message as html, without interpreting it as markdown",
|
"Sends a message as html, without interpreting it as markdown": "Sends a message as html, without interpreting it as markdown",
|
||||||
"Sends a message in TeX mode, using $ and $$ delimiters for maths": "Sends a message in TeX mode, using $ and $$ delimiters for maths",
|
"Sends a message in TeX mode, without restrictions": "Sends a message in TeX mode, without restrictions",
|
||||||
"Searches DuckDuckGo for results": "Searches DuckDuckGo for results",
|
"Searches DuckDuckGo for results": "Searches DuckDuckGo for results",
|
||||||
"/ddg is not a command": "/ddg is not a command",
|
"/ddg is not a command": "/ddg is not a command",
|
||||||
"To use it, just wait for autocomplete results to load and tab through them.": "To use it, just wait for autocomplete results to load and tab through them.",
|
"To use it, just wait for autocomplete results to load and tab through them.": "To use it, just wait for autocomplete results to load and tab through them.",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue