Skip to content

Commit bd56575

Browse files
fix: handle spaces before atx headings
## Details Atx headings are allowed to have spaces before the starting `#` according to the spec. Probably not common but we should be able to handle this properly. To fix this replaced the level calculation of `#text` which is the text width including spaces, with a more complicated calculation that uses the length of the lua pattern match `^%s*(#+)` or 0. This capture will result in the leading hashtags minus any leading space. Put this in the `str` module and used wherever heading level logic was needed. Also replaced use of `nil` with conditions where possible. The lua `fail` is technically subject to change so avoiding checks on `nil` directly is the more robust approach.
1 parent 7f81e9d commit bd56575

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+247
-253
lines changed

justfile

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
init := "tests/minimal_init.lua"
2+
settings := "{ minimal_init = " + quote(init) + ", sequential = true, keep_going = false }"
23

34
default: update check test health
45

56
update:
6-
# Updates types.lua & README.md
7+
# keep documentation in sync with code
78
python scripts/update.py
8-
# https://pandoc.org/
99
# https://github.com/kdheepak/panvimdoc
10-
../../open-source/panvimdoc/panvimdoc.sh \
10+
../../tools/panvimdoc/panvimdoc.sh \
1111
--project-name render-markdown \
1212
--input-file README.md
1313

@@ -19,21 +19,17 @@ test:
1919
just busted "tests"
2020

2121
bench:
22-
just generate
22+
python scripts/generate.py
2323
just busted "benches"
2424

2525
[private]
26-
busted directory:
27-
nvim --headless --noplugin -u {{init}} \
28-
-c "PlenaryBustedDirectory {{directory}} { minimal_init = '{{init}}', sequential = true, keep_going = false }"
29-
30-
generate:
31-
python scripts/generate.py
26+
busted path:
27+
nvim --headless --noplugin -u {{init}} -c "PlenaryBustedDirectory {{path}} {{settings}}"
3228

3329
health:
3430
nvim -c "checkhealth render-markdown" -- -
3531

36-
cat-log:
32+
log:
3733
cat ~/.local/state/nvim/render-markdown.log
3834

3935
demo: heading table box latex callout

lua/render-markdown/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848

4949
function M.config()
5050
local difference = state.difference()
51-
if difference == nil then
51+
if not difference then
5252
-- selene: allow(deprecated)
5353
vim.print('default configuration')
5454
else

lua/render-markdown/colors.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ end
100100
---@return string
101101
function M.combine(foreground, background, force)
102102
local name = ('%s_%s_%s'):format(M.prefix, foreground, background)
103-
if M.cache.combine[name] == nil or force then
103+
if not M.cache.combine[name] or force then
104104
local fg, bg = M.get_hl(foreground), M.get_hl(background)
105105
vim.api.nvim_set_hl(0, name, {
106106
fg = fg.fg,
@@ -118,7 +118,7 @@ end
118118
---@return string
119119
function M.bg_as_fg(highlight, force)
120120
local name = ('%s_%s_bg_as_fg'):format(M.prefix, highlight)
121-
if M.cache.bg_as_fg[name] == nil or force then
121+
if not M.cache.bg_as_fg[name] or force then
122122
local hl = M.get_hl(highlight)
123123
vim.api.nvim_set_hl(0, name, {
124124
fg = hl.bg,

lua/render-markdown/command.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ function M.command(args)
3232
local fargs, err = args.fargs, nil
3333
if #fargs == 0 or #fargs == 1 then
3434
local command = #fargs == 0 and api.enable or api[fargs[1]]
35-
if command ~= nil then
35+
if command then
3636
command()
3737
else
3838
err = ('unexpected command: %s'):format(fargs[1])
3939
end
4040
else
4141
err = ('unexpected # arguments: %d'):format(#fargs)
4242
end
43-
if err ~= nil then
43+
if err then
4444
vim.notify(('%s: %s'):format(M.plugin, err), vim.log.levels.ERROR)
4545
end
4646
end

lua/render-markdown/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Config.new(root, buf)
4545
for _, name in ipairs({ 'buflisted', 'buftype', 'filetype' }) do
4646
local value = Env.buf.get(buf, name)
4747
local override = root.overrides[name][value]
48-
if override ~= nil then
48+
if override then
4949
config = vim.tbl_deep_extend('force', config, override)
5050
end
5151
end
@@ -156,7 +156,7 @@ function Config:hidden(mode, row)
156156
-- anti-conceal is not enabled -> hide nothing
157157
-- row is not known -> buffer is not active -> hide nothing
158158
local config = self.anti_conceal
159-
if not config.enabled or row == nil then
159+
if not config.enabled or not row then
160160
return nil
161161
end
162162
if Env.mode.is(mode, { 'v', 'V', '\22' }) then

lua/render-markdown/core/conceal.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Conceal:add(row, entry)
4444
if not self:enabled() then
4545
return
4646
end
47-
if self.lines[row] == nil then
47+
if not self.lines[row] then
4848
self.lines[row] = { hidden = false, sections = {} }
4949
end
5050
local line = self.lines[row]
@@ -119,7 +119,7 @@ function Conceal:line(node)
119119
self:compute()
120120
end
121121
local line = self.lines[node.start_row]
122-
if line == nil then
122+
if not line then
123123
line = { hidden = false, sections = {} }
124124
end
125125
return line
@@ -131,11 +131,11 @@ function Conceal:compute()
131131
if not self:enabled() then
132132
return
133133
end
134-
if vim.treesitter.highlighter.active[self.buf] == nil then
134+
if not vim.treesitter.highlighter.active[self.buf] then
135135
return
136136
end
137137
local parser = vim.treesitter.get_parser(self.buf)
138-
if parser == nil then
138+
if not parser then
139139
return
140140
end
141141
parser:for_each_tree(function(tree, language_tree)
@@ -154,18 +154,18 @@ function Conceal:compute_tree(language, root)
154154
return
155155
end
156156
local query = vim.treesitter.query.get(language, 'highlights')
157-
if query == nil then
157+
if not query then
158158
return
159159
end
160160
self.context:for_each(function(range)
161161
local top, bottom = range.top, range.bottom
162162
for id, node, data in query:iter_captures(root, self.buf, top, bottom) do
163-
if data.conceal_lines ~= nil then
163+
if data.conceal_lines then
164164
local node_range = self:node_range(id, node, data)
165165
local row = unpack(node_range)
166166
self:add(row, true)
167167
end
168-
if data.conceal ~= nil then
168+
if data.conceal then
169169
local node_range = self:node_range(id, node, data)
170170
local row, start_col, _, end_col = unpack(node_range)
171171
local text = vim.treesitter.get_node_text(node, self.buf)
@@ -187,11 +187,11 @@ end
187187
---@return Range
188188
function Conceal:node_range(id, node, data)
189189
local range = data.range
190-
if range ~= nil then
190+
if range then
191191
return range
192192
end
193-
range = data[id] ~= nil and data[id].range or nil
194-
if range ~= nil then
193+
range = data[id] and data[id].range or nil
194+
if range then
195195
return range
196196
end
197197
return { node:range() }

lua/render-markdown/core/context.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494
---@param node? render.md.Node
9595
---@return integer
9696
function Context:width(node)
97-
if node == nil then
97+
if not node then
9898
return 0
9999
end
100100
return Str.width(node.text) + self:get_offset(node) - self.conceal:get(node)
@@ -120,7 +120,7 @@ function Context:add_offset(row, offset)
120120
if offset.width <= 0 then
121121
return
122122
end
123-
if self.offsets[row] == nil then
123+
if not self.offsets[row] then
124124
self.offsets[row] = {}
125125
end
126126
local offsets = self.offsets[row]
@@ -206,7 +206,7 @@ M.cache = {}
206206
---@return boolean
207207
function M.contains(buf, win)
208208
local context = M.cache[buf]
209-
return context ~= nil and context:contains(win)
209+
return context and context:contains(win) or false
210210
end
211211

212212
---@param props render.md.context.Props

lua/render-markdown/core/extmark.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121
---@param range? render.md.Range
2222
---@return boolean
2323
function Extmark:overlaps(range)
24-
if range == nil then
24+
if not range then
2525
return false
2626
end
2727
local top = self.mark.start_row
@@ -36,7 +36,7 @@ end
3636
---@param ns integer
3737
---@param buf integer
3838
function Extmark:show(ns, buf)
39-
if self.id ~= nil then
39+
if self.id then
4040
return
4141
end
4242
local mark = self.mark
@@ -60,7 +60,7 @@ end
6060
---@param ns integer
6161
---@param buf integer
6262
function Extmark:hide(ns, buf)
63-
if self.id == nil then
63+
if not self.id then
6464
return
6565
end
6666
vim.api.nvim_buf_del_extmark(buf, ns, self.id)

lua/render-markdown/core/ui.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
---@return render.md.Buffer
5353
function M.get(buf)
5454
local result = M.cache[buf]
55-
if result == nil then
55+
if not result then
5656
result = Buffer.new(buf)
5757
M.cache[buf] = result
5858
end
@@ -167,7 +167,7 @@ end
167167
function M.parse_buffer(props)
168168
local buf = props.buf
169169
local has_parser, parser = pcall(vim.treesitter.get_parser, buf)
170-
if not has_parser or parser == nil then
170+
if not has_parser or not parser then
171171
log.buf('error', 'fail', buf, 'no treesitter parser found')
172172
return {}
173173
end
@@ -209,15 +209,15 @@ function M.parse_tree(context, ctx, language)
209209

210210
local marks = {}
211211
local user = M.config.custom_handlers[language]
212-
if user ~= nil then
212+
if user then
213213
log.buf('debug', 'handler', ctx.buf, 'user')
214214
vim.list_extend(marks, user.parse(ctx))
215215
if not user.extends then
216216
return marks
217217
end
218218
end
219219
local builtin = builtin_handlers[language]
220-
if builtin ~= nil then
220+
if builtin then
221221
log.buf('debug', 'handler', ctx.buf, 'builtin')
222222
vim.list_extend(marks, builtin.parse(ctx))
223223
end

lua/render-markdown/debug/diff.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function M.get(t1, t2)
1515
end
1616
local result = {}
1717
for _, key in ipairs(keys) do
18-
local difference = nil
18+
local difference
1919
local v1, v2 = t1[key], t2[key]
2020
if type(v1) == 'table' and type(v2) == 'table' then
2121
difference = M.get(v1, v2)

0 commit comments

Comments
 (0)