Skip to content

Commit

Permalink
fix: editor markdown not incrementing in a numbered list
Browse files Browse the repository at this point in the history
  • Loading branch information
harryvince committed Jan 10, 2025
1 parent 39d51e7 commit 27c910d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions web_src/js/features/comp/EditorMarkdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ test('EditorMarkdown', () => {
testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});

testInput('- x', '- x\n- ');
testInput('1. foo', '1. foo\n1. ');
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n1. \n2. b\n3. c', pos: 8});
testInput('1. foo', '1. foo\n2. ');
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n2. \n2. b\n3. c', pos: 8});
testInput('- [ ]', '- [ ]\n- ');
testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
testInput('* [x] foo', '* [x] foo\n* [ ] ');
testInput('1. [x] foo', '1. [x] foo\n1. [ ] ');
testInput('1. [x] foo', '1. [x] foo\n2. [ ] ');
});
7 changes: 5 additions & 2 deletions web_src/js/features/comp/EditorMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ function handleNewline(textarea: HTMLTextAreaElement, e: Event) {
} else {
// start a new line with the same indention and prefix
let newPrefix = prefix;
// a simple approach, otherwise it needs to parse the lines after the current line
if (/^\d+\./.test(prefix)) newPrefix = `1. ${newPrefix.slice(newPrefix.indexOf('.') + 2)}`;
if (/^\d+\./.test(prefix)) {
const number = parseInt(prefix.match(/^\d+/)[0]); // Extract the number
const incremented = number + 1;
newPrefix = `${incremented}. ${newPrefix.slice(newPrefix.indexOf('.') + 2)}`; // Replace with incremented number
}
newPrefix = newPrefix.replace('[x]', '[ ]');
const newLine = `\n${indention}${newPrefix}`;
textarea.value = value.slice(0, selStart) + newLine + value.slice(selEnd);
Expand Down

0 comments on commit 27c910d

Please sign in to comment.