Skip to content

Commit

Permalink
Avoid multiply creation dates (PKief#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganskef committed May 12, 2023
1 parent 2092e40 commit c97257e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/createCheckbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/spec/checkbox/createCheckbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('typeOfBulletPoint');
const expectedResult = `${typeOfBulletPoint} [ ] 9999-99-99 this is a text`;

assert.strictEqual(content, expectedResult);
});
});

0 comments on commit c97257e

Please sign in to comment.