Skip to content

Commit 0986638

Browse files
feat: add ability to center headings and code blocks
## Details Issue: #179 This change allows `left_pad` and `right_pad` for both headings and code blocks to interpret floating point values < 1 as a percentage of available space. I.e. a value of 0.5 will mean 50% of the available window space after removing the max width of the block, i.e. center it. Integer values >= 1 continue to function as before. - The `language_pad` option is also interpreted in this way now The term block is used because we do not want to center every line of a code block individually, instead we want the entire block to be centered. This also applies to headings but these are generally one line anyway so no real difference. This also adds a `left_margin` option to both headings and code blocks. The differences between padding and margin are: - Paddings uses the highlight associated with the block - Margin uses the overall window background color so it's "inivisible" - It is also important to note that margin is computed after accounting for padding As an example lets say we have a heading that occupies 40% of the windows width. And lets say we are applying the following config: `{ width = 'block', left_pad = 0.33, right_pad = 0.33, left_margin = 0.5 }` - We find that 60% of the width is availble - We add 33% of this to the left and right side, i.e. 20% of total, with the heading background - For margin we find that 100% - (40% + 20% + 20%) = 20% of the space is available so we add half (10%) to the left with the background color - Naturally this will leave the remaining 10% on the right so things should look centered - I find this pretty neat :) In a general sense margin is probably not very useful for `full` widths but you can use it if you like.
1 parent 75a0a95 commit 0986638

File tree

10 files changed

+212
-79
lines changed

10 files changed

+212
-79
lines changed

Diff for: README.md

+26
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,15 @@ require('render-markdown').setup({
256256
-- Can also be an array of the above values in which case the 'level' is used
257257
-- to index into the array using a clamp
258258
width = 'full',
259+
-- Amount of margin to add to the left of headings
260+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
261+
-- Margin available space is computed after accounting for padding
262+
left_margin = 0,
259263
-- Amount of padding to add to the left of headings
264+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
260265
left_pad = 0,
261266
-- Amount of padding to add to the right of headings when width is 'block'
267+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
262268
right_pad = 0,
263269
-- Minimum width to use for headings when width is 'block'
264270
min_width = 0,
@@ -307,6 +313,7 @@ require('render-markdown').setup({
307313
-- left: left side of code block
308314
position = 'left',
309315
-- Amount of padding to add around the language
316+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
310317
language_pad = 0,
311318
-- An array of language names for which background highlighting will be disabled
312319
-- Likely because that language has background highlights itself
@@ -315,9 +322,15 @@ require('render-markdown').setup({
315322
-- block: width of the code block
316323
-- full: full width of the window
317324
width = 'full',
325+
-- Amount of margin to add to the left of code blocks
326+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
327+
-- Margin available space is computed after accounting for padding
328+
left_margin = 0,
318329
-- Amount of padding to add to the left of code blocks
330+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
319331
left_pad = 0,
320332
-- Amount of padding to add to the right of code blocks when width is 'block'
333+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
321334
right_pad = 0,
322335
-- Minimum width to use for code blocks when width is 'block'
323336
min_width = 0,
@@ -601,9 +614,15 @@ require('render-markdown').setup({
601614
-- Can also be an array of the above values in which case the 'level' is used
602615
-- to index into the array using a clamp
603616
width = 'full',
617+
-- Amount of margin to add to the left of headings
618+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
619+
-- Margin available space is computed after accounting for padding
620+
left_margin = 0,
604621
-- Amount of padding to add to the left of headings
622+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
605623
left_pad = 0,
606624
-- Amount of padding to add to the right of headings when width is 'block'
625+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
607626
right_pad = 0,
608627
-- Minimum width to use for headings when width is 'block'
609628
min_width = 0,
@@ -661,6 +680,7 @@ require('render-markdown').setup({
661680
-- left: left side of code block
662681
position = 'left',
663682
-- Amount of padding to add around the language
683+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
664684
language_pad = 0,
665685
-- An array of language names for which background highlighting will be disabled
666686
-- Likely because that language has background highlights itself
@@ -669,9 +689,15 @@ require('render-markdown').setup({
669689
-- block: width of the code block
670690
-- full: full width of the window
671691
width = 'full',
692+
-- Amount of margin to add to the left of code blocks
693+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
694+
-- Margin available space is computed after accounting for padding
695+
left_margin = 0,
672696
-- Amount of padding to add to the left of code blocks
697+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
673698
left_pad = 0,
674699
-- Amount of padding to add to the right of code blocks when width is 'block'
700+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
675701
right_pad = 0,
676702
-- Minimum width to use for code blocks when width is 'block'
677703
min_width = 0,

Diff for: doc/render-markdown.txt

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 September 23
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 September 24
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*
@@ -302,9 +302,15 @@ Full Default Configuration ~
302302
-- Can also be an array of the above values in which case the 'level' is used
303303
-- to index into the array using a clamp
304304
width = 'full',
305+
-- Amount of margin to add to the left of headings
306+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
307+
-- Margin available space is computed after accounting for padding
308+
left_margin = 0,
305309
-- Amount of padding to add to the left of headings
310+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
306311
left_pad = 0,
307312
-- Amount of padding to add to the right of headings when width is 'block'
313+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
308314
right_pad = 0,
309315
-- Minimum width to use for headings when width is 'block'
310316
min_width = 0,
@@ -353,6 +359,7 @@ Full Default Configuration ~
353359
-- left: left side of code block
354360
position = 'left',
355361
-- Amount of padding to add around the language
362+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
356363
language_pad = 0,
357364
-- An array of language names for which background highlighting will be disabled
358365
-- Likely because that language has background highlights itself
@@ -361,9 +368,15 @@ Full Default Configuration ~
361368
-- block: width of the code block
362369
-- full: full width of the window
363370
width = 'full',
371+
-- Amount of margin to add to the left of code blocks
372+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
373+
-- Margin available space is computed after accounting for padding
374+
left_margin = 0,
364375
-- Amount of padding to add to the left of code blocks
376+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
365377
left_pad = 0,
366378
-- Amount of padding to add to the right of code blocks when width is 'block'
379+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
367380
right_pad = 0,
368381
-- Minimum width to use for code blocks when width is 'block'
369382
min_width = 0,
@@ -647,9 +660,15 @@ Wiki Page
647660
-- Can also be an array of the above values in which case the 'level' is used
648661
-- to index into the array using a clamp
649662
width = 'full',
663+
-- Amount of margin to add to the left of headings
664+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
665+
-- Margin available space is computed after accounting for padding
666+
left_margin = 0,
650667
-- Amount of padding to add to the left of headings
668+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
651669
left_pad = 0,
652670
-- Amount of padding to add to the right of headings when width is 'block'
671+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
653672
right_pad = 0,
654673
-- Minimum width to use for headings when width is 'block'
655674
min_width = 0,
@@ -709,6 +728,7 @@ Wiki Page
709728
-- left: left side of code block
710729
position = 'left',
711730
-- Amount of padding to add around the language
731+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
712732
language_pad = 0,
713733
-- An array of language names for which background highlighting will be disabled
714734
-- Likely because that language has background highlights itself
@@ -717,9 +737,15 @@ Wiki Page
717737
-- block: width of the code block
718738
-- full: full width of the window
719739
width = 'full',
740+
-- Amount of margin to add to the left of code blocks
741+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
742+
-- Margin available space is computed after accounting for padding
743+
left_margin = 0,
720744
-- Amount of padding to add to the left of code blocks
745+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
721746
left_pad = 0,
722747
-- Amount of padding to add to the right of code blocks when width is 'block'
748+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
723749
right_pad = 0,
724750
-- Minimum width to use for code blocks when width is 'block'
725751
min_width = 0,

Diff for: lua/render-markdown/core/context.lua

+13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ function Context:get_offset(info)
9797
return result
9898
end
9999

100+
---@param offset number
101+
---@param width integer
102+
---@return integer
103+
function Context:resolve_offset(offset, width)
104+
if offset <= 0 then
105+
return 0
106+
elseif offset < 1 then
107+
return math.floor(((self:get_width() - width) * offset) + 0.5)
108+
else
109+
return offset
110+
end
111+
end
112+
100113
---@return integer
101114
function Context:get_width()
102115
return vim.api.nvim_win_get_width(self.win)

Diff for: lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.1.5'
7+
M.version = '7.1.6'
88

99
function M.check()
1010
M.start('version')

Diff for: lua/render-markdown/init.lua

+20-5
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ local M = {}
112112
---@field public sign? boolean
113113
---@field public style? render.md.code.Style
114114
---@field public position? render.md.code.Position
115-
---@field public language_pad? integer
115+
---@field public language_pad? number
116116
---@field public disable_background? string[]
117117
---@field public width? render.md.code.Width
118-
---@field public left_pad? integer
119-
---@field public right_pad? integer
118+
---@field public left_margin? number
119+
---@field public left_pad? number
120+
---@field public right_pad? number
120121
---@field public min_width? integer
121122
---@field public border? render.md.code.Border
122123
---@field public above? string
@@ -134,8 +135,9 @@ local M = {}
134135
---@field public icons? string[]
135136
---@field public signs? string[]
136137
---@field public width? render.md.heading.Width|(render.md.heading.Width)[]
137-
---@field public left_pad? integer
138-
---@field public right_pad? integer
138+
---@field public left_margin? number
139+
---@field public left_pad? number
140+
---@field public right_pad? number
139141
---@field public min_width? integer
140142
---@field public border? boolean
141143
---@field public border_prefix? boolean
@@ -327,9 +329,15 @@ M.default_config = {
327329
-- Can also be an array of the above values in which case the 'level' is used
328330
-- to index into the array using a clamp
329331
width = 'full',
332+
-- Amount of margin to add to the left of headings
333+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
334+
-- Margin available space is computed after accounting for padding
335+
left_margin = 0,
330336
-- Amount of padding to add to the left of headings
337+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
331338
left_pad = 0,
332339
-- Amount of padding to add to the right of headings when width is 'block'
340+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
333341
right_pad = 0,
334342
-- Minimum width to use for headings when width is 'block'
335343
min_width = 0,
@@ -378,6 +386,7 @@ M.default_config = {
378386
-- left: left side of code block
379387
position = 'left',
380388
-- Amount of padding to add around the language
389+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
381390
language_pad = 0,
382391
-- An array of language names for which background highlighting will be disabled
383392
-- Likely because that language has background highlights itself
@@ -386,9 +395,15 @@ M.default_config = {
386395
-- block: width of the code block
387396
-- full: full width of the window
388397
width = 'full',
398+
-- Amount of margin to add to the left of code blocks
399+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
400+
-- Margin available space is computed after accounting for padding
401+
left_margin = 0,
389402
-- Amount of padding to add to the left of code blocks
403+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
390404
left_pad = 0,
391405
-- Amount of padding to add to the right of code blocks when width is 'block'
406+
-- If a floating point value < 1 is provided it is treated as a percentage of the available window space
392407
right_pad = 0,
393408
-- Minimum width to use for code blocks when width is 'block'
394409
min_width = 0,

0 commit comments

Comments
 (0)