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.

compose_paste: Use `katex-mathml` for inline math filtering.

Apoorva Pendsea33d7bacb94f
diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.ts
index bccb9ed3df6..1afaeb4844b 100644
--- a/web/src/compose_paste.ts
+++ b/web/src/compose_paste.ts
@@ -280,9 +280,11 @@ export function paste_handler_converter(
turndownService.addRule("katex-inline-math", {
filter(node) {
- if (node.classList.contains("katex")) {
- const parent = node.parentElement;
- if (parent?.classList.contains("katex-display")) {
+ if (node.classList.contains("katex-mathml") || node.classList.contains("katex-html")) {
+ // Check if this lies within a `.katex-display` which
+ // is processed as a math block.
+ const grand_parent = node.parentElement?.parentElement;
+ if (grand_parent?.classList.contains("katex-display")) {
// This is already processed by the math block rule.
return false;
}
@@ -292,8 +294,11 @@ export function paste_handler_converter(
},
replacement(content, node: Node) {
assert(node instanceof HTMLElement);
+ if (node.classList.contains("katex-html")) {
+ return "";
+ }
const annotation_element = node.querySelector(
- `.katex-mathml annotation[encoding="application/x-tex"]`,
+ `annotation[encoding="application/x-tex"]`,
);
const katex_source = annotation_element?.textContent?.trim() ?? content;
return `$$${katex_source}$$`;
diff --git a/zerver/tests/fixtures/katex_test_cases.json b/zerver/tests/fixtures/katex_test_cases.json
index 108a9492329..541ee9ddb3a 100644
--- a/zerver/tests/fixtures/katex_test_cases.json
+++ b/zerver/tests/fixtures/katex_test_cases.json
@@ -54,6 +54,9 @@
"description":"Another katex inline expression containing nested tags",
"expected_output":"world ~~it works **out $$a+b=c$$** so `well` if you try *this*** not $$p+q=r$$ that~~ good good",
"original_markup":"world ~~it works **out $$a+b=c$$** so `well` if you try *this*** not $$p+q=r$$ that~~ good good"
+ },{
+ "original_markup":"$$a^{b^{c}}$$",
+ "expected_output":"$$a^{b^{c}}$$"
}
],
"text_node_to_span_conversion_tests":[