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 blank lines when applying list formatting.

sandeep94a26c0e0763
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts
index 87b877ca33d..aa6a17e7b9c 100644
--- a/web/src/compose_ui.ts
+++ b/web/src/compose_ui.ts
@@ -887,15 +887,12 @@ export let format_text = (
const format_list = (type: string): void => {
let is_marked: (line: string) => boolean;
- let mark: (line: string, i: number) => string;
let strip_marking: (line: string) => string;
if (type === "bulleted") {
is_marked = bulleted_numbered_list_util.is_bulleted;
- mark = (line: string) => "- " + line;
strip_marking = bulleted_numbered_list_util.strip_bullet;
} else {
is_marked = bulleted_numbered_list_util.is_numbered;
- mark = (line, i) => i + 1 + ". " + line;
strip_marking = bulleted_numbered_list_util.strip_numbering;
}
// We toggle complete lines even when they are partially selected (and just selecting the
@@ -906,10 +903,23 @@ export let format_text = (
// If there is even a single unmarked line selected, we mark all.
const should_mark = selected_lines.split("\n").some((line) => !is_marked(line));
if (should_mark) {
- selected_lines = selected_lines
- .split("\n")
- .map((line, i) => mark(line, i))
- .join("\n");
+ const lines = selected_lines.split("\n");
+ const processed_lines = [];
+ let counter = 1;
+ for (const line of lines) {
+ if (line.trim() === "") {
+ processed_lines.push(line);
+ } else {
+ if (type === "bulleted") {
+ processed_lines.push("- " + line);
+ } else {
+ processed_lines.push(counter + ". " + line);
+ counter += 1;
+ }
+ }
+ }
+ selected_lines = processed_lines.join("\n");
+
// We always ensure a blank line after the list, as we want
// a clean separation between the list and the rest of the text, especially
// when the markdown is rendered.
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index 9b5ccb10a02..af64ead6532 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -818,7 +818,12 @@ run_test("format_text - bulleted and numbered lists", ({override_rewire}) => {
init_textarea_state("<\nfirst_item\nsecond_item>");
compose_ui.format_text($textarea, "bulleted");
- assert.equal(get_textarea_state(), "<- \n- first_item\n- second_item>");
+ assert.equal(get_textarea_state(), "<\n- first_item\n- second_item>");
+
+ // Blank lines between items should be skipped during bulleted list formatting
+ init_textarea_state("<\nfirst_item\n\nsecond_item\n\nthird_item>");
+ compose_ui.format_text($textarea, "bulleted");
+ assert.equal(get_textarea_state(), "<\n- first_item\n\n- second_item\n\n- third_item>");
// Toggling off bulleted list
init_textarea_state("<- first_item\n- second_item>");
@@ -852,6 +857,11 @@ run_test("format_text - bulleted and numbered lists", ({override_rewire}) => {
"before_first\n<1. first_item\n2. second_item>\n\nafter_last",
);
+ // Blank lines between items should be skipped, counter increments only on non-blank lines
+ init_textarea_state("<\nfirst_item\n\nsecond_item\n\nthird_item>");
+ compose_ui.format_text($textarea, "numbered");
+ assert.equal(get_textarea_state(), "<\n1. first_item\n\n2. second_item\n\n3. third_item>");
+
// Toggling off numbered list
init_textarea_state("<1. first_item\n2. second_item>");
compose_ui.format_text($textarea, "numbered");