diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.ts b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.ts index 037a4906ab0..70a4c9d02f7 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.ts +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.test.ts @@ -2,19 +2,76 @@ import { modifyListLines } from './useCursorUtils'; describe('useCursorUtils', () => { - const listWithDashes = `- item1 -- item2 -- item3`; + const listWithDashes = [ + '- item1', + '- item2', + '- item3', + ]; - const listNoDashes = `item1 -item2 -item3`; + const listWithNoPrefixes = [ + 'item1', + 'item2', + 'item3', + ]; - test('should remove "- " from beggining of each line of input string', () => { - expect(JSON.stringify(modifyListLines(listWithDashes.split('\n'), 0, '- '))).toBe(JSON.stringify(listNoDashes.split('\n'))); + const listWithNumbers = [ + '1. item1', + '2. item2', + '3. item3', + ]; + + const listWithOnes = [ + '1. item1', + '1. item2', + '1. item3', + ]; + + const listWithSomeNumbers = [ + '1. item1', + 'item2', + '2. item3', + ]; + + const numberedListWithEmptyLines = [ + '1. item1', + '2. item2', + '3. ' , + '4. item3', + ]; + + const noPrefixListWithEmptyLines = [ + 'item1', + 'item2', + '' , + 'item3', + ]; + + test('should remove "- " from beginning of each line of input string', () => { + expect(modifyListLines([...listWithDashes], NaN, '- ')).toStrictEqual(listWithNoPrefixes); + }); + + test('should add "- " at the beginning of each line of the input string', () => { + expect(modifyListLines([...listWithNoPrefixes], NaN, '- ')).toStrictEqual(listWithDashes); + }); + + test('should remove "n. " at the beginning of each line of the input string', () => { + expect(modifyListLines([...listWithNumbers], 4, '1. ')).toStrictEqual(listWithNoPrefixes); + }); + + test('should add "n. " at the beginning of each line of the input string', () => { + expect(modifyListLines([...listWithNoPrefixes], 1, '1. ')).toStrictEqual(listWithNumbers); + }); + + test('should remove "1. " at the beginning of each line of the input string', () => { + expect(modifyListLines([...listWithOnes], 2, '1. ')).toStrictEqual(listWithNoPrefixes); + }); + + test('should remove "n. " from each line that has it, and ignore' + + ' lines which do not', () => { + expect(modifyListLines([...listWithSomeNumbers], 2, '2. ')).toStrictEqual(listWithNoPrefixes); }); - test('should add "- " at the beggining of each line of the input string', () => { - expect(JSON.stringify(modifyListLines(listNoDashes.split('\n'), 0, '- '))).toBe(JSON.stringify(listWithDashes.split('\n'))); + test('should add numbers to each line including empty one', () => { + expect(modifyListLines(noPrefixListWithEmptyLines, 1, '1. ')).toStrictEqual(numberedListWithEmptyLines); }); }); diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.ts b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.ts index ad2c2c8a093..f55636325f4 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.ts +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/utils/useCursorUtils.ts @@ -1,20 +1,27 @@ import markdownUtils from '@joplin/lib/markdownUtils'; import Setting from '@joplin/lib/models/Setting'; -export function modifyListLines(lines: string[],num: number,listSymbol: string) { +export function modifyListLines(lines: string[], num: number, listSymbol: string) { + const isNotNumbered = num === 1; for (let j = 0; j < lines.length; j++) { const line = lines[j]; if (!line && j === lines.length - 1) continue; // Only add the list token if it's not already there // if it is, remove it - if (!line.startsWith(listSymbol)) { - if (num) { + if (num) { + const lineNum = markdownUtils.olLineNumber(line); + if (!lineNum && isNotNumbered) { lines[j] = `${num.toString()}. ${line}`; num++; } else { - lines[j] = listSymbol + line; + const listToken = markdownUtils.extractListToken(line); + lines[j] = line.substr(listToken.length, line.length - listToken.length); } } else { - lines[j] = line.substr(listSymbol.length, line.length - listSymbol.length); + if (!line.startsWith(listSymbol)) { + lines[j] = listSymbol + line; + } else { + lines[j] = line.substr(listSymbol.length, line.length - listSymbol.length); + } } } return lines;