1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixes #4877: Incorrect list renumbering (#4914)

This commit is contained in:
Austin Doupnik 2021-06-07 02:17:46 -07:00 committed by GitHub
parent 7921e70c4f
commit 1b7d40387d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 15 deletions

View File

@ -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 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 "- " 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 numbers to each line including empty one', () => {
expect(modifyListLines(noPrefixListWithEmptyLines, 1, '1. ')).toStrictEqual(numberedListWithEmptyLines);
});
});

View File

@ -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;