Skip to content

Commit

Permalink
feat(toggle_checkbox): transform non-checkbox line into a checkbox (#363
Browse files Browse the repository at this point in the history
)

* feat(toggle_checkbox) transform non-checkbox line into a checkbox

* fix(toggle_checkbox): handle conversion from unordered list to checkbox

---------

Co-authored-by: Pete <[email protected]>
  • Loading branch information
anggerdeni and epwalsh authored Jan 31, 2024
1 parent d9be302 commit 97f0cfe
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,18 @@ end
util.toggle_checkbox = function()
local line_num = unpack(vim.api.nvim_win_get_cursor(0)) -- 1-indexed
local line = vim.api.nvim_get_current_line()
if string.match(line, "^%s*- %[ %].*") then

local checkbox_pattern = "^%s*- %[.*"

if not string.match(line, checkbox_pattern) then
local unordered_list_pattern = "^([ ]*)[-*+] ([^%[])"

if string.match(line, unordered_list_pattern) then
line = string.gsub(line, unordered_list_pattern, "%1- [ ] %2")
else
line = string.gsub(line, "^([%s]*)", "%1- [ ] ")
end
elseif string.match(line, "^%s*- %[ %].*") then
line = util.string_replace(line, "- [ ]", "- [x]", 1)
else
for check_char in iter { "x", "~", ">", "-" } do
Expand Down

0 comments on commit 97f0cfe

Please sign in to comment.