Skip to content

Commit 68fa996

Browse files
committed
fix: editor markdown not incrementing in a numbered list
1 parent 5b31077 commit 68fa996

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

web_src/js/features/comp/EditorMarkdown.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ test('EditorMarkdown', () => {
3232
testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});
3333

3434
testInput('- x', '- x\n- ');
35-
testInput('1. foo', '1. foo\n1. ');
36-
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n1. \n2. b\n3. c', pos: 8});
35+
testInput('1. foo', '1. foo\n2. ');
36+
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n2. \n2. b\n3. c', pos: 8});
3737
testInput('- [ ]', '- [ ]\n- ');
3838
testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
3939
testInput('* [x] foo', '* [x] foo\n* [ ] ');
40-
testInput('1. [x] foo', '1. [x] foo\n1. [ ] ');
40+
testInput('1. [x] foo', '1. [x] foo\n2. [ ] ');
4141
});

web_src/js/features/comp/EditorMarkdown.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ function handleNewline(textarea: HTMLTextAreaElement, e: Event) {
9696
} else {
9797
// start a new line with the same indention and prefix
9898
let newPrefix = prefix;
99-
// a simple approach, otherwise it needs to parse the lines after the current line
100-
if (/^\d+\./.test(prefix)) newPrefix = `1. ${newPrefix.slice(newPrefix.indexOf('.') + 2)}`;
99+
const digitMatch = /^\d+/.exec(prefix);
100+
if (digitMatch) {
101+
const number = parseInt(digitMatch[0]);
102+
const incremented = number + 1;
103+
newPrefix = `${incremented}. ${newPrefix.slice(newPrefix.indexOf('.') + 2)}`;
104+
}
101105
newPrefix = newPrefix.replace('[x]', '[ ]');
102106
const newLine = `\n${indention}${newPrefix}`;
103107
textarea.value = value.slice(0, selStart) + newLine + value.slice(selEnd);

0 commit comments

Comments
 (0)