Skip to content

Commit d147c77

Browse files
authored
Merge pull request #19 from PKief/feature/dateformat
Make date format configurable
2 parents b0936b7 + 333d0c7 commit d147c77

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

package-lock.json

Lines changed: 45 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,18 @@
147147
],
148148
"default": "X",
149149
"description": "Checkmark of the checkbox."
150+
},
151+
"markdown-checkbox.dateFormat": {
152+
"type": "string",
153+
"default": "YYYY-MM-DD",
154+
"description": "Format date"
150155
}
151156
}
152157
}
153158
},
154159
"devDependencies": {
155160
"@types/mocha": "^5.2.6",
161+
"@types/moment": "^2.13.0",
156162
"@types/node": "^11.13.7",
157163
"clean-webpack-plugin": "^2.0.1",
158164
"ts-loader": "^5.4.3",
@@ -161,5 +167,8 @@
161167
"vscode": "^1.1.33",
162168
"webpack": "^4.30.0",
163169
"webpack-cli": "^3.3.1"
170+
},
171+
"dependencies": {
172+
"moment": "^2.24.0"
164173
}
165174
}

src/toggleCheckbox.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import { Position, Range, TextEditorEdit } from 'vscode';
33
import * as helpers from './helpers';
4+
const moment = require('moment');
45

56
/** Mark a checkbox as checked or unchecked */
67
export const toggleCheckbox = async () => {
@@ -69,10 +70,12 @@ const markField = (checkboxPosition: Position, replacement: string): Thenable<bo
6970
const foundTrailingWhitespace = lineText.substr(checkboxPosition.character + 4, lineText.length).match(/[\s\n\r]*$/);
7071
const whitespace = foundTrailingWhitespace ? foundTrailingWhitespace.join('') : '';
7172

73+
const dateFormat = helpers.getConfig<string>('dateFormat');
74+
7275
if (!lhc.checked && textWithoutCheckbox.length > 0) {
7376
let newText = (strikeThroughWhenChecked ? '~~' : '') + (italicWhenChecked ? '*' : '') + textWithoutCheckbox + (italicWhenChecked ? '*' : '') + (strikeThroughWhenChecked ? '~~' : '');
7477
// add the date string
75-
newText = newText + (dateWhenChecked ? ' [' + helpers.getDateString(new Date()) + ']' : '') + whitespace;
78+
newText = newText + (dateWhenChecked ? ' [' + moment(new Date()).format(dateFormat) + ']' : '') + whitespace;
7679

7780
editBuilder.replace(new Range(
7881
new Position(checkboxPosition.line, checkboxPosition.character + 4),
@@ -82,7 +85,9 @@ const markField = (checkboxPosition: Position, replacement: string): Thenable<bo
8285
else if (lhc.checked) {
8386
let newText = textWithoutCheckbox.replace(/~~/g, '').replace(/\*/g, '');
8487
// remove the date string
85-
newText = newText.replace(/\s+\[\d{4}[\-]\d{2}[\-]\d{2}\]\s*/, '') + whitespace;
88+
if (dateWhenChecked) {
89+
newText = newText.replace(/\s+\[[\s\S]+\]$/, '') + whitespace;
90+
}
8691

8792
editBuilder.replace(new Range(
8893
new Position(checkboxPosition.line, checkboxPosition.character + 4),

0 commit comments

Comments
 (0)