Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. [ ] ');
});
8 changes: 6 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,12 @@ 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)}`;
const digitMatch = /^\d+/.exec(prefix);
if (digitMatch) {
const number = parseInt(digitMatch[0]);
const incremented = number + 1;
newPrefix = `${incremented}. ${newPrefix.slice(newPrefix.indexOf('.') + 2)}`;
}
newPrefix = newPrefix.replace('[x]', '[ ]');
const newLine = `\n${indention}${newPrefix}`;
textarea.value = value.slice(0, selStart) + newLine + value.slice(selEnd);
Expand Down
Loading