Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `opts.follow_img_func` option for customizing how to handle image paths.
- Added better handling for undefined template fields, which will now be prompted for.
- Added `ObsidianSetCheckbox` to set the value of a checkbox to the passed in value. Defaults to `x`.

### Changed

Expand Down
3 changes: 3 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local iter = require("obsidian.itertools").iter

local command_lookups = {
ObsidianCheck = "obsidian.commands.check",
ObsidianSetCheckbox = "obsidian.commands.set_checkbox",
ObsidianToggleCheckbox = "obsidian.commands.toggle_checkbox",
ObsidianToday = "obsidian.commands.today",
ObsidianYesterday = "obsidian.commands.yesterday",
Expand Down Expand Up @@ -168,6 +169,8 @@ M.register("ObsidianLinks", { opts = { nargs = 0, desc = "Collect all links with

M.register("ObsidianFollowLink", { opts = { nargs = "?", desc = "Follow reference or link under cursor" } })

M.register("ObsidianSetCheckbox", { opts = { nargs = 1, desc = "Set the value of a checkbox" } })

M.register("ObsidianToggleCheckbox", { opts = { nargs = 0, desc = "Toggle checkbox" } })

M.register("ObsidianWorkspace", { opts = { nargs = "?", desc = "Check or switch workspace" } })
Expand Down
10 changes: 10 additions & 0 deletions lua/obsidian/commands/set_checkbox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local set_checkbox = require("obsidian.util").set_checkbox

---@param client obsidian.Client
return function(client, data)
-- If no check_char is provided, default to "x"
if string.len(data.args) == 0 then
data.args = "x"
end
set_checkbox(data.args)
end
17 changes: 17 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ util.toggle_checkbox = function(opts, line_num)
local checkboxes = opts or { " ", "x" }

if not string.match(line, checkbox_pattern) then
-- Add an empty checkbox if one is not found.
local unordered_list_pattern = "^(%s*)[-*+] (.*)"
if string.match(line, unordered_list_pattern) then
line = string.gsub(line, unordered_list_pattern, "%1- [ ] %2")
Expand All @@ -534,6 +535,22 @@ util.toggle_checkbox = function(opts, line_num)
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
end

---Set the value of a checkbox to a given check_char
util.set_checkbox = function(check_char, line_num)
-- Allow line_num to be optional, defaulting to the current line if not provided
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 checkbox_pattern = "^%s*- %[.] "

if string.match(line, checkbox_pattern) then
-- Only set the check_char if the line contains the checkbox pattern.
line = util.string_replace(line, "- [" .. check_char .. "]", "- [" .. check_char .. "]", 1)
end
-- 0-indexed
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, true, { line })
end

---Determines if the given date is a working day (not weekend)
---
---@param time integer
Expand Down
Loading