Skip to content

Commit 4fb7ea2

Browse files
Get some initial things working for headings and codeblocks, based off of headlines implementation
1 parent f6f7cf9 commit 4fb7ea2

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Diff for: .stylua.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
column_width = 120
2+
line_endings = 'Unix'
3+
indent_type = 'Spaces'
4+
indent_width = 4
5+
quote_style = 'AutoPreferSingle'
6+
call_parentheses = 'Always'
7+
8+
[sort_requires]
9+
enabled = true

Diff for: README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
# markdown.nvim
2+
23
Plugin to improve viewing Markdown files in Neovim
4+
5+
# Dependencies
6+
7+
- [markdown](https://github.com/tree-sitter-grammars/tree-sitter-markdown/tree/split_parser)
8+
parser for [treesitter](https://github.com/nvim-treesitter/nvim-treesitter/tree/master):
9+
Used to parse `markdown` files
10+
11+
12+
# Related Projects
13+
14+
- [headlines.nvim](https://github.com/lukas-reineke/headlines.nvim) - Same high level
15+
idea different features
16+
- [markdown-preview.nvim](https://github.com/iamcco/markdown-preview.nvim) - Uses browser
17+
- [vim-markdown-composer](https://github.com/euclio/vim-markdown-composer) - Uses browser

Diff for: lua/markdown/init.lua

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
local state = require('markdown.state')
2+
3+
local M = {}
4+
5+
---@class UserHighlights
6+
---@field public heading? string
7+
---@field public code? string
8+
9+
---@class UserConfig
10+
---@field public query? Query
11+
---@field public bullets? string[]
12+
---@field public highlights? Highlights
13+
14+
---@param opts UserConfig|nil
15+
function M.setup(opts)
16+
---@type Config
17+
local default_config = {
18+
query = vim.treesitter.query.parse(
19+
'markdown',
20+
[[
21+
(atx_heading [
22+
(atx_h1_marker)
23+
(atx_h2_marker)
24+
(atx_h3_marker)
25+
(atx_h4_marker)
26+
(atx_h5_marker)
27+
(atx_h6_marker)
28+
] @heading)
29+
30+
(fenced_code_block) @code
31+
]]
32+
),
33+
bullets = { '', '', '', '' },
34+
highlights = {
35+
heading = '@comment.hint',
36+
code = 'ColorColumn',
37+
},
38+
}
39+
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
40+
41+
vim.api.nvim_create_autocmd({
42+
'FileChangedShellPost',
43+
'InsertLeave',
44+
'Syntax',
45+
'TextChanged',
46+
'WinResized',
47+
}, {
48+
group = vim.api.nvim_create_augroup('Markdown', { clear = true }),
49+
callback = function()
50+
vim.schedule(M.refresh)
51+
end,
52+
})
53+
end
54+
55+
M.namespace = vim.api.nvim_create_namespace('markdown.nvim')
56+
57+
M.refresh = function()
58+
if vim.bo.filetype ~= 'markdown' then
59+
return
60+
end
61+
62+
-- Remove existing highlights / virtual text
63+
vim.api.nvim_buf_clear_namespace(0, M.namespace, 0, -1)
64+
65+
local parser = vim.treesitter.get_parser(0, 'markdown')
66+
local root = parser:parse()[1]:root()
67+
68+
local highlights = state.config.highlights
69+
70+
---@diagnostic disable-next-line: missing-parameter
71+
for id, node in state.config.query:iter_captures(root, 0) do
72+
local capture = state.config.query.captures[id]
73+
local start_row, _, end_row, _ = node:range()
74+
75+
if capture == 'heading' then
76+
local level = #vim.treesitter.get_node_text(node, 0)
77+
local bullet = state.config.bullets[((level - 1) % #state.config.bullets) + 1]
78+
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, 0, {
79+
end_row = end_row + 1,
80+
end_col = 0,
81+
hl_group = highlights.heading,
82+
-- This is done to exactly cover over the heading hashtags
83+
virt_text = { { string.rep(' ', level - 1) .. bullet, highlights.heading } },
84+
virt_text_pos = 'overlay',
85+
hl_eol = true,
86+
})
87+
elseif capture == 'code' then
88+
vim.api.nvim_buf_set_extmark(0, M.namespace, start_row, 0, {
89+
end_row = end_row,
90+
end_col = 0,
91+
hl_group = highlights.code,
92+
hl_eol = true,
93+
})
94+
end
95+
end
96+
end
97+
98+
return M

Diff for: lua/markdown/state.lua

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---@class Highlights
2+
---@field public heading string
3+
---@field public code string
4+
5+
---@class Config
6+
---@field public query Query
7+
---@field public bullets string[]
8+
---@field public highlights Highlights
9+
10+
---@class State
11+
---@field config Config
12+
local state = {}
13+
return state

0 commit comments

Comments
 (0)