Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desktop: Fixes #4877: Incorrect list renumbering #4914

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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;
Expand Down