Remove /tex command
This commit is contained in:
parent
c4f0987487
commit
bcc0697710
3 changed files with 44 additions and 83 deletions
|
@ -48,7 +48,6 @@ import SettingsStore from "./settings/SettingsStore";
|
||||||
import {UIFeature} from "./settings/UIFeature";
|
import {UIFeature} from "./settings/UIFeature";
|
||||||
import {CHAT_EFFECTS} from "./effects"
|
import {CHAT_EFFECTS} from "./effects"
|
||||||
import CallHandler from "./CallHandler";
|
import CallHandler from "./CallHandler";
|
||||||
import {markdownSerializeIfNeeded} from './editor/serialize';
|
|
||||||
|
|
||||||
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
|
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
|
||||||
interface HTMLInputEvent extends Event {
|
interface HTMLInputEvent extends Event {
|
||||||
|
@ -224,23 +223,6 @@ export const Commands = [
|
||||||
},
|
},
|
||||||
category: CommandCategories.messages,
|
category: CommandCategories.messages,
|
||||||
}),
|
}),
|
||||||
new Command({
|
|
||||||
command: 'tex',
|
|
||||||
args: '<message>',
|
|
||||||
description: _td('Sends a message in TeX mode, without restrictions'),
|
|
||||||
runFn: function(roomId, args) {
|
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
|
||||||
if (args) {
|
|
||||||
const html = markdownSerializeIfNeeded(args, {forceHTML: false}, {forceTEX: true});
|
|
||||||
return success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, html));
|
|
||||||
}
|
|
||||||
return reject(this.getUsage());
|
|
||||||
} else {
|
|
||||||
return reject("Render LaTeX maths in messages needs to be enabled in Labs");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
category: CommandCategories.messages,
|
|
||||||
}),
|
|
||||||
new Command({
|
new Command({
|
||||||
command: 'ddg',
|
command: 'ddg',
|
||||||
args: '<query>',
|
args: '<query>',
|
||||||
|
|
|
@ -41,77 +41,63 @@ export function mdSerialize(model: EditorModel) {
|
||||||
}, "");
|
}, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {}, {forceTEX = false} = {}) {
|
export function htmlSerializeIfNeeded(model: EditorModel, {forceHTML = false} = {}) {
|
||||||
|
let md = mdSerialize(model);
|
||||||
// copy of raw input to remove unwanted math later
|
// copy of raw input to remove unwanted math later
|
||||||
const orig = md;
|
const orig = md;
|
||||||
|
|
||||||
if (SettingsStore.getValue("feature_latex_maths")) {
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
||||||
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)\\$";
|
||||||
|
|
||||||
md = md.replace(RegExp(displayPattern, "gm"), function(m, p1, p2) {
|
// conditions for display math detection $$...$$:
|
||||||
const p2e = AllHtmlEntities.encode(p2);
|
// - pattern starts at beginning of line or is not prefixed with backslash or dollar
|
||||||
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
// - left delimiter ($$) is not escaped by backslash
|
||||||
});
|
const displayPatternDollar = "(^|[^\\\\$])\\$\\$(([^$]|\\\\\\$)+?)\\$\\$";
|
||||||
|
|
||||||
md = md.replace(RegExp(inlinePattern, "gm"), function(m, p1, p2) {
|
// conditions for inline math detection $...$:
|
||||||
const p2e = AllHtmlEntities.encode(p2);
|
// - pattern starts at beginning of line, follows whitespace character or punctuation
|
||||||
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
// - pattern is on a single line
|
||||||
});
|
// - left and right delimiters ($) are not escaped by backslashes
|
||||||
} else {
|
// - left delimiter is not followed by whitespace character
|
||||||
// detect math with tex delimiters, inline: $...$, display $$...$$
|
// - right delimiter is not prefixed with whitespace character
|
||||||
// preferably use negative lookbehinds, not supported in all major browsers:
|
const inlinePatternDollar = "(^|\\s|[.,!?:;])(?!\\\\)\\$(?!\\s)(([^$\\n]|\\\\\\$)*([^\\\\\\s\\$]|\\\\\\$)(?:\\\\\\$)?)\\$";
|
||||||
// const displayPattern = "^(?<!\\\\)\\$\\$(?![ \\t])(([^$]|\\\\\\$)+?)\\$\\$$";
|
|
||||||
// const inlinePattern = "(?:^|\\s)(?<!\\\\)\\$(?!\\s)(([^$]|\\\\\\$)+?)(?<!\\\\|\\s)\\$";
|
|
||||||
|
|
||||||
// conditions for display math detection $$...$$:
|
md = md.replace(RegExp(displayPatternDollar, "gm"), function(m, p1, p2) {
|
||||||
// - pattern starts at beginning of line or is not prefixed with backslash or dollar
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
// - left delimiter ($$) is not escaped by backslash
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
||||||
const displayPatternDollar = "(^|[^\\\\$])\\$\\$(([^$]|\\\\\\$)+?)\\$\\$";
|
});
|
||||||
|
|
||||||
// conditions for inline math detection $...$:
|
md = md.replace(RegExp(inlinePatternDollar, "gm"), function(m, p1, p2) {
|
||||||
// - pattern starts at beginning of line, follows whitespace character or punctuation
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
// - pattern is on a single line
|
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
||||||
// - 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) {
|
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
||||||
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) {
|
// conditions for display math detection \[...\]:
|
||||||
const p2e = AllHtmlEntities.encode(p2);
|
// - pattern starts at beginning of line or is not prefixed with backslash
|
||||||
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
// - pattern is not empty
|
||||||
});
|
const displayPattern = (SdkConfig.get()['latex_maths_delims'] || {})['display_pattern'] ||
|
||||||
|
"(^|[^\\\\])\\\\\\[(?!\\\\\\])(.*?)\\\\\\]";
|
||||||
|
|
||||||
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
// conditions for inline math detection \(...\):
|
||||||
|
// - pattern starts at beginning of line or is not prefixed with backslash
|
||||||
|
// - pattern is not empty
|
||||||
|
const inlinePattern = (SdkConfig.get()['latex_maths_delims'] || {})['inline_pattern'] ||
|
||||||
|
"(^|[^\\\\])\\\\\\((?!\\\\\\))(.*?)\\\\\\)";
|
||||||
|
|
||||||
// conditions for display math detection \[...\]:
|
md = md.replace(RegExp(displayPattern, "gms"), function(m, p1, p2) {
|
||||||
// - pattern starts at beginning of line or is not prefixed with backslash
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
// - pattern is not empty
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
||||||
const displayPattern = (SdkConfig.get()['latex_maths_delims'] || {})['display_pattern'] ||
|
});
|
||||||
"(^|[^\\\\])\\\\\\[(?!\\\\\\])(.*?)\\\\\\]";
|
|
||||||
|
|
||||||
// conditions for inline math detection \(...\):
|
md = md.replace(RegExp(inlinePattern, "gms"), function(m, p1, p2) {
|
||||||
// - pattern starts at beginning of line or is not prefixed with backslash
|
const p2e = AllHtmlEntities.encode(p2);
|
||||||
// - pattern is not empty
|
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
||||||
const inlinePattern = (SdkConfig.get()['latex_maths_delims'] || {})['inline_pattern'] ||
|
});
|
||||||
"(^|[^\\\\])\\\\\\((?!\\\\\\))(.*?)\\\\\\)";
|
|
||||||
|
|
||||||
md = md.replace(RegExp(displayPattern, "gms"), 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(inlinePattern, "gms"), function(m, p1, p2) {
|
|
||||||
const p2e = AllHtmlEntities.encode(p2);
|
|
||||||
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure div tags always start on a new line, otherwise it will confuse
|
// make sure div tags always start on a new line, otherwise it will confuse
|
||||||
// the markdown parser
|
// the markdown parser
|
||||||
|
@ -154,12 +140,6 @@ export function markdownSerializeIfNeeded(md: string, {forceHTML = false} = {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function htmlSerializeIfNeeded(model: EditorModel, {forceHTML = false} = {}) {
|
|
||||||
const md = mdSerialize(model);
|
|
||||||
|
|
||||||
return markdownSerializeIfNeeded(md, {forceHTML: forceHTML});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function textSerialize(model: EditorModel) {
|
export function textSerialize(model: EditorModel) {
|
||||||
return model.parts.reduce((text, part) => {
|
return model.parts.reduce((text, part) => {
|
||||||
switch (part.type) {
|
switch (part.type) {
|
||||||
|
|
|
@ -416,7 +416,6 @@
|
||||||
"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, 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