Skip to content

feature: custom heading icons depending on filetype #320

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
petobens opened this issue Feb 5, 2025 · 6 comments
Closed

feature: custom heading icons depending on filetype #320

petobens opened this issue Feb 5, 2025 · 6 comments
Labels
enhancement New feature or request

Comments

@petobens
Copy link

petobens commented Feb 5, 2025

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

Currently we can define heading icons like:

    heading = {
        sign = false,
        icons = { '󰪥 ', '󰺕 ', '', '', '', '' },
        position = 'inline',
    },

and enable render-markdown for filetypes by doing

file_types = { 'markdown', 'chatgpt', 'chatgpt-input', 'codecompanion' },

Describe the solution you'd like

I was wondering whether it would be possible to add the ability to have different heading icons per filetype. In particular I'm using render-markdown with codecompanion ai plugin. The codecompanion markdown chat ui buffer looks like:

Image

where the light blue headers are simply markdown H2 headers. I would love to be able to do something similar to checkbox/links icons for markdown headings:

heading = {
    custom = {
        h2_codecompanion = {
            raw = '##',
            rendered = 'some_icon',
            highlight = 'some_highlight',
            ft = 'codecompanion',
        },

Describe alternatives you've considered

Additional information

No response

@petobens petobens added the enhancement New feature or request label Feb 5, 2025
@MeanderingProgrammer
Copy link
Owner

This plugin already supports this behavior, through the overrides configuration, I see why it would be difficult to find. You can modify every option per filetype, though the configuration is identical to the top level one, and not some custom format like the suggestion.

For example to set custom heading icons for codecompanion buffers you would do:

require('render-markdown').setup({
    overrides = {
        filetype = {
            codecompanion = {
                heading = { icons = { 'h1_icon', 'h2_icon', 'h3_icon', 'h4_icon', 'h5_icon', 'h6_icon' } },
            },
        },
    },
})

At the codecompanion level the configuration is the same as at the top level, and anything you don't define falls back to the top level value. This lets you have different values for links, bullets, really everything can be made custom per filetype.

The reason for the kind of convoluted nested structure is we allow overrides for several different buffer options, including buftype & buflisted currently and potentially more in the future if the use case comes up. Is a more scalable approach.

@petobens
Copy link
Author

petobens commented Feb 5, 2025

Oh that's great! And triggers a follow-up question: is it possible to have different icons and colors for a same H2 heading depending on the H2 title? i.e adding some sort of pattern/regex that changes the icon depending on such pattern. For instance now with your suggested change I can do:

overrides = {
        filetype = {
            codecompanion = {
                heading = {
                    icons = { '󰪥 ', '', '', '', '', '' },
                },
            },
        },
    }

And get:

Image

I would like i) to have an icon for when the H2 text is Me and a different one when it isn't (in order to distinguish my prompt from the LLM) and ii) different hl groups /colors for each H2 heading

@MeanderingProgrammer
Copy link
Owner

That is currently not possible, will see about adding it as a feature :)

MeanderingProgrammer added a commit that referenced this issue Feb 6, 2025
## Details

Request: #320

The desired feature was some way to change the icon, foreground, and
background for a heading based on the text of the heading.

To accomplish have added a new configuration under `heading` called
`custom` which functions very similarly to custom links. Users define a
`pattern` which will be matched against the heading text and if it does
we will use the `icon`, `foreground`, & `background` (if provided) from
the matching custom configuration instead of the top level values.

For example:

```lua
require('render-markdown').setup({
    heading = {
        custom = {
            codecompanion_me = {
                pattern = '^## Me$',
                icon = 'some_icon',
                foreground = 'some_highlight',
                background = 'some_highlight',
            },
        },
    },
})
```
@MeanderingProgrammer
Copy link
Owner

Added this feature here: 5c2440d

It's pretty similar to what you suggested but not at the filetype level (as there's a different way to do that), and it uses lua patterns rather than a direct string equals, so be careful of setting overly generic patterns here.

As an example:

require('render-markdown').setup({
    heading = {
        custom = {
            codecompanion_me = {
                pattern = '^## Me$',
                icon = 'some_icon',
                foreground = 'some_highlight',
                background = 'some_highlight',
            },
        },
    },
})

icon, foreground, & background are all optional and fallback to the value that would have been set if they're not provided. So you can change just the icon, just the highlights, or whatever combination of those you feel like.

@petobens
Copy link
Author

petobens commented Feb 7, 2025

This is fantastic. Thank you :)

I didn't get this bit though: not at the filetype level (as there's a different way to do that). What's the different way?

@MeanderingProgrammer
Copy link
Owner

I was referring to the overrides filetype thing I mentioned earlier, as opposed to an ft option in the custom headings.

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