|
| 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 |
0 commit comments