From 895074ac97ba8cb52753c524d3c9c41167537cf6 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 2 Dec 2026 00:47:00 -0800 Subject: [PATCH 1/2] Highlight Markdown filters in native Slim --- .../logseq-native-modes/catalog/generate.mjs | 2 +- .../logseq-native-modes/catalog/mode_data.ml | 2 +- .../catalog/test/test_mode_catalog.ml | 26 +++++++++ .../lang-haml-slim/README.md | 4 +- .../codemirror_ml_lang_haml_slim.ml | 2 +- .../codemirror_ml_lang_haml_slim.mli | 6 ++- .../logseq-native-modes/lang-haml-slim/dune | 1 + .../lang-haml-slim/slim.ml | 3 +- .../lang-haml-slim/template.ml | 54 +++++++++++++++---- .../lang-haml-slim/template.mli | 3 +- 10 files changed, 86 insertions(+), 17 deletions(-) diff --git a/integrations/logseq-native-modes/catalog/generate.mjs b/integrations/logseq-native-modes/catalog/generate.mjs index f7f55ec..3973474 100644 --- a/integrations/logseq-native-modes/catalog/generate.mjs +++ b/integrations/logseq-native-modes/catalog/generate.mjs @@ -72,7 +72,7 @@ const native = new Map(Object.entries({ PostgreSQL: "stream Sql.pg_sql", SQL: "modern (Codemirror_ml_lang_sql.sql ())", SQLite: "stream Sql.sqlite", - Slim: "modern (Codemirror_ml_lang_haml_slim.slim ())", + Slim: "modern (Codemirror_ml_lang_haml_slim.slim ~code_languages ())", Tornado: "modern (Codemirror_ml_lang_django_tornado.tornado ())", LaTeX: "stream Stex.stex_math", Twig: "modern (Codemirror_ml_lang_twig.twig ~base:(Codemirror_ml_lang_html.html ()) ())", diff --git a/integrations/logseq-native-modes/catalog/mode_data.ml b/integrations/logseq-native-modes/catalog/mode_data.ml index 4cb448c..c9a699a 100644 --- a/integrations/logseq-native-modes/catalog/mode_data.ml +++ b/integrations/logseq-native-modes/catalog/mode_data.ml @@ -354,7 +354,7 @@ let entries ~code_languages provider = (fun () -> stream Sieve.sieve); provider "Slim" ["slim"] ["text/x-slim"; "application/x-slim"] ["slim"] None - (fun () -> modern (Codemirror_ml_lang_haml_slim.slim ())); + (fun () -> modern (Codemirror_ml_lang_haml_slim.slim ~code_languages ())); provider "Smalltalk" ["smalltalk"] ["text/x-stsrc"] ["st"] None (fun () -> stream Smalltalk.smalltalk); diff --git a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml index 2f5849d..a296e3a 100644 --- a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml +++ b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml @@ -31,6 +31,31 @@ let check_markdown_fences () = ; "markdown", "", "text", [] ]) +let check_slim_markdown_filter () = + let open Highlight.Tags in + let highlighter = + Highlight.Highlighter.of_tags + [ [heading], "heading"; [link], "link"; [quote], "quote" + ; [keyword], "keyword"; [number], "number"; [tag_name], "tag" ] + in + let source = + "section\n markdown:\n # Slim heading\n\n" + ^ " [🚀 Link](https://example.com)\n > Quote\n\n" + ^ " ```js\n const answer = 42;\n ```\n p Back to Slim" + in + let text = Input.Text.of_utf8 source in + let spans = ref [] in + Highlight.highlight_tree (parse "Slim" source) [highlighter] + ~put_style:(fun first last style -> + let chunk = Input.Text.slice text ~from:first ~to_:last in + spans := (style, Input.Text.to_wtf8 chunk) :: !spans); + List.iter + (fun ((style, chunk) as expected) -> + if not (List.mem expected !spans) then + failwith (Printf.sprintf "Missing %s highlight for %S in Slim" style chunk)) + [ "heading", "# Slim heading"; "link", "[🚀 Link](https://example.com)" + ; "quote", "> Quote"; "keyword", "const"; "number", "42"; "tag", "p" ] + let check_lisp_brackets () = let module State = Codemirror_ml_state in let module View = Codemirror_ml_view.Editor_view in @@ -165,4 +190,5 @@ let () = List.iter (fun mode -> ignore (parse mode "\n")) (Logseq_native_modes.names ()); check_markdown_fences (); + check_slim_markdown_filter (); check_lisp_brackets () diff --git a/integrations/logseq-native-modes/lang-haml-slim/README.md b/integrations/logseq-native-modes/lang-haml-slim/README.md index f8167c5..eb44edf 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/README.md +++ b/integrations/logseq-native-modes/lang-haml-slim/README.md @@ -24,7 +24,9 @@ modern language API and syntax-tree highlighter. wrapped and bare attributes, Ruby switches and interpolation, and indented text and Ruby regions. Wrapped attribute lists can span lines in both languages, including HAML's parenthesized attributes. -- Slim's JavaScript and CSS filters mount the corresponding native grammars. +- Slim's JavaScript, CSS, and Markdown filters mount the corresponding native + grammars. Markdown starts at the first content line's indentation, and the + Logseq catalog resolves fenced languages inside it. Other filters beyond Ruby, HTML, and HAML's preserve filter are plain text. Individual Ruby attribute values are line-local. Spaces in a value are supported inside strings and balanced brackets. At the top level a space ends diff --git a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.ml b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.ml index 965eab5..a1ef603 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.ml @@ -1,4 +1,4 @@ let haml_language = Haml.language let haml ?base () = Template.with_html haml_language base let slim_language = Slim.language -let slim ?base () = Template.with_html slim_language base +let slim ?base ?code_languages () = Template.with_html ?code_languages slim_language base diff --git a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli index 7bef919..6ef5b45 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli +++ b/integrations/logseq-native-modes/lang-haml-slim/codemirror_ml_lang_haml_slim.mli @@ -15,7 +15,9 @@ val slim_language : Codemirror_ml_language.Language.t val slim : ?base:Codemirror_ml_language.Language.Support.t -> + ?code_languages:Codemirror_ml_lang_markdown.code_languages -> unit -> Codemirror_ml_language.Language.Support.t -(** Highlight Slim and its JavaScript and CSS filters. Embedded markup uses the - base language (HTML by default). *) +(** Highlight Slim and its JavaScript, CSS, and Markdown filters. Embedded + markup uses the base language (HTML by default). [code_languages] resolves + fenced language names inside Markdown filters. *) diff --git a/integrations/logseq-native-modes/lang-haml-slim/dune b/integrations/logseq-native-modes/lang-haml-slim/dune index f4ed06f..fe3167c 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/dune +++ b/integrations/logseq-native-modes/lang-haml-slim/dune @@ -6,6 +6,7 @@ codemirror-ml.lang-html codemirror-ml.lang-css codemirror-ml.lang-javascript + codemirror-ml.lang-markdown codemirror-ml.language logseq-native-modes.legacy-modes codemirror-ml.lezer_common diff --git a/integrations/logseq-native-modes/lang-haml-slim/slim.ml b/integrations/logseq-native-modes/lang-haml-slim/slim.ml index 505d3e3..991d5bb 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -12,8 +12,9 @@ let filter line = | "html" -> Some (size, Html) | "javascript" -> Some (size, Javascript) | "css" -> Some (size, Css) + | "markdown" -> Some (size, Markdown) | "sass" | "scss" | "less" | "styl" | "coffee" - | "asciidoc" | "markdown" | "textile" | "creole" | "wiki" | "mediawiki" + | "asciidoc" | "textile" | "creole" | "wiki" | "mediawiki" | "rdoc" | "builder" | "nokogiri" | "erb" | "plain" -> Some (size, Text) | _ -> None diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 53bb3f4..5f37a40 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -32,6 +32,8 @@ let html_region = node "HtmlRegion" 10 [] let ruby_region = node "RubyRegion" 11 [] let javascript_region = node "JavaScriptRegion" 12 [] let css_region = node "CSSRegion" 13 [] +let markdown_region = node "MarkdownRegion" 14 [] +let markdown_text = node "MarkdownText" 15 [] let ruby_types = let open Highlight.Tags in @@ -53,7 +55,7 @@ let ruby_types = |> List.mapi (fun i (style, name, tag) -> (style, node name (20 + i) [ tag ])) type mark = Tag | Attribute | Keyword | Marker | Comment -type body = Comments | Text | Html | Ruby | Javascript | Css +type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown type action = Finish | Indented of body | Following_text of int | Ruby_line type ruby = { state : Ruby_mode.state; mutable closes : char list } type ruby_group = { ruby : ruby; closing : char } @@ -268,6 +270,7 @@ let html line = type region = { body : body; min_indent : int; + mutable markdown_indent : int option; from : int; mutable to_ : int; mutable children : (int * Tree.child) list; @@ -281,18 +284,36 @@ let region_type = function | Ruby -> ruby_region | Javascript -> javascript_region | Css -> css_region + | Markdown -> markdown_region + +let next_column line index width = + if at line index '\t' then width + tab_size - (width mod tab_size) + else width + 1 let column line = let rec columns i width = if i = line.cursor then width - else - let step = - if at line i '\t' then tab_size - (width mod tab_size) else 1 - in - columns (i + 1) (width + step) + else columns (i + 1) (next_column line i width) in columns 0 0 +let markdown_line line body ~depth ~blank = + if not blank then ( + let indent = + match body.markdown_indent with + | Some indent -> indent + | None -> + body.markdown_indent <- Some depth; + depth + in + let rec offset i width = + if width >= indent then i + else offset (i + 1) (next_column line i width) + in + line.cursor <- offset 0 0); + (* Keep line breaks in the overlay and omit only the template indentation. *) + emit line markdown_text line.width + let read_line input from limit = let rec find from = if from >= limit then limit @@ -360,13 +381,16 @@ let parser root rule after_group = let depth = column line in let blank = line.cursor = line.limit in (match !region with - | Some body when (not blank) && depth < body.min_indent -> + | Some body + when (not blank) + && depth < Option.value ~default:body.min_indent body.markdown_indent -> close_region () | _ -> ()); let line_node () = Tree.Node (tree line_type line.nodes line.width) in match !region with | Some body -> - (if blank then + (if body.body = Markdown then markdown_line line body ~depth ~blank + else if blank then Option.iter (fun ruby -> ruby_mode.blank_line ruby.state 2) body.ruby @@ -404,6 +428,7 @@ let parser root rule after_group = { body; min_indent; + markdown_indent = None; from = after; to_ = after; children = []; @@ -487,7 +512,7 @@ let css_parser = Language.Lr_language.language Codemirror_ml_lang_css.css_language |> Language.parser -let with_html template base = +let with_html ?code_languages template base = let base = match base with Some base -> base | None -> Lazy.force default_html in @@ -495,6 +520,14 @@ let with_html template base = let overlay node = if Syntax_node.node_type node == html_text then Mix.Include else Mix.Skip in + let markdown_parser = + lazy + (Codemirror_ml_lang_markdown.markdown ?code_languages () + |> Language.Support.language |> Language.parser) + in + let markdown_overlay node = + if Syntax_node.node_type node == markdown_text then Mix.Include else Mix.Skip + in let wrap = Mix.parse_mixed (fun node _ -> let type_ = Syntax_node.node_type node in @@ -504,6 +537,9 @@ let with_html template base = Some { Mix.parser = javascript_parser; overlay = None; bracketed = false } else if type_ == css_region then Some { Mix.parser = css_parser; overlay = None; bracketed = false } + else if type_ == markdown_region then + Some { Mix.parser = Lazy.force markdown_parser; + overlay = Some (Mix.Predicate markdown_overlay); bracketed = false } else None) in let mixed = Parser.with_wrapper (Language.parser template) wrap in diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.mli b/integrations/logseq-native-modes/lang-haml-slim/template.mli index cd6be5b..ae14650 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.mli +++ b/integrations/logseq-native-modes/lang-haml-slim/template.mli @@ -2,7 +2,7 @@ type line type mark = Tag | Attribute | Keyword | Marker | Comment -type body = Comments | Text | Html | Ruby | Javascript | Css +type body = Comments | Text | Html | Ruby | Javascript | Css | Markdown type action = Finish | Indented of body | Following_text of int | Ruby_line val peek : ?offset:int -> line -> char -> bool @@ -30,6 +30,7 @@ val language : Codemirror_ml_language.Language.t val with_html : + ?code_languages:Codemirror_ml_lang_markdown.code_languages -> Codemirror_ml_language.Language.t -> Codemirror_ml_language.Language.Support.t option -> Codemirror_ml_language.Language.Support.t From b26ff0b2fff1a7f1d1217fb5dac7e0aaf455455d Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 2 Dec 2026 01:05:01 -0800 Subject: [PATCH 2/2] Preserve nested Slim filters across Markdown margins --- .../logseq-native-modes/catalog/test/test_mode_catalog.ml | 7 +++++-- .../logseq-native-modes/lang-haml-slim/template.ml | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml index a296e3a..2e2eb9b 100644 --- a/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml +++ b/integrations/logseq-native-modes/catalog/test/test_mode_catalog.ml @@ -41,7 +41,9 @@ let check_slim_markdown_filter () = let source = "section\n markdown:\n # Slim heading\n\n" ^ " [🚀 Link](https://example.com)\n > Quote\n\n" - ^ " ```js\n const answer = 42;\n ```\n p Back to Slim" + ^ " ```js\n const answer = 42;\n ```\n\n" + ^ " ```slim\n javascript:\n throw 99;\n ```\n" + ^ " p Back to Slim" in let text = Input.Text.of_utf8 source in let spans = ref [] in @@ -54,7 +56,8 @@ let check_slim_markdown_filter () = if not (List.mem expected !spans) then failwith (Printf.sprintf "Missing %s highlight for %S in Slim" style chunk)) [ "heading", "# Slim heading"; "link", "[🚀 Link](https://example.com)" - ; "quote", "> Quote"; "keyword", "const"; "number", "42"; "tag", "p" ] + ; "quote", "> Quote"; "keyword", "const"; "number", "42" + ; "keyword", "throw"; "number", "99"; "tag", "p" ] let check_lisp_brackets () = let module State = Codemirror_ml_state in diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 5f37a40..6e76da0 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -271,7 +271,7 @@ type region = { body : body; min_indent : int; mutable markdown_indent : int option; - from : int; + mutable from : int; mutable to_ : int; mutable children : (int * Tree.child) list; ruby : ruby option; @@ -389,6 +389,8 @@ let parser root rule after_group = let line_node () = Tree.Node (tree line_type line.nodes line.width) in match !region with | Some body -> + (* A nested parser starts at the first included source position. *) + if body.children = [] then body.from <- from; (if body.body = Markdown then markdown_line line body ~depth ~blank else if blank then Option.iter @@ -462,10 +464,8 @@ let parser root rule after_group = | None, _ when !cursor >= !ceiling -> done_ () | None, [] -> done_ () | None, range :: tail when !cursor >= range.Range.to_ -> ( + (* Markdown fences can omit a margin between included lines. *) ranges := tail; - close_region (); - pending := None; - group := None; match tail with | next :: _ when next.Range.from < !ceiling -> cursor := next.from;