diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index 6d66238..03f8a0e 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -22,7 +22,11 @@ const createCheckboxOfLine = ( const dateNow = helpers.getDateString(new Date()); const checkboxOfLine = helpers.getCheckboxOfLine(line); - const checkboxCharacters = dateWhenCreated ? `[ ] ${dateNow} ` : '[ ] '; + const hasDate = helpers + .getPlainLineText(line.text) + .match(/^\d{4}-\d{2}-\d{2} /); + const checkboxCharacters = + dateWhenCreated && !hasDate ? `[ ] ${dateNow} ` : '[ ] '; return editor.edit((editBuilder: TextEditorEdit) => { if (!checkboxOfLine) { diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index 154be0b..c6b80a7 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -173,4 +173,36 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should not insert creation date twice if exists', 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 typeOfBulletPoint = getConfig('typeOfBulletPoint'); + const expectedResult = `${typeOfBulletPoint} [ ] 9999-99-99 this is a text`; + + assert.strictEqual(content, expectedResult); + }); });