-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: Support for Markdown text highlight using ==
e.g. ==I'm a highlighted text==
#212
Comments
Duplicate of: #173 I rely completely on tree-sitter to do the heavy lifting of getting relevant sections of the buffer to add decorations to. This has the upsides of being fast, well defined, and easy to use, but comes with the downside that if I can't pull the node directly I'm probably not going to add it to the plugin. Custom parsing is just not something I want to include in this plugin for my own sanity. Since the I get it's a kind of regression and the feature feels like it would be simple to add, but that's unfortunately just not the case, at least currently. I do include a |
Oh thank you so much, I knew you would have a much better and straightforward approach. The I'm unfortunately not really familiar with the Lua language and the treesitter framework. Would this be an easy patch to the handler to enhance the capture of the highlight across multiple lines? I believe that this handler would be a nice addition to your Custom handlers page, as it is not yet crowded with recipes as well as this is already the second time you are asked about this, and more over that as long as treesitter markdown grammar implementation does not support it, it is super valuable asset. Thank you for the support so far |
It's not super straightforward. Once you have the text from You would run the pattern match directly on the It gets tricky since you have to find offset from nearest line boundaries as well as the number of lines covered to figure out the start / end row / column. |
I played around with a multi-line version and came up with this: require('render-markdown').setup({
custom_handlers = {
markdown = {
extends = true,
parse = function(root, buf)
local marks = {}
---@param row { [1]: integer, [2]: integer }
---@param col { [1]: integer, [2]: integer }
---@param conceal? string
---@param hl_group? string
local function append(row, col, conceal, hl_group)
table.insert(marks, {
conceal = row[1] == row[2],
start_row = row[1],
start_col = col[1],
opts = { end_row = row[2], end_col = col[2], conceal = conceal, hl_group = hl_group },
})
end
local text = vim.treesitter.get_node_text(root, buf)
local top_row = root:range()
---@param index integer
---@return integer, integer
local function row_col(index)
local lines = vim.split(text:sub(1, index), '\n', { plain = true })
return top_row + #lines - 1, #lines[#lines]
end
---@type integer|nil
local index = 1
while index ~= nil do
local start_index, end_index = text:find('(=)=[^=]+=(=)', index)
if start_index ~= nil and end_index ~= nil then
local start_row, start_col = row_col(start_index - 1)
local end_row, end_col = row_col(end_index)
-- Hide first 2 equal signs
append({ start_row, start_row }, { start_col, start_col + 2 }, '', nil)
-- Highlight contents
append({ start_row, end_row }, { start_col, end_col }, nil, 'DiffDelete')
-- Hide last 2 equal signs
append({ end_row, end_row }, { end_col - 2, end_col }, '', nil)
index = end_index + 1
else
index = nil
end
end
return marks
end,
},
},
}) |
This works like a charm 🥇. Thank you so much! You saved my day here. I hope this will benefit more people down the line 🙏. It may be worth to include it among your examples/recipes of custom handlers, this remains obviously your call 😉 Thank you again so much! |
This is now handled natively by the plugin after: d6a82d7 |
Oh thank you so much for this addition ❤️ I just gave it a try, highlighting some french text with accents, and it behaved weirdly. Just as if accents were left out of the column tracking or something. It's getting worse as we move the cursor away for the line. I attached a video capture of this in the hope that it give you a better visual representation of what I mean. video.recording.of.render.markdown.highlight.having.issue.to.cope.with.accents.mp4Here is the text used for the attached video capture if you want to give a try at fixing this:
|
## Details Issue: #212 Calculating the end index of highlighted ranges was done based on display width but should be done using byte width. Fix is simple and replaces the usage of `Str.width` with the Lua `#`. Need a better way to keep the concepts of displayed width and byte width and when to use which straight. For the most part displayed width is the correct thing but when placing marks it is not.
Ah, thanks, messed up the usage of displayed width vs. byte width. Should be fixed after: 3a319cd. |
Just tried the fix. It works! Thank you so so much 🏆 |
Is your feature request related to a problem? Please describe.
Sort of. Obsidian.nvim supports text highlighting using
==
pairs, as it seems to be the case as well for Obsidian, Quilt, iA Writer, HackMD, Typora to name a few. This converts toward<mark>I'm a highlighted text</mark>
.In order to benefit from this awesome plugin of yours, it's preferable to disable Obsidian.nvim
ui
module. Since your plugin does not yet support the highlighting syntax, this surface a regression if we turn off Obsidian.nvimui
module.The highlight option in Markdown is super valuable for folks using a markdown wiki as their Second Brain and practicing Progressive Summarization
Describe the solution you'd like
The solution I would see is the support for the
==highlighted text==
syntax. A configuration could be:You may have a much better and straightforward approach.
Describe alternatives you've considered
Tried to follow this Stackoverflow thread answer without luck
Additional information
No response
The text was updated successfully, but these errors were encountered: