render-markdown.nvim
Improve viewing Markdown in Neovim
Features
- Contained: runs entirely inside Neovim with no external windows
- Configurable: all components, padding, icons, and colors can be modified
- File type agnostic: can render
markdown injected into any file
- Automatically runs on lazy load file types defined in
lazy.nvim ft
- Injections: can directly manipulate treesitter to add logical
markdown sections
- Modal rendering: changes between
rendered and raw view based on mode
- Anti-conceal: hides virtual text added by this plugin on cursor line
- Window options: changes option values between
rendered and raw view
- Large files: only renders visible range, can be entirely disabled based on size
- Custom rendering: provides extension point where user can add anything
- Renders the following
markdown components out of the box:
- Headings: icon, color, border, padding [^1], width
- Code blocks: background, language icon [^1] [^2], border, padding [^1], width
- Code inline: background
- Horizontal breaks: icon, color, width
- List bullets: icon, color, padding [^1]
- Checkboxes: icon, color, user defined states
- Block quotes: icon, color, line breaks [^1]
- Callouts: icon, color, user defined values, Github & Obsidian defaults
- Tables: border, color, alignment indicator, auto align cells [^1]
- Links [^1]: icon, color, user defined destinations
- Latex blocks [^3]: renders formulas
- Org indent mode [^1]: per level padding
[^1]: Requires neovim >= 0.10.0
[^2]: Requires icon provider, mini.icons or nvim-web-devicons
[^3]: Requires latex parser and pylatexenc
Requirements
- Neovim
>= 0.9.0 (minimum) >= 0.10.0 (recommended)
- Nerd font symbols: more details
- treesitter parsers:
- markdown & markdown_inline:
Used to parse
markdown files
- html (Optional):
Used to conceal
HTML comments
- latex (Optional):
Used to get
latex blocks from markdown files
- yaml (Optional):
Used to render elements in
frontmatter metadata
- Icon provider plugin (Optional): Used for icon above code blocks
- System dependencies:
- libtexprintf (Optional):
Used to transform
latex strings to appropriate unicode using utftex
- pylatexenc (Optional):
Used to transform
latex strings to appropriate unicode using latex2text
Install
vim.pack
vim.pack.add({
'https://github.com/nvim-treesitter/nvim-treesitter',
'https://github.com/nvim-mini/mini.nvim', -- if you use the mini.nvim suite
-- 'https://github.com/nvim-mini/mini.icons', -- if you use standalone mini plugins
-- 'https://github.com/nvim-tree/nvim-web-devicons', -- if you prefer nvim-web-devicons
'https://github.com/MeanderingProgrammer/render-markdown.nvim',
})
require('render-markdown').setup({}) -- only mandatory if you want to set custom options
lazy.nvim
{
'MeanderingProgrammer/render-markdown.nvim',
dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-mini/mini.nvim' }, -- if you use the mini.nvim suite
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-mini/mini.icons' }, -- if you use standalone mini plugins
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' }, -- if you prefer nvim-web-devicons
---@module 'render-markdown'
---@type render.md.UserConfig
opts = {},
}
rocks.nvim
This plugin is available on LuaRocks
:Rocks install render-markdown.nvim
packer.nvim
use({
'MeanderingProgrammer/render-markdown.nvim',
after = { 'nvim-treesitter' },
requires = { 'nvim-mini/mini.nvim', opt = true }, -- if you use the mini.nvim suite
-- requires = { 'nvim-mini/mini.icons', opt = true }, -- if you use standalone mini plugins
-- requires = { 'nvim-tree/nvim-web-devicons', opt = true }, -- if you prefer nvim-web-devicons
config = function()
require('render-markdown').setup({})
end,
})
Commands
| Command |
Lua Function |
Description |
:RenderMarkdown |
require('render-markdown').enable() |
Alias for enable |
:RenderMarkdown enable |
require('render-markdown').enable() |
Alias for set(true) |
:RenderMarkdown buf_enable |
require('render-markdown').buf_enable() |
Alias for set_buf(true) |
:RenderMarkdown disable |
require('render-markdown').disable() |
Alias for set(false) |
:RenderMarkdown buf_disable |
require('render-markdown').buf_disable() |
Alias for set_buf(false) |
:RenderMarkdown toggle |
require('render-markdown').toggle() |
Alias for set() |
:RenderMarkdown buf_toggle |
require('render-markdown').buf_toggle() |
Alias for set_buf() |
:RenderMarkdown get |
require('render-markdown').get() |
Return current state |
:RenderMarkdown set bool? |
require('render-markdown').set(bool?) |
Sets state, nil to toggle |
:RenderMarkdown set_buf bool? |
require('render-markdown').set_buf(bool?) |
Sets state for current buffer, nil to toggle |
:RenderMarkdown preview |
require('render-markdown').preview() |
Show rendered buffer to the side |
:RenderMarkdown log |
require('render-markdown').log() |
Opens the log file for this plugin |
:RenderMarkdown expand |
require('render-markdown').expand() |
Increase anti-conceal margin above and below by 1 |
:RenderMarkdown contract |
require('render-markdown').contract() |
Decrease anti-conceal margin above and below by 1 |
:RenderMarkdown debug |
require('render-markdown').debug() |
Prints information about marks on current line |
:RenderMarkdown config |
require('render-markdown').config() |
Prints difference between config and default |
Completions
This plugin provides completions for both checkboxes and callouts provided you follow
the relevant setup.
in-process lsp
The recommended way of getting completions from this plugin. Only requires being
enabled with no additional configuration, assuming you have general LSP completions.
Works automatically with nvim-cmp and blink.cmp.
require('render-markdown').setup({
completions = { lsp = { enabled = true } },
})
coq_nvim
require('render-markdown').setup({
completions = { coq = { enabled = true } },
})
Setup
Checkout the Wiki
for examples and images associated with different configuration options.
The full default configuration is provided below for reference.
Any part of it can be modified however for many fields this does not make much sense.
Some of the more useful fields are discussed further down.
Default Configuration
require('render-markdown').setup({
-- Whether markdown should be rendered by default.
enabled = true,
-- Vim modes that will show a rendered view of the markdown file, :h mode(), for all enabled
-- components. Individual components can be enabled for other modes. Remaining modes will be
-- unaffected by this plugin.
render_modes = { 'n', 'c', 't' },
-- Milliseconds that must pass before updating marks, updates occur.
-- within the context of the visible window, not the entire buffer.
debounce = 100,
-- Pre configured settings that will attempt to mimic various target user experiences.
-- User provided settings will take precedence.
-- | obsidian | mimic Obsidian UI |
-- | lazy | will attempt to stay up to date with LazyVim configuration |
-- | none | does nothing |
preset = 'none',
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'.
-- Only intended to be used for plugin development / debugging.
log_level = 'error',
-- Print runtime of main update method.
-- Only intended to be used for plugin development / debugging.
log_runtime = false,
-- Filetypes this plugin will run on.
file_types = { 'markdown' },
-- Maximum file size (in MB) that this plugin will attempt to render.
-- File larger than this will effectively be ignored.
max_file_size = 10.0,
-- Takes buffer as input, if it returns true this plugin will not attach to the buffer.
ignore = function()
return false
end,
-- Whether markdown should be rendered when nested inside markdown, i.e. markdown code block
-- inside markdown file.
nested = true,
-- Additional events that will trigger this plugin's render loop.
change_events = {},
-- Whether the treesitter highlighter should be restarted after this plugin attaches to its
-- first buffer for the first time. May be necessary if this plugin is lazy loaded to clear
-- highlights that have been dynamically disabled.
restart_highlighter = false,
injections = {
-- Out of the box language injections for known filetypes that allow markdown to be interpreted
-- in specified locations, see :h treesitter-language-injections.
-- Set enabled to false in order to disable.
gitcommit = {
enabled = true,
query = [[
((message) @injection.content
(#set! injection.combined)
(#set! injection.include-children)
(#set! injection.language "markdown"))
]],
},
},
patterns = {
-- Highlight patterns to disable for filetypes, i.e. lines concealed around code blocks
markdown = {
disable = true,
directives = {
{ id = 17, name = 'conceal_lines' },
{ id = 18, name = 'conceal_lines' },
},
},
},
anti_conceal = {
-- This enables hiding added text on the line the cursor is on.
enabled = true,
-- Modes to disable anti conceal feature.
disabled_modes = false,
-- Number of lines above cursor to show.
above = 0,
-- Number of lines below cursor to show.
below = 0,
-- Which elements to always show, ignoring anti conceal behavior. Values can either be
-- booleans to fix the behavior or string lists representing modes where anti conceal
-- behavior will be ignored. Valid values are:
-- bullet
-- callout
-- check_icon, check_scope
-- code_background, code_border, code_language
-- dash
-- head_background, head_border, head_icon
-- indent
-- latex
-- link
-- quote
-- sign
-- table_border
-- virtual_lines
ignore = {
code_background = true,
indent = true,
sign = true,
virtual_lines = true,
},
},
padding = {
-- Highlight to use when adding whitespace, should blend in with background.
highlight = 'RenderMarkdownPadding',
},
latex = {
-- Turn on / off latex rendering.
enabled = true,
-- Additional modes to render latex.
render_modes = false,
-- Executable used to convert latex formula to rendered unicode.
-- If a list is provided the commands run in order until the first success.
converter = { 'utftex', 'latex2text' },
-- Render inline latex formulas.
inline = true,
-- Render block latex formulas.
block = true,
-- Highlight for latex blocks.
highlight = 'RenderMarkdownMath',
-- Determines where latex formula is rendered relative to block.
-- | above | above latex block
提示:代码过长已自动截断
We use the following definitions when discussing indexing into lists:
- Cycle: Indexed
mod the length.
Example: { 1, 2, 3 } @ 4 = 1.
- Clamp: Indexed normally but larger values use the last value in the list.
Example:
{ 1, 2, 3 } @ 4 = 3.
Headings
Wiki Page
Heading Configuration
require('render-markdown').setup({
heading = {
-- Useful context to have when evaluating values.
-- | level | the number of '#' in the heading marker |
-- | sections | for each level how deeply nested the heading is |
-- Turn on / off heading icon & background rendering.
enabled = true,
-- Additional modes to render headings.
render_modes = false,
-- Turn on / off atx heading rendering.
atx = true,
-- Turn on / off setext heading rendering.
setext = true,
-- Turn on / off sign column related rendering.
sign = true,
-- Replaces '#+' of 'atx_h._marker'.
-- Output is evaluated depending on the type.
-- | function | `value(context)` |
-- | string[] | `cycle(value, context.level)` |
icons = { ' ', ' ', ' ', ' ', ' ', ' ' },
-- Determines how icons fill the available space.
-- | eol | '#'s are concealed and icon is placed at right most column |
-- | right | '#'s are concealed and icon is appended to right side |
-- | inline | '#'s are concealed and icon is inlined on left side |
-- | overlay | icon is left padded with spaces and overlayed hiding all '#' |
position = 'overlay',
-- Added to the sign column if enabled.
-- Output is evaluated by `cycle(value, context.level)`.
signs = { ' ' },
-- Width of the heading background.
-- | block | width of the heading text |
-- | full | full width of the window |
-- Can also be a list of the above values evaluated by `clamp(value, context.level)`.
width = 'full',
-- Amount of margin to add to the left of headings.
-- Margin available space is computed after accounting for padding.
-- If a float < 1 is provided it is treated as a percentage of available window space.
-- Can also be a list of numbers evaluated by `clamp(value, context.level)`.
left_margin = 0,
-- Amount of padding to add to the left of headings.
-- Output is evaluated using the same logic as 'left_margin'.
left_pad = 0,
-- Amount of padding to add to the right of headings when width is 'block'.
-- Output is evaluated using the same logic as 'left_margin'.
right_pad = 0,
-- Minimum width to use for headings when width is 'block'.
-- Can also be a list of integers evaluated by `clamp(value, context.level)`.
min_width = 0,
-- Determines if a border is added above and below headings.
-- Can also be a list of booleans evaluated by `clamp(value, context.level)`.
border = false,
-- Always use virtual lines for heading borders instead of attempting to use empty lines.
border_virtual = false,
-- Highlight the start of the border using the foreground highlight.
border_prefix = false,
-- Used above heading for border.
above = '▄',
-- Used below heading for border.
below = '▀',
-- Show underline under setext headings.
show_underline = false,
-- Highlight for the heading icon and extends through the entire line.
-- Output is evaluated by `clamp(value, context.level)`.
backgrounds = {
'RenderMarkdownH1Bg',
'RenderMarkdownH2Bg',
'RenderMarkdownH3Bg',
'RenderMarkdownH4Bg',
'RenderMarkdownH5Bg',
'RenderMarkdownH6Bg',
},
-- Highlight for the heading and sign icons.
-- Output is evaluated using the same logic as 'backgrounds'.
foregrounds = {
'RenderMarkdownH1',
'RenderMarkdownH2',
'RenderMarkdownH3',
'RenderMarkdownH4',
'RenderMarkdownH5',
'RenderMarkdownH6',
},
-- Define custom heading patterns which allow you to override various properties based on
-- the contents of a heading.
-- The key is for healthcheck and to allow users to change its values, value type below.
-- | pattern | matched against the heading text @see :h lua-patterns |
-- | icon | optional override for the icon |
-- | background | optional override for the background |
-- | foreground | optional override for the foreground |
custom = {},
},
})
Paragraphs
Wiki Page
Paragraph Configuration
require('render-markdown').setup({
paragraph = {
-- Useful context to have when evaluating values.
-- | text | text value of the node |
-- Turn on / off paragraph rendering.
enabled = true,
-- Additional modes to render paragraphs.
render_modes = false,
-- Amount of margin to add to the left of paragraphs.
-- If a float < 1 is provided it is treated as a percentage of available window space.
-- Output is evaluated depending on the type.
-- | function | `value(context)` |
-- | number | `value` |
left_margin = 0,
-- Amount of padding to add to the first line of each paragraph.
-- Output is evaluated using the same logic as 'left_margin'.
indent = 0,
-- Minimum width to use for paragraphs.
min_width = 0,
},
})
Code Blocks
Wiki Page
Code Block Configuration
require('render-markdown').setup({
code = {
-- Turn on / off code block & inline code rendering.
enabled = true,
-- Additional modes to render code blocks.
render_modes = false,
-- Turn on / off sign column related rendering.
sign = true,
-- Whether to conceal nodes at the top and bottom of code blocks.
conceal_delimiters = true,
-- Turn on / off language heading related rendering.
language = true,
-- Determines where language icon is rendered.
-- | center | center of code block |
-- | right | right of code block |
-- | left | left of code block |
position = 'left',
-- Whether to include the language icon above code blocks.
language_icon = true,
-- Whether to include the language name above code blocks.
language_name = true,
-- Whether to include the language info above code blocks.
language_info = true,
-- Amount of padding to add around the language.
-- If a float < 1 is provided it is treated as a percentage of available window space.
language_pad = 0,
-- A list of language names for which rendering will be disabled.
disable = {},
-- A list of language names for which background highlighting will be disabled.
-- Likely because that language has background highlights itself.
-- Use a boolean to make behavior apply to all languages.
-- Borders above & below blocks will continue to be rendered.
disable_background = { 'diff' },
-- Number of lines from start/end to skip rendering background.
background_inset = 1,
-- Width of the code block background.
-- | block | width of the code block |
-- | full | full width of the window |
width = 'full',
-- Amount of margin to add to the left of code blocks.
-- If a float < 1 is provided it is treated as a percentage of available window space.
-- Margin available space is computed after accounting for padding.
left_margin = 0,
-- Amount of padding to add to the left of code blocks.
-- If a float < 1 is provided it is treated as a percentage of available window space.
left_pad = 0,
-- Amount of padding to add to the right of code blocks when width is 'block'.
-- If a float < 1 is provided it is treated as a percentage of available window space.
right_pad = 0,
-- Minimum width to use for code blocks when width is 'block'.
min_width = 0,
-- Determines how the top / bottom of code block are rendered.
-- | none | do not render a border |
-- | thick | use the same highlight as the code body |
-- | thin | when lines are empty overlay the above & below icons |
-- | hide | conceal lines unless language name or icon is added |
border = 'hide',
-- Used above code blocks to fill remaining space around language.
language_border = '█',
-- Added to the left of language.
language_left = '',
-- Added to the right of language.
language_right = '',
-- Used above code blocks for thin border.
above = '▄',
-- Used below code blocks for thin border.
below = '▀',
-- Turn on / off inline code related rendering.
inline = true,
-- Icon to add to the left of inline code.
inline_left = '',
-- Icon to add to the right of inline code.
inline_right = '',
-- Padding to add to the left & right of inline code.
inline_pad = 0,
-- Priority to assign to code background highlight.
priority = 140,
-- Highlight for code blocks.
highlight = 'RenderMarkdownCode',
-- Highlight for code info section, after the language.
highlight_info = 'RenderMarkdownCodeInfo',
-- Highlight for language, overrides icon provider value.
highlight_language = nil,
-- Highlight for border, use false to add no highlight.
highlight_border = 'RenderMarkdownCodeBorder',
-- Highlight for language, used if icon provider does not have a value.
highlight_fallback = 'RenderMarkdownCodeFallback',
-- Highlight for inline code.
highlight_inline = 'RenderMarkdownCodeInline',
-- Highlight for inline code left icon, default to reverse of highlight_inline.
highlight_inline_left = nil,
-- Highlight for inline code right icon, default to reverse of highlight_inline.
highlight_inline_right = nil,
-- Determines how code blocks & inline code are rendered.
-- | none | { enabled = false } |
-- | normal | { language = false } |
-- | language | { disable_background = true, inline = false } |
提示:代码过长已自动截断
Dashed Line
Wiki Page
Dashed Line Configuration
require('render-markdown').setup({
dash = {
-- Useful context to have when evaluating values.
-- | width | width of the current window |
-- Turn on / off thematic break rendering.
enabled = true,
-- Additional modes to render dash.
render_modes = false,
-- Replaces '---'|'***'|'___'|'* * *' of 'thematic_break'.
-- The icon gets repeated across the window's width.
icon = '─',
-- Width of the generated line.
-- If a float < 1 is provided it is treated as a percentage of available window space.
-- Output is evaluated depending on the type.
-- | function | `value(context)` |
-- | number | `value` |
-- | full | width of the window |
width = 'full',
-- Amount of margin to add to the left of dash.
-- If a float < 1 is provided it is treated as a percentage of available window space.
left_margin = 0,
-- Priority to assign to dash.
priority = nil,
-- Highlight for the whole line generated from the icon.
highlight = 'RenderMarkdownDash',
},
})
List Bullets
Wiki Page
Bullet Point Configuration
require('render-markdown').setup({
bullet = {
-- Useful context to have when evaluating values.
-- | level | how deeply nested the list is, 1-indexed |
-- | index | how far down the item is at that level, 1-indexed |
-- | value | text value of the marker node |
-- Turn on / off list bullet rendering
enabled = true,
-- Additional modes to render list bullets
render_modes = false,
-- Replaces '-'|'+'|'*' of 'list_item'.
-- If the item is a 'checkbox' a conceal is used to hide the bullet instead.
-- Output is evaluated depending on the type.
-- | function | `value(context)` |
-- | string | `value` |
-- | string[] | `cycle(value, context.level)` |
-- | string[][] | `clamp(cycle(value, context.level), context.index)` |
icons = { '●', '○', '◆', '◇' },
-- Replaces 'n.'|'n)' of 'list_item'.
-- Output is evaluated using the same logic as 'icons'.
ordered_icons = function(ctx)
local value = vim.trim(ctx.value)
local index = tonumber(value:sub(1, #value - 1)) or 0
return ('%d.'):format(index > 1 and index or ctx.index)
end,
-- Padding to add to the left of bullet point.
-- Output is evaluated depending on the type.
-- | function | `value(context)` |
-- | integer | `value` |
left_pad = 0,
-- Padding to add to the right of bullet point.
-- Output is evaluated using the same logic as 'left_pad'.
right_pad = 0,
-- Highlight for the bullet icon.
-- Output is evaluated using the same logic as 'icons'.
highlight = 'RenderMarkdownBullet',
-- Highlight for item associated with the bullet point.
-- Output is evaluated using the same logic as 'icons'.
scope_highlight = {},
-- Priority to assign to scope highlight.
scope_priority = nil,
},
})
Checkbo
README 内容过长,已截取前 64 KB。完整内容请前往 GitHub 仓库查看。
评论