From 16990074c0faa9e0f37d479c9c7059298a0556a8 Mon Sep 17 00:00:00 2001 From: Frank Ganske Date: Fri, 12 May 2023 18:50:43 +0200 Subject: [PATCH] Fix: existing bullet point causes duplicates (#17) --- src/createCheckbox.ts | 2 +- src/test/spec/checkbox/createCheckbox.spec.ts | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index 03f8a0e..1310761 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -24,7 +24,7 @@ const createCheckboxOfLine = ( const checkboxOfLine = helpers.getCheckboxOfLine(line); const hasDate = helpers .getPlainLineText(line.text) - .match(/^\d{4}-\d{2}-\d{2} /); + .match(/^(?:[+*-]\s)?\d{4}-\d{2}-\d{2} /); const checkboxCharacters = dateWhenCreated && !hasDate ? `[ ] ${dateNow} ` : '[ ] '; diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index c6b80a7..ea49ada 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -174,7 +174,7 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); - it('should not insert creation date twice if exists', async () => { + it('should not insert another creation date', async () => { // create new document const newDocument = await vscode.workspace.openTextDocument({ content: '9999-99-99 this is a text', @@ -205,4 +205,35 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should not insert another creation date with existing bullet', async () => { + // create new document + const newDocument = await vscode.workspace.openTextDocument({ + content: '- 9999-99-99 this is a text', + language: 'markdown', + }); + await vscode.window.showTextDocument(newDocument); + + // update config to insert creation date if configured + await vscode.workspace + .getConfiguration('markdown-checkbox') + .update('dateWhenCreated', true); + + // set the cursor to the current line + const editor = getEditor(); + const position = editor.selection.active; + const newCursorPosition = position.with(0, 0); + const newSelection = new vscode.Selection( + newCursorPosition, + newCursorPosition + ); + editor.selection = newSelection; + + await createCheckbox(editor); + + const content = editor.document.getText(); + const expectedResult = `- [ ] 9999-99-99 this is a text`; + + assert.strictEqual(content, expectedResult); + }); });