Created
June 3, 2020 21:39
-
-
Save christiangenco/7f2669a52e5a51590379b0639eca7779 to your computer and use it in GitHub Desktop.
Sublime Text 3 plugin to toggle and timestamp markdown todo checkboxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/sublime-text-3/Packages/User/MarkdownCheckboxes.py | |
import sublime, sublime_plugin | |
import re | |
from time import strftime | |
def now(): | |
return strftime("%Y-%m-%dT%H:%M:%S") | |
timestampRegex = r"\s*\(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\)" | |
def toggleCheckbox(line): | |
if "[ ]" in line: | |
timestamp = " (" + now() + ")" | |
return line.replace("[ ]", "[x]") + timestamp | |
elif "[x]" in line: | |
unchecked = line.replace("[x]", "[ ]") | |
undated = re.sub(timestampRegex, "", unchecked) | |
return undated | |
else: | |
return line | |
class ToggleMarkdownCheckboxCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
# self.view.insert(edit, 0, "hello world") | |
for region in self.view.sel(): | |
# just look at lines with a cusor, not a selection | |
if region.empty(): | |
line = self.view.line(region) | |
lineContents = self.view.substr(line) | |
newLineContents = toggleCheckbox(lineContents) | |
self.view.replace(edit, line, newLineContents) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"keys": ["shift+enter"], "command": "toggle_markdown_checkbox"} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On a user-defined shortkey (I like
shift+enter
), toggle and timestamp a markdown checkbox between:and: