all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] feat: add markdown-ts-mode
@ 2024-04-20  3:23 Rahul Martim Juliato
  2024-04-20  6:44 ` Eli Zaretskii
  0 siblings, 1 reply; 7+ messages in thread
From: Rahul Martim Juliato @ 2024-04-20  3:23 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1018 bytes --]


Hello there!


I've been using Emacs without any extra packages as an educational
experiment after years of package hording.


One of the few things I've been missing is a way of displaying some sort
of syntax highlight for markdown documents.


It feels a bit frustrating opening a README.md file with a single face
color, since Emacs from the box can handle so much.  I am sure others
might have shared this feeling before.


This patch is a modified version of a package I've recently published on
MELPA, screenshots are available here:
https://github.com/LionyxML/markdown-ts-mode


It is a very basic mode that provides syntax highlight and imenu support
using treesitter grammar from
https://github.com/tree-sitter-grammars/tree-sitter-markdown


The idea here is to provide minimal support to Emacs and continue
building up features in the future.


It is the first time I contribute to Emacs devel, so please let me know
if I did something wrong or anything is not at the expected standards.


----- 
Rahul



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-feat-add-markdown-ts-mode.patch --]
[-- Type: text/x-diff, Size: 5071 bytes --]

From 364f61b03d601d2cb3aeb1687da2d1b2a232474c Mon Sep 17 00:00:00 2001
From: Rahul Martim Juliato <rahul.juliato@gmail.com>
Date: Fri, 19 Apr 2024 23:21:20 -0300
Subject: [PATCH] feat: add markdown-ts-mode

---
 etc/NEWS                           |   5 ++
 lisp/progmodes/markdown-ts-mode.el | 106 +++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+)
 create mode 100644 lisp/progmodes/markdown-ts-mode.el

diff --git a/etc/NEWS b/etc/NEWS
index 8ad1e78ca60..ea0640337fb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1688,6 +1688,11 @@ A major mode based on the tree-sitter library for editing Elixir files.
 *** New major mode 'lua-ts-mode'.
 A major mode based on the tree-sitter library for editing Lua files.
 
+---
+*** New major mode 'markdown-ts-mode'.
+A major mode based on the tree-sitter library for editing Markdown files.
+
+
 ** Minibuffer and Completions
 
 +++
diff --git a/lisp/progmodes/markdown-ts-mode.el b/lisp/progmodes/markdown-ts-mode.el
new file mode 100644
index 00000000000..a20d754f83e
--- /dev/null
+++ b/lisp/progmodes/markdown-ts-mode.el
@@ -0,0 +1,106 @@
+;;; markdown-ts-mode.el --- tree sitter support for Markdown  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024 Free Software Foundation, Inc.
+
+;; Author     : Rahul Martim Juliato <rahul.juliato@gmail.com>
+;; Maintainer : Rahul Martim Juliato <rahul.juliato@gmail.com>
+;; Created    : April 2024
+;; Keywords   : markdown md languages tree-sitter
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+
+;;; Code:
+
+(require 'treesit)
+(require 'subr-x)
+
+(declare-function treesit-node-parent "treesit.c")
+(declare-function treesit-node-type "treesit.c")
+(declare-function treesit-parser-create "treesit.c")
+
+(defvar markdown-ts--treesit-settings
+  (treesit-font-lock-rules
+   :language 'markdown
+   :override t
+   :feature 'delimiter
+   '([ "[" "]" "(" ")" ] @shadow)
+
+   :language 'markdown
+   :feature 'paragraph
+   '([((atx_heading) @font-lock-keyword-face)
+      ((block_quote_marker) @font-lock-string-face)
+      ((code_span) @font-lock-string-face)
+      ((emphasis) @underline)
+      ((image_description) @link)
+      ((indented_code_block) @font-lock-string-face)
+      ((link_destination) @font-lock-string-face)
+      ((setext_heading) @font-lock-keyword-face)
+      ((strong_emphasis) @bold)
+      ((thematic_break) @shadow)
+      (block_quote (block_quote_marker) @font-lock-string-face)
+      (block_quote (paragraph) @font-lock-string-face)
+      (fenced_code_block (code_fence_content) @font-lock-string-face)
+      (fenced_code_block (fenced_code_block_delimiter) @font-lock-doc-face)
+      (inline_link (link_destination) @font-lock-string-face)
+      (inline_link (link_text) @link)
+      (list_item (list_marker_dot) @font-lock-keyword-face)
+      (list_item (list_marker_minus) @font-lock-keyword-face)
+      (list_item (list_marker_plus) @font-lock-keyword-face)
+      (list_item (list_marker_star) @font-lock-keyword-face)
+      (shortcut_link (link_text) @link)
+      ])))
+
+(defun markdown-ts-imenu-node-p (node)
+  "Check if NODE is a valid entry to imenu."
+  (equal (treesit-node-type (treesit-node-parent node))
+         "atx_heading"))
+
+(defun markdown-ts-imenu-name-function (node)
+  "Return an imenu entry if NODE is a valid header."
+  (let ((name (treesit-node-text node)))
+    (if (markdown-ts-imenu-node-p node)
+	(thread-first (treesit-node-parent node)(treesit-node-text))
+      name)))
+
+(defun markdown-ts-setup ()
+  "Setup treesit for `markdown-ts-mode'."
+  (setq-local treesit-font-lock-settings markdown-ts--treesit-settings)
+  (treesit-major-mode-setup))
+
+;;;###autoload
+(define-derived-mode markdown-ts-mode fundamental-mode "markdown[ts]"
+  "Major mode for editing Markdown using tree-sitter grammar."
+  (setq-local font-lock-defaults nil
+	      treesit-font-lock-feature-list '((delimiter)
+					       (paragraph)))
+
+  (setq-local treesit-simple-imenu-settings
+              `(("Headings" markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function)))
+
+  (when (treesit-ready-p 'markdown)
+    (treesit-parser-create 'markdown)
+    (markdown-ts-setup)))
+
+(derived-mode-add-parents 'markdown-ts-mode '(markdown-mode))
+
+(if (treesit-ready-p 'markdown)
+    (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-ts-mode)))
+
+(provide 'markdown-ts-mode)
+;;; markdown-ts-mode.el ends here
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-27 10:18 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-20  3:23 [PATCH] feat: add markdown-ts-mode Rahul Martim Juliato
2024-04-20  6:44 ` Eli Zaretskii
2024-04-20 17:45   ` Jostein Kjønigsen
2024-04-21  5:50     ` Rahul Martim Juliato
2024-04-22  7:02       ` Philip Kaludercic
2024-04-23  1:20         ` Rahul Martim Juliato
2024-04-27 10:18           ` Philip Kaludercic

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.