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: Skip adding backticks when pasting in inline code span.

apoorvapendse00ad005ca02d
diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.ts
index a3a9939cccf..7ca7c87e970 100644
--- a/web/src/compose_paste.ts
+++ b/web/src/compose_paste.ts
@@ -535,13 +535,9 @@ export function paste_handler_converter(
const language = get_code_block_language(node, className);
// We convert single line code inside a code block which does not have language metadata
// to inline markdown code, and the code for this is taken from upstream's `code` rule.
- if (!code.includes("\n") && language === "") {
- // If the cursor is just after a backtick, then we don't add extra backticks.
- if (
- $textarea &&
- $textarea.caret() !== 0 &&
- $textarea.val()?.at($textarea.caret() - 1) === "`"
- ) {
+ if (!code.includes("\n")) {
+ // If the cursor is inside an inline code span, then we don't add extra backticks.
+ if ($textarea && compose_ui.cursor_inside_inline_code_span($textarea)) {
return content;
}
if (!code) {
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts
index bbb0a583831..1fdacbff89c 100644
--- a/web/src/compose_ui.ts
+++ b/web/src/compose_ui.ts
@@ -621,6 +621,46 @@ export function handle_keyup(
rtl.set_rtl_class_for_textarea($textarea);
}
+/**
+ * True if the cursor in `$textarea` for the current line sits between an opening run
+ * of backticks (`, ```, ...) and its still‑missing matching closer
+ * where the cursor is placed.
+ */
+export function cursor_inside_inline_code_span($textarea: JQuery<HTMLTextAreaElement>): boolean {
+ const text_area_element = $textarea[0];
+ if (!text_area_element) {
+ return false;
+ }
+ // jQuery.val() can be string | number | string[] | undefined.
+ const val = $textarea.val();
+ assert(typeof val === "string");
+ const caret = text_area_element.selectionStart;
+
+ const last_newline = val.lastIndexOf("\n", caret - 1);
+ const line_start = last_newline === -1 ? 0 : last_newline + 1;
+ const current_line_prefix = val.slice(line_start, caret);
+
+ let open_backtick_count = 0;
+ for (let i = 0; i < current_line_prefix.length; i += 1) {
+ if (current_line_prefix[i] === "`") {
+ let consecutive_count = 1;
+ while (i + 1 < current_line_prefix.length && current_line_prefix[i + 1] === "`") {
+ consecutive_count += 1;
+ i += 1;
+ }
+
+ // A code span can be opened with any number of consecutive backticks,
+ // and can only be closed with the same number of consecutive backticks.
+ if (open_backtick_count === 0) {
+ open_backtick_count = consecutive_count;
+ } else if (consecutive_count === open_backtick_count) {
+ open_backtick_count = 0;
+ }
+ }
+ }
+ return open_backtick_count > 0;
+}
+
export function cursor_inside_code_block($textarea: JQuery<HTMLTextAreaElement>): boolean {
// Returns whether the cursor is at a point that would be inside
// a code block on rendering the textarea content as markdown.
diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjs
index b5cd1453a92..2124a8a03fe 100644
--- a/web/tests/compose_paste.test.cjs
+++ b/web/tests/compose_paste.test.cjs
@@ -149,18 +149,99 @@ run_test("paste_handler_converter", () => {
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
assert.equal(
compose_paste.paste_handler_converter(input, {
- caret: () => 6,
- val: () => "e.g. `",
+ val() {
+ return "e.g. `";
+ },
+ 0: {
+ selectionStart: 6,
+ value: "e.g. `",
+ },
+ length: 1,
}),
"single line",
);
- // Yes code formatting if the given text area has a backtick but not at the cursor position
+ // No code formatting if the given text area has a backtick at the cursor position
+ input =
+ '<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
+ assert.equal(
+ compose_paste.paste_handler_converter(input, {
+ val() {
+ return "`e.g. ` ```hi`` ``";
+ },
+ 0: {
+ selectionStart: 19,
+ value: "`e.g. ` ```hi`` ``",
+ },
+ length: 1,
+ }),
+ "single line",
+ );
+
+ // No code formatting if the given text area has a opening backtick before the cursor position
+ input =
+ '<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
+ assert.equal(
+ compose_paste.paste_handler_converter(input, {
+ val() {
+ return "e.g. ` ";
+ },
+ 0: {
+ selectionStart: 7,
+ value: "e.g. ` ",
+ },
+ length: 1,
+ }),
+ "single line",
+ );
+
+ // Yes code formatting if the given text area does not have a backtick at the cursor position.
+ input =
+ '<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
+ assert.equal(
+ compose_paste.paste_handler_converter(input, {
+ val() {
+ return "";
+ },
+ 0: {
+ selectionStart: 0,
+ value: "",
+ },
+ length: 1,
+ }),
+ "`single line`",
+ );
+
+ // Yes code formatting if the given text area closes the code block before the cursor position
+ input =
+ '<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
+ assert.equal(
+ compose_paste.paste_handler_converter(input, {
+ val() {
+ return "` e.g. ` ";
+ },
+ 0: {
+ selectionStart: 9,
+ value: "` e.g. ` ",
+ },
+ length: 1,
+ }),
+ "`single line`",
+ );
+
+ // Yes code formatting if the given text area closes the code block before the cursor position
input =
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><pre><code>single line</code></pre>';
assert.equal(
compose_paste.paste_handler_converter(input, {
- caret: () => 0,
+ val() {
+ return "``` e.g. ``` ``hi`` ";
+ },
+ 0: {
+ selectionStart: 20,
+ value: "``` e.g. ``` ``hi`` ",
+ },
+ length: 1,
}),
"`single line`",
);