Skip to content

Commit

Permalink
feat: Added custom checkbox states
Browse files Browse the repository at this point in the history
Checkboxes can now have custom states. For trying out this feature, the
plugin now comes with 2 custom states, [~] for on progress tasks & [o]
for cancelled tasks.

Closes #117
  • Loading branch information
OXY2DEV committed Aug 21, 2024
1 parent 169faa8 commit ff96638
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ An experimental `markdown` previewer for Neovim.
Markview.nvim comes with a ton of features such as,

- Close to `full render` of markdown documents. Currently supported items are,
* Block quotes(includes `callouts`/`alerts`
* Chekboxes(checked, unchecked & pending state)
* Headings(atx_headings & setext_headings)
* Horizontal rules
* Html support(only for simple tags, e.g. `<u>Underline</u>`)
* Html entites(both `&<name>;` and `&<name>` support)
* Inline codes
* Links(hyprlinks, images & email support)
* List item(ordered & unordered)
* Tables
* Block quotes(includes `callouts`/`alerts`.
* Chekboxes(checked, unchecked, pending & custom states, e.g. `[~]` for in progress).
* Headings(atx_headings & setext_headings).
* Horizontal rules.
* Html support(only for simple tags, e.g. `<u>Underline</u>`).
* Html entites(both `&<name>;` and `&<name>` support).
* Inline codes.
* Links(hyprlinks, images & email support).
* List item(ordered & unordered).
* Tables.
- Fully customisable setup! You can customise everything to your needs!
- A `hybrid mode` that allows rendering in real-time! It will even unconceal nodes under the cursor.
- Dynamic `highlight groups` that allows support for almost any colorscheme!
Expand Down Expand Up @@ -278,6 +278,17 @@ Checkboxes use these highlight groups,
- MarkviewCheckboxUnhecked, from `DiagnosticError`.
- MarkviewCheckboxPending, from `DiagnosticWarn`.

Markview also comes with 2 custom checkbox states,

>[!Note]
> These are purely for custom notes and aren't taken from external tools(e.g. Obsidian).
- MarkviewCheckboxProgress, from `Keyword`.
Checkboes using `[~]`.

- MarkviewCheckboxCancelled, from `Comment`.
Checkboxes using `[o]`.

### 💻 Code blocks & inline codes

Code blocks use the following highlight group,
Expand Down
8 changes: 7 additions & 1 deletion lua/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,18 @@
---
--- Configuration table for the unchecked state
---@field unchecked markview.render_config.checkbox.state
---
--- Configuration table for the unchecked state
---@field custom markview.render_config.checkbox.state[]?


--- Configuration table for the checkbox state
---@class markview.render_config.checkbox.state
---
--- Text to use as the ustom checkbox
--- The text inside [] checkboxes to match
---@field match string?
---
--- Text to use as the custom checkbox
---@field text string
---
--- Highlight group for text
Expand Down
37 changes: 37 additions & 0 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,31 @@ markview.configuration = {
return { fg = fg, default = true };
end
},
{
group_name = "CheckboxProgress",
value = function ()
local fg = markview.colors.get({
markview.colors.get_hl_value(0, "Conditional", "fg"),
markview.colors.get_hl_value(0, "Keyword", "fg"),

vim.o.background == "dark" and "#cba6f7" or "#8839ef"
});

return { fg = fg, default = true };
end
},
{
group_name = "CheckboxCancelled",
value = function ()
local fg = markview.colors.get({
markview.colors.get_hl_value(0, "Comment", "fg"),

vim.o.background == "dark" and "#6c7086" or "#9ca0b0";
});

return { fg = fg, default = true };
end
},


{
Expand Down Expand Up @@ -1415,6 +1440,18 @@ markview.configuration = {
},
unchecked = {
text = "", hl = "MarkviewCheckboxUnchecked"
},
custom = {
{
match = "~",
text = "",
hl = "MarkviewCheckboxProgress"
},
{
match = "o",
text = "󰩹",
hl = "MarkviewCheckboxCancelled"
}
}
},

Expand Down
10 changes: 6 additions & 4 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,15 @@ parser.md_inline = function (buffer, TStree, from, to)
local line = vim.api.nvim_buf_get_lines(buffer, row_start, row_start + 1, false);
local title = string.match(line ~= nil and line[1] or "", "%b[]%s*(.*)$")

if capture_text == "[-]" then
if capture_text:match("%[(.)%]") then
for _, extmark in ipairs(parser.parsed_content) do
if extmark.type == "list_item" and extmark.row_start == row_start then
local marker = capture_text:match("%[(.)%]");

local start_line = extmark.list_lines[1] or "";
local atStart = start_line:match("%-%s+(%[%-%])%s+");
local atStart = start_line:match("[+%-*]%s+(%[%" .. marker .. "%])%s+");

local chk_start, _ = start_line:find("%[%-%]");
local chk_start, _ = start_line:find("%[%" .. marker .. "%]");

if not atStart or not chk_start or chk_start - 1 ~= col_start then
goto invalid;
Expand All @@ -540,7 +542,7 @@ parser.md_inline = function (buffer, TStree, from, to)
table.insert(parser.parsed_content, {
node = capture_node,
type = "checkbox",
state = "pending",
state = capture_text:match("%[(.)%]") == "-" and "pending" or capture_text:match("%[(.)%]"),

row_start = row_start,
row_end = row_end,
Expand Down
6 changes: 6 additions & 0 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,12 @@ renderer.render_checkboxes = function (buffer, content, config_table)
chk_config = config_table.unchecked;
elseif content.state == "pending" then
chk_config = config_table.pending;
elseif vim.islist(config_table.custom) then
for _, config in ipairs(config_table.custom) do
if content.state == config.match then
chk_config = config;
end
end
end

if not chk_config or type(chk_config.text) ~= "string" then
Expand Down
2 changes: 1 addition & 1 deletion markview.nvim.wiki

0 comments on commit ff96638

Please sign in to comment.