1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Desktop,Mobile: Fix list renumbering in the Markdown editor resets the first list item number to 1 (#11220)

This commit is contained in:
Henry Heino 2024-10-26 13:03:13 -07:00 committed by GitHub
parent e77fa19fea
commit 59feec1fe2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View File

@ -38,4 +38,30 @@ describe('renumberSelectedLists', () => {
'# End',
].join('\n'));
});
it('should preserve the first list number if not 1', async () => {
const listText = [
'2. This',
'4. is',
'5. a',
'6. test',
].join('\n');
const editor = await createTestEditor(
`${listText}\n\n# End`,
EditorSelection.range(0, listText.length),
['OrderedList', 'ATXHeading1'],
);
editor.dispatch(renumberSelectedLists(editor.state));
expect(editor.state.doc.toString()).toBe([
'2. This',
'3. is',
'4. a',
'5. test',
'',
'# End',
].join('\n'));
});
});

View File

@ -13,6 +13,14 @@ const renumberSelectedLists = (state: EditorState): TransactionSpec => {
// Re-numbers ordered lists and sublists with numbers on each line in [linesToHandle]
const handleLines = (linesToHandle: Line[]) => {
const changes: ChangeSpec[] = [];
if (linesToHandle.length === 0) {
return changes;
}
let firstListNumber = Number(listItemRegex.exec(linesToHandle[0].text)?.[2]);
if (!isFinite(firstListNumber) || firstListNumber < 1) {
firstListNumber = 1;
}
type ListItemRecord = {
nextListNumber: number;
@ -20,7 +28,7 @@ const renumberSelectedLists = (state: EditorState): TransactionSpec => {
};
const listNumberStack: ListItemRecord[] = [];
let currentGroupIndentation = '';
let nextListNumber = 1;
let nextListNumber = firstListNumber;
let prevLineNumber;
for (const line of linesToHandle) {