Skip to content

Commit

Permalink
feat: conceal markdown escape characters. Fixes #170
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 12, 2022
1 parent f5ac589 commit 6824794
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
7 changes: 4 additions & 3 deletions lua/noice/text/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ function NoiceText:highlight(bufnr, ns_id, linenr, byte_start)
end

if self.extmark.lang then
if self.extmark.lang == "markdown" then
Markdown.keys(bufnr)
end
local range = { linenr - self.extmark.lines, 0, linenr, byte_start - 1 }
if Treesitter.has_lang(self.extmark.lang) then
Treesitter.highlight(bufnr, ns_id, range, self.extmark.lang)
else
Syntax.highlight(bufnr, ns_id, range, self.extmark.lang)
end
if self.extmark.lang == "markdown" then
Markdown.keys(bufnr)
Markdown.conceal_escape_characters(bufnr, ns_id, range)
end
return
end

Expand Down
50 changes: 38 additions & 12 deletions lua/noice/text/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ function M.html_entities(text)
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 .. "]"
for i = 1, #chars do
local char = chars:sub(i, i)
assert(("\\" .. char):find(regex) == 1)
end

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)
text = M.html_entities(text)
Expand Down Expand Up @@ -132,13 +161,17 @@ function M.format(message, 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
if md_lines > 0 then
message:append(NoiceText.syntax("markdown", md_lines))
md_lines = 0
end
emit_md()
message:newline()
---@cast block MarkdownCodeBlock
for c, line in ipairs(block.code) do
Expand All @@ -154,10 +187,6 @@ function M.format(message, text)
message:newline()
if M.is_rule(block.line) then
M.horizontal_line(message)
if md_lines > 0 then
message:append(NoiceText.syntax("markdown", md_lines))
md_lines = 0
end
else
message:append(block.line)
for _, t in ipairs(M.get_highlights(block.line)) do
Expand All @@ -167,10 +196,7 @@ function M.format(message, text)
end
end
end
if md_lines > 0 then
message:append(NoiceText.syntax("markdown", md_lines))
md_lines = 0
end
emit_md()
end

function M.keys(buf)
Expand Down

0 comments on commit 6824794

Please sign in to comment.