Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make date format configurable #19

Merged
merged 2 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,18 @@
],
"default": "X",
"description": "Checkmark of the checkbox."
},
"markdown-checkbox.dateFormat": {
"type": "string",
"default": "YYYY-MM-DD",
"description": "Format date"
}
}
}
},
"devDependencies": {
"@types/mocha": "^5.2.6",
"@types/moment": "^2.13.0",
"@types/node": "^11.13.7",
"clean-webpack-plugin": "^2.0.1",
"ts-loader": "^5.4.3",
Expand All @@ -161,5 +167,8 @@
"vscode": "^1.1.33",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.1"
},
"dependencies": {
"moment": "^2.24.0"
}
}
9 changes: 7 additions & 2 deletions src/toggleCheckbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { Position, Range, TextEditorEdit } from 'vscode';
import * as helpers from './helpers';
const moment = require('moment');

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

const dateFormat = helpers.getConfig<string>('dateFormat');

if (!lhc.checked && textWithoutCheckbox.length > 0) {
let newText = (strikeThroughWhenChecked ? '~~' : '') + (italicWhenChecked ? '*' : '') + textWithoutCheckbox + (italicWhenChecked ? '*' : '') + (strikeThroughWhenChecked ? '~~' : '');
// add the date string
newText = newText + (dateWhenChecked ? ' [' + helpers.getDateString(new Date()) + ']' : '') + whitespace;
newText = newText + (dateWhenChecked ? ' [' + moment(new Date()).format(dateFormat) + ']' : '') + whitespace;

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

editBuilder.replace(new Range(
new Position(checkboxPosition.line, checkboxPosition.character + 4),
Expand Down