Skip to content

Commit

Permalink
feat(renderer)!: Added support for simple html elements
Browse files Browse the repository at this point in the history
NOTE: You will now need the `html` parser for the plugin to work.

Tags like <u>, <i> will now be concealed and an appropriate highlight
group will be used instead.

Added html elements & email support to tables.

WARNING: Due to limitations of lua only single tags work as intended
inside tables.
  • Loading branch information
OXY2DEV committed Aug 2, 2024
1 parent 3c8b0dc commit 94ce522
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 28 deletions.
12 changes: 7 additions & 5 deletions ftplugin/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ local utils = require("markview.utils");

local ts_available, treesitter_parsers = pcall(require, "nvim-treesitter.parsers");
local function parser_installed(parser)
return (ts_available and treesitter_parsers.has_parser(parser)) or
(vim.treesitter.query.get(parser, "highlights"))
return (ts_available and treesitter_parsers.has_parser(parser)) or pcall(vim.treesitter.query.get, parser, "highlights")
end

-- Check for requirements
if vim.fn.has("nvim-0.10") == 0 then
warn("[ markview.nvim ] : Thie plugin is only available on version 0.10.0 and higher!");
vim.notify("[ markview.nvim ] : Thie plugin is only available on version 0.10.0 and higher!", vim.log.levels.WARN);
return;
elseif not parser_installed("markdown") then
warn("[ markview.nvim ] : Treesitter parser for 'markdown' wasn't found!");
vim.notify("[ markview.nvim ] : Treesitter parser for 'markdown' wasn't found!", vim.log.levels.WARN);
return;
elseif not parser_installed("markdown_inline") then
warn("[ markview.nvim ] : Treesitter parser for 'markdown_inline' wasn't found!");
vim.notify("[ markview.nvim ] : Treesitter parser for 'markdown_inline' wasn't found!", vim.log.levels.WARN);
return;
elseif not parser_installed("html") then
vim.notify("[ markview.nvim ] : Treesitter parser for 'html' wasn't found! It is required for basic html tag support.", vim.log.levels.WARN);
return;
end

Expand Down
11 changes: 11 additions & 0 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,17 @@ markview.configuration = {
}
}
},
html = {
default = {
conceal = false
},

types = {
p = { conceal = true, hl = "Title" },
u = { conceal = true, hl = "Underlined" },
i = { conceal = true, hl = "Italic" }
}
},

links = {
enable = true,
Expand Down
50 changes: 50 additions & 0 deletions lua/markview/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,52 @@ parser.md_inline = function (buffer, TStree, from, to)
end
end

parser.html = function (buffer, TStree, from, to)
local scanned_queies = vim.treesitter.query.parse("html", [[
((element) @elem)
;((entity_reference) @entity)
]]);

for capture_id, capture_node, _, _ in scanned_queies:iter_captures(TStree:root(), buffer, from, to) do
local capture_name = scanned_queies.captures[capture_id];
local capture_text = vim.treesitter.get_node_text(capture_node, buffer);
local row_start, col_start, row_end, col_end = capture_node:range();

if capture_name == "elem" then
local node_childs = capture_node:named_child_count();

local start_tag = capture_node:named_child(0);
local end_tag = capture_node:named_child(node_childs - 1);

if start_tag:type() == "start_tag" and end_tag:type() == "end_tag" and row_start == row_end then
local _, ts_col_start, _, ts_col_end = start_tag:range();
local _, te_col_start, _, te_col_end = end_tag:range();

table.insert(parser.parsed_content, {
node = capture_node,
type = "html_inline",

tag = vim.treesitter.get_node_text(start_tag, buffer):gsub("[</>]", ""):match("%a+"),
text = capture_text,

start_tag_col_start = ts_col_start,
start_tag_col_end = ts_col_end,

end_tag_col_start = te_col_start,
end_tag_col_end = te_col_end,

row_start = row_start,
row_end = row_end,

col_start = col_start,
col_end = col_end
})
end
end
end
end

--- Initializes the parsers on the specified buffer
--- Parsed data is stored as a "view" in renderer.lua
---
Expand All @@ -541,6 +587,8 @@ parser.init = function (buffer, config_table)
parser.md(buffer, TStree)
elseif tree_language == "markdown_inline" then
parser.md_inline(buffer, TStree);
elseif tree_language == "html" then
parser.html(buffer, TStree);
end
end)

Expand All @@ -566,6 +614,8 @@ parser.parse_range = function (buffer, config_table, from, to)
parser.md(buffer, TStree, from, to)
elseif tree_language == "markdown_inline" then
parser.md_inline(buffer, TStree, from, to);
elseif tree_language == "html" then
parser.html(buffer, TStree, from, to);
end
end)

Expand Down
Loading

0 comments on commit 94ce522

Please sign in to comment.