2019-05-22 16:16:32 +02:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-04-15 00:49:08 +01:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 16:16:32 +02:00
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-05-22 16:16:32 +02:00
|
|
|
*/
|
|
|
|
|
|
2022-12-02 11:10:54 +00:00
|
|
|
import { encode } from "html-entities";
|
2022-04-19 15:53:59 +02:00
|
|
|
import escapeHtml from "escape-html";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2019-05-21 17:59:54 +02:00
|
|
|
import Markdown from "../Markdown";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { makeGenericPermalink } from "../utils/permalinks/Permalinks";
|
2020-04-15 00:49:08 +01:00
|
|
|
import EditorModel from "./model";
|
2020-10-10 16:32:49 +01:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2020-09-20 15:07:12 +01:00
|
|
|
import SdkConfig from "../SdkConfig";
|
2021-07-12 13:26:34 +01:00
|
|
|
import { Type } from "./parts";
|
2019-05-21 17:59:54 +02:00
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function mdSerialize(model: EditorModel): string {
|
2019-05-14 10:37:16 +01:00
|
|
|
return model.parts.reduce((html, part) => {
|
|
|
|
|
switch (part.type) {
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Newline:
|
2019-05-21 17:59:54 +02:00
|
|
|
return html + "\n";
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Plain:
|
2022-01-24 07:53:05 -05:00
|
|
|
case Type.Emoji:
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Command:
|
|
|
|
|
case Type.PillCandidate:
|
|
|
|
|
case Type.AtRoomPill:
|
2019-05-14 10:37:16 +01:00
|
|
|
return html + part.text;
|
2023-07-06 00:00:27 +02:00
|
|
|
case Type.RoomPill: {
|
2024-08-01 13:17:44 +02:00
|
|
|
const url = makeGenericPermalink(part.resourceId, true);
|
2023-07-06 00:00:27 +02:00
|
|
|
// Escape square brackets and backslashes
|
2021-03-11 08:29:03 +01:00
|
|
|
// Here we use the resourceId for compatibility with non-rich text clients
|
|
|
|
|
// See https://github.com/vector-im/element-web/issues/16660
|
2023-07-06 00:00:27 +02:00
|
|
|
const title = part.resourceId.replace(/[[\\\]]/g, (c) => "\\" + c);
|
|
|
|
|
return html + `[${title}](${url})`;
|
|
|
|
|
}
|
|
|
|
|
case Type.UserPill: {
|
2024-08-01 13:17:44 +02:00
|
|
|
const url = makeGenericPermalink(part.resourceId, true);
|
2023-07-06 00:00:27 +02:00
|
|
|
// Escape square brackets and backslashes; convert newlines to HTML
|
|
|
|
|
const title = part.text.replace(/[[\\\]]/g, (c) => "\\" + c).replace(/\n/g, "<br>");
|
|
|
|
|
return html + `[${title}](${url})`;
|
|
|
|
|
}
|
2019-05-14 10:37:16 +01:00
|
|
|
}
|
|
|
|
|
}, "");
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 15:53:59 +02:00
|
|
|
interface ISerializeOpts {
|
|
|
|
|
forceHTML?: boolean;
|
|
|
|
|
useMarkdown?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function htmlSerializeIfNeeded(
|
|
|
|
|
model: EditorModel,
|
|
|
|
|
{ forceHTML = false, useMarkdown = true }: ISerializeOpts = {},
|
2023-02-16 17:21:44 +00:00
|
|
|
): string | undefined {
|
2022-04-19 15:53:59 +02:00
|
|
|
if (!useMarkdown) {
|
|
|
|
|
return escapeHtml(textSerialize(model)).replace(/\n/g, "<br/>");
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 22:20:32 +02:00
|
|
|
const md = mdSerialize(model);
|
|
|
|
|
return htmlSerializeFromMdIfNeeded(md, { forceHTML });
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
export function htmlSerializeFromMdIfNeeded(md: string, { forceHTML = false } = {}): string | undefined {
|
2020-12-20 23:14:56 +01:00
|
|
|
// copy of raw input to remove unwanted math later
|
|
|
|
|
const orig = md;
|
2020-09-20 12:59:22 +01:00
|
|
|
|
2020-10-10 16:32:49 +01:00
|
|
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
2023-02-13 11:39:16 +00:00
|
|
|
const patternNames = ["tex", "latex"] as const;
|
|
|
|
|
const patternTypes = ["display", "inline"] as const;
|
2021-04-01 12:09:51 +02:00
|
|
|
const patternDefaults = {
|
|
|
|
|
tex: {
|
|
|
|
|
// 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)\\$";
|
2020-09-20 12:59:22 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
// conditions for display math detection $$...$$:
|
2021-04-28 19:39:38 +02:00
|
|
|
// - pattern starts and ends on a new line
|
2021-04-01 12:09:51 +02:00
|
|
|
// - left delimiter ($$) is not escaped by backslash
|
2021-04-28 19:39:38 +02:00
|
|
|
display: "(^)\\$\\$(([^$]|\\\\\\$)+?)\\$\\$$",
|
2020-12-20 23:14:56 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
// 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
|
|
|
|
|
inline: "(^|\\s|[.,!?:;])(?!\\\\)\\$(?!\\s)(([^$\\n]|\\\\\\$)*([^\\\\\\s\\$]|\\\\\\$)(?:\\\\\\$)?)\\$",
|
|
|
|
|
},
|
|
|
|
|
latex: {
|
|
|
|
|
// detect math with latex delimiters, inline: \(...\), display \[...\]
|
2021-01-29 00:11:06 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
// conditions for display math detection \[...\]:
|
2021-04-28 19:39:38 +02:00
|
|
|
// - pattern starts and ends on a new line
|
2021-04-01 12:09:51 +02:00
|
|
|
// - pattern is not empty
|
2021-04-28 19:39:38 +02:00
|
|
|
display: "(^)\\\\\\[(?!\\\\\\])(.*?)\\\\\\]$",
|
2021-01-29 00:11:06 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
// conditions for inline math detection \(...\):
|
|
|
|
|
// - pattern starts at beginning of line or is not prefixed with backslash
|
|
|
|
|
// - pattern is not empty
|
|
|
|
|
inline: "(^|[^\\\\])\\\\\\((?!\\\\\\))(.*?)\\\\\\)",
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-01-29 00:11:06 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
patternNames.forEach(function (patternName) {
|
|
|
|
|
patternTypes.forEach(function (patternType) {
|
|
|
|
|
// get the regex replace pattern from config or use the default
|
2022-03-18 10:12:36 -06:00
|
|
|
const pattern =
|
2023-02-13 11:39:16 +00:00
|
|
|
SdkConfig.get("latex_maths_delims")?.[patternType]?.["pattern"]?.[patternName] ||
|
2021-04-01 12:09:51 +02:00
|
|
|
patternDefaults[patternName][patternType];
|
2021-01-29 00:11:06 +01:00
|
|
|
|
2021-04-01 12:09:51 +02:00
|
|
|
md = md.replace(RegExp(pattern, "gms"), function (m, p1, p2) {
|
2022-12-02 11:10:54 +00:00
|
|
|
const p2e = encode(p2);
|
2021-04-01 12:09:51 +02:00
|
|
|
switch (patternType) {
|
|
|
|
|
case "display":
|
|
|
|
|
return `${p1}<div data-mx-maths="${p2e}">\n\n</div>\n\n`;
|
|
|
|
|
case "inline":
|
|
|
|
|
return `${p1}<span data-mx-maths="${p2e}"></span>`;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-01-29 13:05:49 +01:00
|
|
|
});
|
2020-10-25 18:32:24 +00:00
|
|
|
|
2023-05-23 14:31:05 +01:00
|
|
|
// make sure div tags always start on a new line, otherwise it will confuse the markdown parser
|
2020-10-25 18:32:24 +00:00
|
|
|
md = md.replace(/(.)<div/g, function (m, p1) {
|
|
|
|
|
return `${p1}\n<div`;
|
|
|
|
|
});
|
2020-09-20 12:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 17:59:54 +02:00
|
|
|
const parser = new Markdown(md);
|
2019-07-08 16:55:56 +02:00
|
|
|
if (!parser.isPlainText() || forceHTML) {
|
2020-10-14 09:35:57 +01:00
|
|
|
// feed Markdown output to HTML parser
|
2023-05-23 14:31:05 +01:00
|
|
|
const phtml = new DOMParser().parseFromString(parser.toHTML(), "text/html");
|
2020-10-14 09:35:57 +01:00
|
|
|
|
2020-12-20 23:14:56 +01:00
|
|
|
if (SettingsStore.getValue("feature_latex_maths")) {
|
|
|
|
|
// original Markdown without LaTeX replacements
|
|
|
|
|
const parserOrig = new Markdown(orig);
|
2023-05-23 14:31:05 +01:00
|
|
|
const phtmlOrig = new DOMParser().parseFromString(parserOrig.toHTML(), "text/html");
|
2020-12-20 23:14:56 +01:00
|
|
|
|
|
|
|
|
// since maths delimiters are handled before Markdown,
|
|
|
|
|
// code blocks could contain mangled content.
|
|
|
|
|
// replace code blocks with original content
|
2023-05-23 14:31:05 +01:00
|
|
|
[...phtmlOrig.getElementsByTagName("code")].forEach((e, i) => {
|
|
|
|
|
phtml.getElementsByTagName("code").item(i)!.textContent = e.textContent;
|
2020-12-20 23:14:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// add fallback output for latex math, which should not be interpreted as markdown
|
2023-05-23 14:31:05 +01:00
|
|
|
[...phtml.querySelectorAll("div, span")].forEach((e, i) => {
|
|
|
|
|
const tex = e.getAttribute("data-mx-maths");
|
2020-12-20 23:14:56 +01:00
|
|
|
if (tex) {
|
2023-05-23 14:31:05 +01:00
|
|
|
e.innerHTML = `<code>${tex}</code>`;
|
2020-12-20 23:14:56 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-23 14:31:05 +01:00
|
|
|
return phtml.body.innerHTML;
|
2019-05-21 17:59:54 +02:00
|
|
|
}
|
2020-06-03 16:36:48 -05:00
|
|
|
// ensure removal of escape backslashes in non-Markdown messages
|
|
|
|
|
if (md.indexOf("\\") > -1) {
|
|
|
|
|
return parser.toPlaintext();
|
|
|
|
|
}
|
2019-05-21 17:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function textSerialize(model: EditorModel): string {
|
2019-05-14 10:37:16 +01:00
|
|
|
return model.parts.reduce((text, part) => {
|
|
|
|
|
switch (part.type) {
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Newline:
|
2019-05-14 10:37:16 +01:00
|
|
|
return text + "\n";
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Plain:
|
2022-01-24 07:53:05 -05:00
|
|
|
case Type.Emoji:
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.Command:
|
|
|
|
|
case Type.PillCandidate:
|
|
|
|
|
case Type.AtRoomPill:
|
2019-05-14 10:37:16 +01:00
|
|
|
return text + part.text;
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.RoomPill:
|
2021-03-11 18:50:35 +01:00
|
|
|
// Here we use the resourceId for compatibility with non-rich text clients
|
|
|
|
|
// See https://github.com/vector-im/element-web/issues/16660
|
|
|
|
|
return text + `${part.resourceId}`;
|
2021-07-12 13:26:34 +01:00
|
|
|
case Type.UserPill:
|
2019-08-30 11:51:29 +02:00
|
|
|
return text + `${part.text}`;
|
2019-05-14 10:37:16 +01:00
|
|
|
}
|
|
|
|
|
}, "");
|
|
|
|
|
}
|
2019-08-21 11:26:21 +02:00
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function containsEmote(model: EditorModel): boolean {
|
2022-03-21 15:09:43 -04:00
|
|
|
const hasCommand = startsWith(model, "/me ", false);
|
|
|
|
|
const hasArgument = model.parts[0]?.text?.length > 4 || model.parts.length > 1;
|
|
|
|
|
return hasCommand && hasArgument;
|
2020-01-21 15:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function startsWith(model: EditorModel, prefix: string, caseSensitive = true): boolean {
|
2019-08-21 11:26:21 +02:00
|
|
|
const firstPart = model.parts[0];
|
2019-08-21 15:27:50 +02:00
|
|
|
// part type will be "plain" while editing,
|
|
|
|
|
// and "command" while composing a message.
|
2021-09-28 16:04:25 +02:00
|
|
|
let text = firstPart?.text || "";
|
2020-06-16 14:06:42 +01:00
|
|
|
if (!caseSensitive) {
|
2020-06-16 00:41:21 +01:00
|
|
|
prefix = prefix.toLowerCase();
|
|
|
|
|
text = text.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
return firstPart && (firstPart.type === Type.Plain || firstPart.type === Type.Command) && text.startsWith(prefix);
|
2019-08-21 11:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function stripEmoteCommand(model: EditorModel): EditorModel {
|
2019-08-21 11:26:21 +02:00
|
|
|
// trim "/me "
|
2020-01-21 15:55:21 +00:00
|
|
|
return stripPrefix(model, "/me ");
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function stripPrefix(model: EditorModel, prefix: string): EditorModel {
|
2019-08-21 11:26:21 +02:00
|
|
|
model = model.clone();
|
2021-06-29 13:11:58 +01:00
|
|
|
model.removeText({ index: 0, offset: 0 }, prefix.length);
|
2019-08-21 11:26:21 +02:00
|
|
|
return model;
|
|
|
|
|
}
|
2019-09-02 17:53:14 +02:00
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
export function unescapeMessage(model: EditorModel): EditorModel {
|
2021-06-29 13:11:58 +01:00
|
|
|
const { parts } = model;
|
2019-09-02 17:53:14 +02:00
|
|
|
if (parts.length) {
|
|
|
|
|
const firstPart = parts[0];
|
|
|
|
|
// only unescape \/ to / at start of editor
|
2021-07-12 13:26:34 +01:00
|
|
|
if (firstPart.type === Type.Plain && firstPart.text.startsWith("\\/")) {
|
2019-09-02 17:53:14 +02:00
|
|
|
model = model.clone();
|
2021-06-29 13:11:58 +01:00
|
|
|
model.removeText({ index: 0, offset: 0 }, 1);
|
2019-09-02 17:53:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return model;
|
|
|
|
|
}
|