Skip to content
Open
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
2 changes: 1 addition & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"diagnostics.libraryFiles": "Disable",
"workspace.checkThirdParty": "Disable",
"workspace.ignoreDir": ["completion/", "compat.lua"],
"workspace.library": ["$VIMRUNTIME/lua/", "./lua/"],
"workspace.library": ["$VIMRUNTIME/lua/", "./lua/", "${3rd}/luv/library"],
"diagnostics.globals": ["vim", "MiniTest"]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added an option to have the 'ObsidianToggleCheckbox' command to work on blank lines

### Fixed

- Fixed incorrect call signature for `options.callbacks.post_set_workspace`
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,21 @@ require("obsidian").setup {
},
---@class obsidian.config.CheckboxOpts
---
---@field enabled? boolean
---
---Order of checkbox state chars, e.g. { " ", "x" }
---@field order? string[]
---
---Whether to create new checkbox on paragraphs
---@field create_new? boolean
---
---Whether to create new checkbox on empty lines
---@field create_on_empty? boolean
checkbox = {
enabled = true,
order = { " ", "~", "!", ">", "x" },
create_new = true,
create_on_empty = false,
},
}
```
Expand Down
9 changes: 7 additions & 2 deletions lua/obsidian/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,20 @@ end
---@param states table|nil Optional table containing checkbox states (e.g., {" ", "x"}).
---@param line_num number|nil Optional line number to toggle the checkbox on. Defaults to the current line.
M.toggle_checkbox = function(states, line_num)
if not util.in_node { "list", "paragraph" } or util.in_node "block_quote" then
if
(not util.in_node { "list", "paragraph" } or util.in_node "block_quote")
and not Obsidian.opts.checkbox.create_on_empty
then
return
end
line_num = line_num or unpack(vim.api.nvim_win_get_cursor(0))
local line = vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, false)[1]

local checkboxes = states or { " ", "x" }

if util.is_checkbox(line) then
if (line == "" or nil) and Obsidian.opts.checkbox.create_on_empty then
line = "- [ ] "
elseif util.is_checkbox(line) then
for i, check_char in ipairs(checkboxes) do
if string.match(line, "^.* %[" .. vim.pesc(check_char) .. "%].*") then
i = i % #checkboxes
Expand Down
3 changes: 2 additions & 1 deletion lua/obsidian/commands/toggle_checkbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ local toggle_checkbox = require("obsidian.api").toggle_checkbox
return function(_, data)
local start_line, end_line
local checkboxes = Obsidian.opts.checkbox.order
local create_on_empty = Obsidian.opts.checkbox.create_on_empty
start_line = data.line1
end_line = data.line2

local buf = vim.api.nvim_get_current_buf()

for line_nb = start_line, end_line do
local current_line = vim.api.nvim_buf_get_lines(buf, line_nb - 1, line_nb, false)[1]
if current_line and current_line:match "%S" then
if (current_line and current_line:match "%S") or create_on_empty then
toggle_checkbox(checkboxes, line_nb)
end
end
Expand Down
4 changes: 4 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,13 @@ config.default = {
---
---Whether to create new checkbox on paragraphs
---@field create_new? boolean
---
---Whether to create new checkbox on empty lines
---@field create_on_empty? boolean
checkbox = {
enabled = true,
create_new = true,
create_on_empty = false,
order = { " ", "~", "!", ">", "x" },
},

Expand Down
Loading