Skip to content

Commit

Permalink
feat: Added support for YAML & TOML metadata
Browse files Browse the repository at this point in the history
This also adds support to Obsidian-like property icons!
[Note: Needs the YAML treesitter parser]

Ref: #177
  • Loading branch information
OXY2DEV committed Nov 6, 2024
1 parent 81d83eb commit 1f597cc
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 19 deletions.
22 changes: 22 additions & 0 deletions lua/markview/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,28 @@ highlights.dynamic = {
};
---@diagnostic enable
end,
["CodeFg"] = function ()
local vim_bg = highlights.hsl(highlights.color(
"bg",
{ "Normal" },
"#FFFFFF",
"#000000"
));

if vim_bg[3] > 0.5 then
vim_bg[3] = clamp(vim_bg[3] - 0.05, 0.1, 0.9);
else
vim_bg[3] = clamp(vim_bg[3] + 0.05, 0.1, 0.9);
end

---@diagnostic disable
vim_bg = highlights.rgb(vim_bg);

return {
fg = highlights.hex(vim_bg)
};
---@diagnostic enable
end,
["InlineCode"] = function ()
local vim_bg = highlights.hsl(highlights.color(
"bg",
Expand Down
1 change: 1 addition & 0 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parser.markdown_inline = require("markview.parsers.markdown_inline");
parser.html = require("markview.parsers.html");
parser.latex = require("markview.parsers.latex");
parser.typst = require("markview.parsers.typst");
parser.yaml = require("markview.parsers.yaml");

parser.ignore_ranges = {};

Expand Down
110 changes: 110 additions & 0 deletions lua/markview/parsers/yaml.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
local yaml = {};

--- Queried contents
---@type table[]
yaml.content = {};

--- Queried contents, but sorted
yaml.sorted = {}

yaml.insert = function (data)
table.insert(yaml.content, data);

if not yaml.sorted[data.class] then
yaml.sorted[data.class] = {};
end

table.insert(yaml.sorted[data.class], data);
end

yaml.property = function (buffer, TSNode, text, range)
local key, value = TSNode:field("key")[1], TSNode:field("value")[1];

local key_text = key and vim.treesitter.get_node_text(key, buffer) or nil;
local value_text = value and vim.treesitter.get_node_text(value, buffer) or nil;

local value_type = "unknown";

if key_text == "date" then
value_type = "date";
elseif key_text == "time" then
value_type = "time";
elseif key_text == "tags" then
value_type = "tags";
elseif key_text == "aliases" then
value_type = "aliases";
elseif key_text == "cssclasses" then
value_type = "cssclasses";
elseif tonumber(value_text) then
value_type = "number";
elseif value_text == "true" or value_text == "false" then
value_type = "checkbox";
elseif
value and value:child(0) and value:child(0):child(0) and
value:child(0):child(0):type() == "block_sequence_item"
then
value_type = "list";
elseif type(value_text) == "string" then
value_type = "string";
elseif not value_text then
value_type = "nil";
end

if not #text == (range.row_end - range.row_start) then
range.row_end = range.row_start + #text;
end

yaml.insert({
class = "yaml_property",
type = value_type,

text = text,

range = range
});
end

yaml.parse = function (buffer, TSTree, from, to)
-- Clear the previous contents
yaml.sorted = {};
yaml.content = {};

local scanned_queries = vim.treesitter.query.parse("yaml", [[
((block_mapping_pair) @yaml.property)
]]);

for capture_id, capture_node, _, _ in scanned_queries:iter_captures(TSTree:root(), buffer, from, to) do
local capture_name = scanned_queries.captures[capture_id];
local r_start, c_start, r_end, c_end = capture_node:range();

if not capture_name:match("^yaml%.") then
goto continue
end

local capture_text = vim.treesitter.get_node_text(capture_node, buffer);

if not capture_text:match("\n$") then
capture_text = capture_text .. "\n";
end

local lines = {};

for line in capture_text:gmatch("(.-)\n") do
table.insert(lines, line);
end

yaml[capture_name:gsub("^yaml%.", "")](buffer, capture_node, lines, {
row_start = r_start,
col_start = c_start,

row_end = r_end,
col_end = c_end
});

::continue::
end

return yaml.content, yaml.sorted;
end

return yaml;
26 changes: 16 additions & 10 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
local renderer = {};

local mkd = require("markview.renderers.markdown")
local inl = require("markview.renderers.markdown_inline")
local ltx = require("markview.renderers.latex")
renderer.markdown = require("markview.renderers.markdown")
renderer.markdown_inline = require("markview.renderers.markdown_inline")
renderer.latex = require("markview.renderers.latex")
renderer.yaml = require("markview.renderers.yaml")

--- Renders things
---@param buffer integer
renderer.render = function (buffer, parsed_content)
inl.render(buffer, parsed_content.markdown_inline)
mkd.render(buffer, parsed_content.markdown)
ltx.render(buffer, parsed_content.latex)
for lang, content in pairs(parsed_content) do
if renderer[lang] then
renderer[lang].render(buffer, content);
end
end
end

renderer.clear = function (buffer, ignore, from, to)
Expand All @@ -18,12 +21,15 @@ renderer.clear = function (buffer, ignore, from, to)
markdown_inline = {},
html = {},
latex = {},
typst = {}
typst = {},
yaml = {}
}, ignore or {});

mkd.clear(buffer, ignore.markdown, from, to);
inl.clear(buffer, ignore.markdown_inline, from, to);
ltx.clear(buffer, ignore.latex, from, to);
for lang, content in pairs(ignore) do
if renderer[lang] then
renderer[lang].clear(buffer, content, from, to);
end
end
end

renderer.range = function (content)
Expand Down
90 changes: 83 additions & 7 deletions lua/markview/renderers/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,49 @@ markdown.metadata_minus = function (buffer, item)
return;
end

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start, range.col_start, {
vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start, 0, {
undo_restore = false, invalidate = true,
end_col = #item.text[1],
conceal = "",

virt_text_pos = "overlay",
virt_text = config.border_top and {
{
string.rep(
config.border_top,
vim.api.nvim_win_get_width(
utils.buf_getwin(buffer)
)
),
utils.set_hl(config.border_top_hl or config.border_hl or config.hl)
}
} or nil
});

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_end - 1, 0, {
undo_restore = false, invalidate = true,
end_row = range.row_end - 1,
end_col = #item.text[#item.text],
conceal = "|",
conceal = "",

virt_text_pos = "overlay",
virt_text = config.border_bottom and {
{
string.rep(
config.border_bottom,
vim.api.nvim_win_get_width(
utils.buf_getwin(buffer)
)
),
utils.set_hl(config.border_bottom_hl or config.border_hl or config.hl)
}
} or nil
});

if not config.hl then return; end

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start + 1, 0, {
undo_restore = false, invalidate = true,
end_row = range.row_end - 1,

line_hl_group = utils.set_hl(config.hl)
});
Expand All @@ -1129,18 +1167,56 @@ end

markdown.metadata_plus = function (buffer, item)
---+${func, Renders TOML metadata blocks}
local config = get_config("metadata_minus");
local config = get_config("metadata_plus");
local range = item.range;

if not config then
return;
end

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start, range.col_start, {
vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start, 0, {
undo_restore = false, invalidate = true,
end_col = #item.text[1],
conceal = "",

virt_text_pos = "overlay",
virt_text = config.border_top and {
{
string.rep(
config.border_top,
vim.api.nvim_win_get_width(
utils.buf_getwin(buffer)
)
),
utils.set_hl(config.border_top_hl or config.border_hl or config.hl)
}
} or nil
});

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_end - 1, 0, {
undo_restore = false, invalidate = true,
end_row = range.row_end - 1,
end_col = #item.text[#item.text],
conceal = "|",
conceal = "",

virt_text_pos = "overlay",
virt_text = config.border_bottom and {
{
string.rep(
config.border_bottom,
vim.api.nvim_win_get_width(
utils.buf_getwin(buffer)
)
),
utils.set_hl(config.border_bottom_hl or config.border_hl or config.hl)
}
} or nil
});

if not config.hl then return; end

vim.api.nvim_buf_set_extmark(buffer, markdown.ns("metadatas"), range.row_start + 1, 0, {
undo_restore = false, invalidate = true,
end_row = range.row_end - 1,

line_hl_group = utils.set_hl(config.hl)
});
Expand Down
Loading

0 comments on commit 1f597cc

Please sign in to comment.