-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathmarkdown.lua
243 lines (212 loc) · 5.54 KB
/
markdown.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
local require = require("noice.util.lazy")
local NoiceText = require("noice.text")
local Config = require("noice.config")
---@alias MarkdownBlock {line:string}
---@alias MarkdownCodeBlock {code:string[], lang:string}
---@alias Markdown (MarkdownBlock|MarkdownCodeBlock)[]
local M = {}
function M.is_rule(line)
return line and line:find("^%s*[%*%-_][%*%-_][%*%-_]+%s*$")
end
function M.is_code_block(line)
return line and line:find("^%s*```")
end
function M.is_empty(line)
return line and line:find("^%s*$")
end
---@param text string
function M.html_entities(text)
local entities = { nbsp = "", lt = "<", gt = ">", amp = "&", quot = '"' }
for entity, char in pairs(entities) do
text = text:gsub("&" .. entity .. ";", char)
end
return text
end
--- test\_foo
---@param buf buffer
---@param range number[]
function M.conceal_escape_characters(buf, ns, range)
local chars = "\\`*_{}[]()#+-.!"
local regex = "\\["
for i = 1, #chars do
regex = regex .. "%" .. chars:sub(i, i)
end
regex = regex .. "]"
local lines = vim.api.nvim_buf_get_lines(buf, range[1], range[3] + 1, false)
for l, line in ipairs(lines) do
local c = line:find(regex)
while c do
vim.api.nvim_buf_set_extmark(buf, ns, range[1] + l - 1, c - 1, {
end_col = c,
conceal = "",
})
c = line:find(regex, c + 1)
end
end
end
---@param text string
function M.parse(text)
---@type string
text = text:gsub("</?pre>", "```")
text = M.html_entities(text)
---@type Markdown
local ret = {}
local lines = vim.split(text, "\n")
local l = 1
local function eat_nl()
while M.is_empty(lines[l + 1]) do
l = l + 1
end
end
while l <= #lines do
local line = lines[l]
if M.is_empty(line) then
local is_start = l == 1
eat_nl()
local is_end = l == #lines
if not (M.is_code_block(lines[l + 1]) or M.is_rule(lines[l + 1]) or is_start or is_end) then
table.insert(ret, { line = "" })
end
elseif M.is_code_block(line) then
---@type string
local lang = line:match("```(%S+)") or "text"
local block = { lang = lang, code = {} }
while lines[l + 1] and not M.is_code_block(lines[l + 1]) do
table.insert(block.code, lines[l + 1])
l = l + 1
end
local prev = ret[#ret]
if prev and not M.is_rule(prev.line) then
table.insert(ret, { line = "" })
end
table.insert(ret, block)
l = l + 1
eat_nl()
elseif M.is_rule(line) then
table.insert(ret, { line = "---" })
eat_nl()
else
local prev = ret[#ret]
if prev and prev.code then
table.insert(ret, { line = "" })
end
table.insert(ret, { line = line })
end
l = l + 1
end
return ret
end
function M.get_highlights(line)
---@type NoiceText[]
local ret = {}
for pattern, hl_group in pairs(Config.options.markdown.highlights) do
local from = 1
while from do
---@type number, string?
local to, match
---@type number, number, string?
from, to, match = line:find(pattern, from)
if match then
---@type number, number
from, to = line:find(match, from)
end
if from then
table.insert(
ret,
NoiceText("", {
hl_group = hl_group,
col = from - 1,
length = to - from + 1,
-- priority = 120,
})
)
end
from = to and to + 1 or nil
end
end
return ret
end
---@param message NoiceMessage
---@param text string
--```lua
--local a = 1
--local b = true
--```
--foo tex
function M.format(message, text)
local blocks = M.parse(text)
local md_lines = 0
local function emit_md()
if md_lines > 0 then
message:append(NoiceText.syntax("markdown", md_lines))
md_lines = 0
end
end
for l = 1, #blocks do
local block = blocks[l]
if block.code then
emit_md()
message:newline()
---@cast block MarkdownCodeBlock
for c, line in ipairs(block.code) do
message:append(line)
if c == #block.code then
message:append(NoiceText.syntax(block.lang, #block.code))
else
message:newline()
end
end
else
---@cast block MarkdownBlock
message:newline()
if M.is_rule(block.line) then
M.horizontal_line(message)
else
message:append(block.line)
for _, t in ipairs(M.get_highlights(block.line)) do
message:append(t)
end
md_lines = md_lines + 1
end
end
end
emit_md()
end
function M.keys(buf)
if vim.b[buf].markdown_keys then
return
end
local function map(lhs)
vim.keymap.set("n", lhs, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1
for pattern, handler in pairs(Config.options.markdown.hover) do
local from = 1
local to, url
while from do
from, to, url = line:find(pattern, from)
if from and col >= from and col <= to then
return handler(url)
end
if from then
from = to + 1
end
end
end
vim.api.nvim_feedkeys(lhs, "n", false)
end, { buffer = buf, silent = true })
end
map("gx")
map("K")
vim.b[buf].markdown_keys = true
end
---@param message NoiceMessage
function M.horizontal_line(message)
message:append(NoiceText("", {
virt_text_win_col = 0,
virt_text = { { string.rep("─", vim.go.columns), "@punctuation.special.markdown" } },
priority = 100,
}))
end
return M