Skip to content

feature: Highlight text background when double = on each side #173

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

Closed
barcell1 opened this issue Sep 19, 2024 · 4 comments
Closed

feature: Highlight text background when double = on each side #173

barcell1 opened this issue Sep 19, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@barcell1
Copy link

Is your feature request related to a problem? Please describe.

In some markdown apps, like obsidian, if you do a double = between a text it would highlight the text, with yellow background (and also turn black the letter color) and would hide the double = on each side

like ==some text here==
would become :
like some text here (with yellow background and dark letter color)

Describe the solution you'd like

hide the double = on each side and paint the background color with yellow (could be a custom color) and paint the letter with black color

Describe alternatives you've considered

none

Additional information

thanks in advance, it is a amazing plugin so far

@barcell1 barcell1 added the enhancement New feature or request label Sep 19, 2024
@MeanderingProgrammer
Copy link
Owner

Hi, I assume you're talking about this feature of Obsidian: https://help.obsidian.md/Editing+and+formatting/Basic+formatting+syntax#Bold,%20italics,%20highlights.

I rely on tree-sitter to do the entirety of the document parsing and am not open to doing line by line string parsing to implement features, for my own sanity.

Since the == syntax is not recognized as any kind of node by the markdown parser I'm not going to add this.

Appreciate the idea, is sometimes hard to explain why some seemingly easy tasks are hard and vice versa. The other features in that Obsidian document are handled by the markdown parser.

@MeanderingProgrammer MeanderingProgrammer closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2024
@barcell1
Copy link
Author

barcell1 commented Sep 20, 2024

thanks for your response, didnt know this was not standard, do you know if somehow i can do that in my side like a code snippet ?

Im still not neovim tech savy, hence the question.

obsidian.nvim implement this highlight feature if i turn on their ui plugin config, but i keep it disable because it mess with your plugin ui, and your ui is better

@MeanderingProgrammer
Copy link
Owner

Sure, you can use the extension point of this plugin called custom_handlers. I wrote a version of it that'll probably handle most cases. If you come across problems you can use some classic print debugging to figure out what's going on. Your config for this plugin will look like so:

require('render-markdown').setup({
  custom_handlers = {
    markdown = {
      extends = true,
      parse = function(root, buf)
        local marks = {}

        ---@param row integer
        ---@param start_col integer
        ---@param end_col integer
        ---@param conceal? string
        ---@param hl_group? string
        local function append(row, start_col, end_col, conceal, hl_group)
          table.insert(marks, {
            conceal = true,
            start_row = row,
            start_col = start_col,
            opts = { end_row = row, end_col = end_col, conceal = conceal, hl_group = hl_group },
          })
        end

        local start_row = root:range()
        local text = vim.treesitter.get_node_text(root, buf)
        for i, line in ipairs(vim.split(text, '\n', { plain = true })) do
          local row = start_row + i - 1
          ---@type integer|nil
          local position = 1
          while position ~= nil do
            local start_col, end_col = line:find('(=)=[^=]+=(=)', position)
            if start_col ~= nil and end_col ~= nil then
              -- Translate 1 based index to 0 based index, update position
              start_col, position = start_col - 1, end_col + 1
              -- Hide first 2 equal signs
              append(row, start_col, start_col + 2, '', nil)
              -- Highlight contents
              append(row, start_col, end_col, nil, 'DiffDelete')
              -- Hide last 2 equal signs
              append(row, end_col - 2, end_col, '', nil)
            else
              position = nil
            end
          end
        end
        return marks
      end,
    },
  },
})

@MeanderingProgrammer
Copy link
Owner

This is now handled natively by the plugin after: d6a82d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants