Skip to content

Commit

Permalink
Add support for callout syntax
Browse files Browse the repository at this point in the history
# Details

Resolves: MeanderingProgrammer#20

Replaces `shortcut_link` text that matches callout keywords with
configured symbols / text and applies highlights.

Unfortunately highlighting the quote bar itself is NOT supported as
we need information from the inline parser to figure out what
the callout is, while the quote / bar itself is handled by the
standard block level markdown parser.

Passing information between the parsers is not really possible, as
far as I am aware.

Any other solution would rely on a text search and would get tricky
across multi line block quotes, which all callouts are.

Will create a separate issue to make the highlighting consistent.
  • Loading branch information
MeanderingProgrammer committed May 22, 2024
1 parent 49f4597 commit 43bbefd
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 31 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Plugin to improve viewing Markdown files in Neovim
- Updates table borders with better border characters, does NOT automatically align
- Basic support for `LaTeX` if `pylatexenc` is installed on system
- Disable rendering when file is larger than provided value
- Support for [callouts](https://github.com/orgs/community/discussions/16925)

# Dependencies

Expand Down Expand Up @@ -103,6 +104,8 @@ require('render-markdown').setup({
-- Capture groups that get pulled from inline markdown
inline_query = [[
(code_span) @code
(shortcut_link) @callout
]],
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
-- Only intended to be used for plugin development / debugging
Expand All @@ -126,6 +129,14 @@ require('render-markdown').setup({
},
-- Character that will replace the > at the start of block quotes
quote = '',
-- Symbol / text to use for different callouts
callout = {
note = ' Note',
tip = ' Tip',
important = '󰅾 Important',
warning = ' Warning',
caution = '󰳦 Caution',
},
-- See :h 'conceallevel' for more information about meaning of values
conceal = {
-- conceallevel used for buffer when not being rendered, get user setting
Expand Down Expand Up @@ -175,6 +186,14 @@ require('render-markdown').setup({
latex = '@markup.math',
-- Quote character in a block quote
quote = '@markup.quote',
-- Highlights to use for different callouts
callout = {
note = 'DiagnosticInfo',
tip = 'DiagnosticOk',
important = 'DiagnosticHint',
warning = 'DiagnosticWarn',
caution = 'DiagnosticError',
},
},
})
```
Expand Down
Binary file modified demo/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions demo/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ def main(zoom: int, file: str, cast: str) -> None:
pyautogui.hotkey("`", "c")
time.sleep(1.0)

# Zoom in
for _ in range(zoom):
pyautogui.hotkey("command", "=")
# Zoom in / out
zoom_key = "=" if zoom >= 0 else "-"
for _ in range(abs(zoom)):
pyautogui.hotkey("command", zoom_key)

# Start recording demo file
# https://docs.asciinema.org/manual/cli/usage/
Expand Down
6 changes: 6 additions & 0 deletions demo/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ if __name__ == "__main__":
main()
```

> [!NOTE]
> [!TIP]
> [!IMPORTANT]
> [!WARNING]
> [!CAUTION]
- List Item 1: with [link](https://example.com)
* List Item 2: with `inline` code
* Nested List 1 Item 1
Expand Down
21 changes: 20 additions & 1 deletion doc/render-markdown.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*render-markdown.txt* For 0.10.0 Last change: 2024 May 21
*render-markdown.txt* For 0.10.0 Last change: 2024 May 22

==============================================================================
Table of Contents *render-markdown-table-of-contents*
Expand Down Expand Up @@ -40,6 +40,7 @@ Plugin to improve viewing Markdown files in Neovim
- Updates table borders with better border characters, does NOT automatically align
- Basic support for `LaTeX` if `pylatexenc` is installed on system
- Disable rendering when file is larger than provided value
- Support for callouts <https://github.com/orgs/community/discussions/16925>


==============================================================================
Expand Down Expand Up @@ -132,6 +133,8 @@ modified by the user.
-- Capture groups that get pulled from inline markdown
inline_query = [[
(code_span) @code

(shortcut_link) @callout
]],
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
-- Only intended to be used for plugin development / debugging
Expand All @@ -155,6 +158,14 @@ modified by the user.
},
-- Character that will replace the > at the start of block quotes
quote = '┃',
-- Symbol / text to use for different callouts
callout = {
note = ' Note',
tip = ' Tip',
important = '󰅾 Important',
warning = ' Warning',
caution = '󰳦 Caution',
},
-- See :h 'conceallevel' for more information about meaning of values
conceal = {
-- conceallevel used for buffer when not being rendered, get user setting
Expand Down Expand Up @@ -204,6 +215,14 @@ modified by the user.
latex = '@markup.math',
-- Quote character in a block quote
quote = '@markup.quote',
-- Highlights to use for different callouts
callout = {
note = 'DiagnosticInfo',
tip = 'DiagnosticOk',
important = 'DiagnosticHint',
warning = 'DiagnosticWarn',
caution = 'DiagnosticError',
},
},
})
<
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
init := "tests/minimal.lua"
default_zoom := "2"
default_zoom := "-2"

test:
nvim --headless --noplugin -u {{init}} \
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/handler/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local logger = require('render-markdown.logger')
local state = require('render-markdown.state')

---@class render.md.Cache
---@field expressions table<string,string[]>
---@field expressions table<string, string[]>

---@type render.md.Cache
local cache = {
Expand Down
19 changes: 19 additions & 0 deletions lua/render-markdown/handler/markdown_inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ M.render = function(namespace, root, buf)
local highlights = state.config.highlights
for id, node in state.inline_query:iter_captures(root, buf) do
local capture = state.inline_query.captures[id]
local value = vim.treesitter.get_node_text(node, buf)
local start_row, start_col, end_row, end_col = node:range()
logger.debug_node(capture, node, buf)

Expand All @@ -19,6 +20,24 @@ M.render = function(namespace, root, buf)
end_col = end_col,
hl_group = highlights.code,
})
elseif capture == 'callout' then
local value_to_key = {
['[!NOTE]'] = 'note',
['[!TIP]'] = 'tip',
['[!IMPORTANT]'] = 'important',
['[!WARNING]'] = 'warning',
['[!CAUTION]'] = 'caution',
}
local key = value_to_key[value]
if key ~= nil then
local virt_text = { state.config.callout[key], highlights.callout[key] }
vim.api.nvim_buf_set_extmark(buf, namespace, start_row, start_col, {
end_row = end_row,
end_col = end_col,
virt_text = { virt_text },
virt_text_pos = 'overlay',
})
end
else
-- Should only get here if user provides custom capture, currently unhandled
logger.error('Unhandled inline capture: ' .. capture)
Expand Down
25 changes: 25 additions & 0 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ local util = require('render-markdown.util')

local M = {}

---@class render.md.UserCallout
---@field public note? string
---@field public tip? string
---@field public important? string
---@field public warning? string
---@field public caution? string

---@class render.md.UserTableHighlights
---@field public head? string
---@field public row? string
Expand All @@ -25,6 +32,7 @@ local M = {}
---@field public table? render.md.UserTableHighlights
---@field public latex? string
---@field public quote? string
---@field public callout? render.md.UserCallout

---@class render.md.UserConceal
---@field public default? integer
Expand All @@ -47,6 +55,7 @@ local M = {}
---@field public bullets? string[]
---@field public checkbox? render.md.UserCheckbox
---@field public quote? string
---@field public callout? render.md.UserCallout
---@field public conceal? render.md.UserConceal
---@field public table_style? 'full'|'normal'|'none'
---@field public highlights? render.md.UserHighlights
Expand Down Expand Up @@ -90,6 +99,8 @@ function M.setup(opts)
]],
inline_query = [[
(code_span) @code
(shortcut_link) @callout
]],
log_level = 'error',
file_types = { 'markdown' },
Expand All @@ -102,6 +113,13 @@ function M.setup(opts)
checked = '',
},
quote = '',
callout = {
note = ' Note',
tip = ' Tip',
important = '󰅾 Important',
warning = ' Warning',
caution = '󰳦 Caution',
},
conceal = {
default = vim.opt.conceallevel:get(),
rendered = 3,
Expand Down Expand Up @@ -132,6 +150,13 @@ function M.setup(opts)
},
latex = '@markup.math',
quote = '@markup.quote',
callout = {
note = 'DiagnosticInfo',
tip = 'DiagnosticOk',
important = 'DiagnosticHint',
warning = 'DiagnosticWarn',
caution = 'DiagnosticError',
},
},
}
state.config = vim.tbl_deep_extend('force', default_config, opts or {})
Expand Down
9 changes: 9 additions & 0 deletions lua/render-markdown/state.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
---@class render.md.Callout
---@field public note string
---@field public tip string
---@field public important string
---@field public warning string
---@field public caution string

---@class render.md.TableHighlights
---@field public head string
---@field public row string
Expand All @@ -19,6 +26,7 @@
---@field public table render.md.TableHighlights
---@field public latex string
---@field public quote string
---@field public callout render.md.Callout

---@class render.md.Conceal
---@field public default integer
Expand All @@ -41,6 +49,7 @@
---@field public bullets string[]
---@field public checkbox render.md.Checkbox
---@field public quote string
---@field public callout render.md.Callout
---@field public conceal render.md.Conceal
---@field public table_style 'full'|'normal'|'none'
---@field public highlights render.md.Highlights
Expand Down
Loading

0 comments on commit 43bbefd

Please sign in to comment.