Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

copy_and_paste: Support inline tags with katex siblings.

apoorvapendsed3b601ef3b70
diff --git a/web/src/copy_and_paste.ts b/web/src/copy_and_paste.ts
index 944cdf68303..af085f30ef4 100644
--- a/web/src/copy_and_paste.ts
+++ b/web/src/copy_and_paste.ts
@@ -13,6 +13,66 @@ import * as stream_data from "./stream_data.ts";
import * as topic_link_util from "./topic_link_util.ts";
import * as util from "./util.ts";
+const all_inline_html_elements = new Set([
+ "a",
+ "abbr",
+ "acronym",
+ "b",
+ "bdi",
+ "bdo",
+ "big",
+ "br",
+ "cite",
+ "code",
+ "data",
+ "del",
+ "dfn",
+ "em",
+ "i",
+ "img",
+ "input",
+ "kbd",
+ "label",
+ "map",
+ "mark",
+ "output",
+ "q",
+ "ruby",
+ "rp",
+ "rt",
+ "s",
+ "samp",
+ "select",
+ "small",
+ "span",
+ "strong",
+ "sub",
+ "sup",
+ "textarea",
+ "time",
+ "tt",
+ "u",
+ "var",
+ "wbr",
+]);
+const markdown_map_for_inline_html_tags = new Map([
+ ["strong", "**"],
+ ["b", "**"],
+ ["em", "*"],
+ ["i", "*"],
+ ["code", "`"],
+ ["s", "~~"],
+ ["del", "~~"],
+ ["br", "\n"],
+ ["sub", "<sub>"],
+ ["sup", "<sup>"],
+ ["u", "<u>"],
+ ["mark", "=="],
+ ["kbd", "`"],
+ ["var", "_"],
+ ["cite", "_"],
+ ["q", "“"],
+]);
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface HTMLElementTagNameMap {
@@ -640,16 +700,21 @@ export function paste_handler_converter(
return (math_block_markdown += "```");
},
});
- const inlineTags = new Set(["em", "strong"]);
function is_inline_tag(node: HTMLElement): boolean {
- return node.nodeType === 1 && inlineTags.has(node.tagName.toLowerCase());
+ return node.nodeType === 1 && all_inline_html_elements.has(node.tagName.toLowerCase());
}
turndownService.addRule("katex-inline-math", {
filter(node) {
- // Allow the intermediate text blocks, so that they don't get appended to the end.
- if (node.classList.contains("zulip-paste-parser-text-node") || is_inline_tag(node)) {
+ // Allow the intermediate text blocks and inline tags with katex siblings, so that they don't get appended to the end.
+ if (
+ node.classList.contains("zulip-paste-parser-text-node") ||
+ (is_inline_tag(node) &&
+ [...node.parentNode!.children].some(
+ (sibling) => sibling !== node && sibling.classList.contains("katex"),
+ ))
+ ) {
return true;
}
if (node.classList.contains("katex") && !node.classList.contains("katex-display")) {
@@ -672,17 +737,10 @@ export function paste_handler_converter(
}
processed_inline_math_block_parents.add(parent);
for (const child of parent.children) {
- let is_strong_ele = false;
- let is_emphasized_ele = false;
- if (child.nodeName.toLocaleLowerCase() === "strong") {
- is_strong_ele = true;
- parsed_inline_expression += "**";
- }
- if (child.nodeName.toLocaleLowerCase() === "em") {
- is_emphasized_ele = true;
- parsed_inline_expression += "*";
- }
- if (child.nodeName === "BR") {
+ const tag = child.nodeName.toLowerCase();
+ const markdown_tag_wrap = markdown_map_for_inline_html_tags.get(tag) ?? "";
+
+ if (tag === "br") {
parsed_inline_expression += "\n";
continue;
}
@@ -699,13 +757,8 @@ export function paste_handler_converter(
continue;
}
// It is a text node that is not between two katex spans
- parsed_inline_expression += child.textContent;
- if (is_strong_ele) {
- parsed_inline_expression += "**";
- }
- if (is_emphasized_ele) {
- parsed_inline_expression += "*";
- }
+ parsed_inline_expression +=
+ markdown_tag_wrap + child.textContent + markdown_tag_wrap;
continue;
}
}
diff --git a/zerver/tests/fixtures/katex_test_cases.json b/zerver/tests/fixtures/katex_test_cases.json
index c6592f3319b..5bc84a149e9 100644
--- a/zerver/tests/fixtures/katex_test_cases.json
+++ b/zerver/tests/fixtures/katex_test_cases.json
@@ -44,6 +44,11 @@
"input": "<div class=\"message_content rendered_markdown\"><p>I <em>think</em> <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mi>e</mi><mrow><mi>i</mi><mi>π</mi></mrow></msup><mo>=</mo><mo>−</mo><mn>1</mn></mrow><annotation encoding=\"application/x-tex\">e^{i\\pi} = -1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8247em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">e</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8247em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mathnormal mtight\" style=\"margin-right:0.03588em;\">iπ</span></span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">−</span><span class=\"mord\">1</span></span></span></span> is the <strong>coolest</strong> thing I've seen since <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><msup><mi>y</mi><mn>2</mn></msup><mo>=</mo><msup><mi>z</mi><mn>2</mn></msup></mrow><annotation encoding=\"application/x-tex\">x^2 + y^2 = z^2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8974em;vertical-align:-0.0833em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:1.0085em;vertical-align:-0.1944em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">y</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8141em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.04398em;\">z</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span></span> blew my mind.</p></div>",
"expected_output":"I *think* $$e^{i\\pi} = -1$$ is the **coolest** thing I've seen since $$x^2 + y^2 = z^2$$ blew my mind."
+ },{
+ "original_markup":"there $$a+b$$ `some code` some text _hello_ `more code` $$y+z=4444$$ ~~don't read this~~ some text `code` $$p+q=44$$ text haha wooo",
+ "expected_output":"there $$a+b$$ `some code` some text _hello_ `more code` $$y+z=4444$$ ~~don't read this~~ some text `code` $$p+q=44$$ text haha wooo",
+ "input":"<div class=\"message_content rendered_markdown\"><p>there <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>a</mi><mo>+</mo><mi>b</mi></mrow><annotation encoding=\"application/x-tex\">a+b</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6667em;vertical-align:-0.0833em;\"></span><span class=\"mord mathnormal\">a</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em;\"></span><span class=\"mord mathnormal\">b</span></span></span></span> <code>some code</code> some text _hello_ <code>more code</code> <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>y</mi><mo>+</mo><mi>z</mi><mo>=</mo><mn>4444</mn></mrow><annotation encoding=\"application/x-tex\">y+z=4444</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7778em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">y</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.4306em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.04398em;\">z</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">4444</span></span></span></span> <del>don't read this</del> some text <code>code</code> <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi><mo>+</mo><mi>q</mi><mo>=</mo><mn>44</mn></mrow><annotation encoding=\"application/x-tex\">p+q=44</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7778em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">q</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">44</span></span></span></span> text haha wooo</p></div>",
+ "description": "Inline expression with inline elements."
}
],
"text_node_to_span_conversion_tests":[