Skip to content

help: Can't get the paragraph.min_width config option to work. #401

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
XavierLaRochelle opened this issue Apr 16, 2025 · 3 comments
Closed
Labels
question Further information is requested

Comments

@XavierLaRochelle
Copy link

Neovim version (nvim -v)

0.11.0

Neovim distribution

N/A

Description

I want paragraphs to be rendered in the french style, which means that they have a left margin on the first line only. Something that looks like that :

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent fringilla quam nec 
lectus commodo, ac semper ante semper. Curabitur et consectetur elit, id facilisis magna. 
Donec interdum, risus id aliquet hendrerit, sem ante dictum ex, volutpat imperdiet purus
orci sed metus. Duis sit amet lorem urna.

I was able to achieve it with the following config options :

paragraph = {
  enabled = true,
  left_margin = 4,
  min_width = 80
}

This works fine but the indentation is annoying for small lines. It was my understanding that the this was purpose of the min_width options, i.e. disable paragraph-rendering (aka. adding indentation) if the paragraph is under min_width characters long. I've been testing it for about an hour and no matter what I do, changing min_width has no effect on rendering. I tried disabling potentially conflicting plugins like obsidian.nvim but it still doesn't do anything. Am I not understanding this config option properly?

@XavierLaRochelle XavierLaRochelle added the question Further information is requested label Apr 16, 2025
@MeanderingProgrammer
Copy link
Owner

The documentation on min_width could definitely be improved. Currently:

-- Minimum width to use for paragraphs.

The purpose of min_width is only for percentage based left_margin values. The doc for left_margin is:

-- Amount of margin to add to the left of paragraphs.
-- If a float < 1 is provided it is treated as a percentage of available window space.        

So if you set a value of 0.5 it'll add a left margin of 50% of the available space.

The way this is calculated is by taking the window width, subtracting the width of the longest line in the paragraph, and multiplying by 0.5. For example:

  • Window width = 120
  • Longest paragraph width = 40
  • Left margin added = (120 - 40) * 0.5 = 40

If you specified a min_width value of 80 for example, this would change and instead of treating the width of the paragraph as 40 it'll be treated as 80. So the calculation changes to:

  • Window width = 120
  • max(Longest paragraph width, min_width) = 80
  • Left margin added = (120 - 80) * 0.5 = 20

There's currently no way to specify the rows that should have a margin added within a paragraph, it's an all rows or none functionality, nor is there a way to apply the functionality to only some paragraphs and not others. I'm actually not sure which functionality you want.

I wouldn't expect your example paragraph to work how you would like. Unless the paragraph is a single line and relies on neovim line wrapping, then only the first line will have the margin added since the paragraph as a whole is only one line. If you add line breaks directly into the markdown all of the lines will have the margin.

If all you need is a way to disable the margin I can add a function to do that. Probably by allowing left_margin to be a function that takes the paragraph text as input. From there you could either return 4 or 0 depending on the text length.

MeanderingProgrammer added a commit that referenced this issue Apr 18, 2025
## Details

Request: #401

Allows `paragraph.left_margin` option to be a function that returns a
number based on the context in addition to a flat number. The context
provided currently is just the text value of the node, but can be
expanded in the future if needed.

This can be used to set a different margin based on the length of the
paragraph. For example use a margin of 4 for paragraphs longer than 80
characters, otherwise use no margin:

```lua
require('render-markdown').setup({
    paragraph = {
        left_margin = function(ctx)
            return #ctx.text > 80 and 4 or 0
        end,
    },
})
```
@MeanderingProgrammer
Copy link
Owner

I've added the ability to set left_margin to a function here: dfc1299

After updating if you want to use a left_margin of 4 only if the paragraph is longer than 80 characters you would do:

require('render-markdown').setup({
    paragraph = {
        left_margin = function(ctx)
            return #ctx.text > 80 and 4 or 0
        end,
    },
})

@XavierLaRochelle
Copy link
Author

Yes sorry for taking some time to respond but your solution does solve my problem. I also wasn't clear in my initial question. My example was indeed one line and relies on neovim's line wrapping feature. Should've mentioned it. Anyway, thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants