md-ts-mode is a tree-sitter-based major mode for editing Markdown.
It is derived from markdown-ts-mode in GNU Emacs 31 and works on
Emacs 29, 30, and 31.
- Syntax highlighting for headings (atx and setext), emphasis, strong
emphasis, strikethrough (
~~deleted~~), code spans, links (inline, full reference, collapsed reference, shortcut), images, block quotes, list markers, and indented code blocks. - Task list markers (
[ ],[x]) rendered as checkbox glyphs (☐, ☑). - Thematic breaks (
---,***,___) rendered as horizontal rules. - Pipe tables with bold headers, subdued delimiter rows, and inline markup (emphasis, code, links) fontified inside cells.
- HTML blocks highlighted with
font-lock-doc-face. - Fenced code blocks fontified by the corresponding language grammar when available.
- YAML and TOML front matter fontified when their grammars are available.
- Hide-markup mode:
md-ts-toggle-hide-markuphides delimiters (#,*,~, brackets, URLs) so only content is visible. - Imenu and
outline-minor-modeintegration via headings. - Custom faces:
md-ts-heading-1throughmd-ts-heading-6,md-ts-setext-heading,md-ts-delimiter,md-ts-list-marker,md-ts-block-quote,md-ts-strikethrough,md-ts-language-keyword, andmd-ts-task-list-marker.
- GNU Emacs 29.1 or later, built with tree-sitter support.
- Tree-sitter grammars:
markdownandmarkdown-inlinefrom tree-sitter-markdown (version 0.4.1 or compatible).
With package-vc (Emacs 29+):
(package-vc-install "https://github.com/dnouri/md-ts-mode")Or clone the repository and add it to load-path:
(add-to-list 'load-path "/path/to/md-ts-mode/")
(require 'md-ts-mode)The package registers itself in auto-mode-alist for .md files
and in treesit-major-mode-remap-alist as a replacement for
markdown-mode. No further configuration is needed.
The package registers grammar recipes in
treesit-language-source-alist, so after loading:
M-x treesit-install-language-grammar RET markdown RET M-x treesit-install-language-grammar RET markdown-inline RET
If only part of a file is highlighted, one of the grammars is probably missing.
Fenced code blocks with a language tag (e.g., ```python) are
parsed and fontified by the corresponding language grammar.
Install the grammar for any language you want highlighted:
M-x treesit-install-language-grammar RET python RET
The variable md-ts-code-block-source-mode-map maps language
symbols to their tree-sitter major modes. The mode must be
installed and its grammar available for highlighting to work.
The compatibility shims in md-ts-mode.el backport Emacs 31
tree-sitter range infrastructure to Emacs 29 and 30. This
includes replacements for treesit-range-fn-exclude-children,
treesit-query-range (with RANGE-FN argument),
treesit-range-rules (with :range-fn keyword and function-form
:embed), treesit-update-ranges, and
treesit--update-ranges-local.
On Emacs 29, additional shims provide treesit-node-children,
derived-mode-add-parents, and wrappers for C functions whose
signatures changed in Emacs 30 (treesit-parser-create,
treesit-parser-list).
On Emacs 31 all guards are false and no shims are defined.
Tested on Emacs 29.4, 30.1, and development snapshot (31).
Based on markdown-ts-mode from GNU Emacs 31, originally written
by Rahul Martim Juliato.