From 75d9cadbde763c496cb272fd182d59ccf2c6696c Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 30 Nov 2026 11:26:37 -0800 Subject: [PATCH] Highlight JavaScript and CSS in native Slim filters --- .../lang-haml-slim/README.md | 3 +- .../codemirror_ml_lang_haml_slim.mli | 4 +-- .../logseq-native-modes/lang-haml-slim/dune | 2 ++ .../lang-haml-slim/slim.ml | 4 ++- .../lang-haml-slim/template.ml | 29 ++++++++++++++----- .../lang-haml-slim/template.mli | 2 +- .../test/template_highlight_test.ml | 7 ++++- 7 files changed, 37 insertions(+), 14 deletions(-) diff --git a/integrations/logseq-native-modes/lang-haml-slim/README.md b/integrations/logseq-native-modes/lang-haml-slim/README.md index c56e090..f8167c5 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/README.md +++ b/integrations/logseq-native-modes/lang-haml-slim/README.md @@ -24,7 +24,8 @@ 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. -- Filters other than Ruby and HTML or HAML's preserve filter are plain text. +- Slim's JavaScript and CSS filters mount the corresponding native grammars. + 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 the value. 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 ec90897..7bef919 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 @@ -17,5 +17,5 @@ val slim : ?base:Codemirror_ml_language.Language.Support.t -> unit -> Codemirror_ml_language.Language.Support.t -(** Highlight Slim and parse embedded markup using the base (HTML by default). -*) +(** Highlight Slim and its JavaScript and CSS filters. Embedded markup uses the + base language (HTML by default). *) diff --git a/integrations/logseq-native-modes/lang-haml-slim/dune b/integrations/logseq-native-modes/lang-haml-slim/dune index 0118582..f4ed06f 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/dune +++ b/integrations/logseq-native-modes/lang-haml-slim/dune @@ -4,6 +4,8 @@ (modes native) (libraries codemirror-ml.lang-html + codemirror-ml.lang-css + codemirror-ml.lang-javascript 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 692fd1c..505d3e3 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/slim.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/slim.ml @@ -10,7 +10,9 @@ let filter line = match name line ~offset:0 ~length:size with | "ruby" -> Some (size, Ruby) | "html" -> Some (size, Html) - | "javascript" | "css" | "sass" | "scss" | "less" | "styl" | "coffee" + | "javascript" -> Some (size, Javascript) + | "css" -> Some (size, Css) + | "sass" | "scss" | "less" | "styl" | "coffee" | "asciidoc" | "markdown" | "textile" | "creole" | "wiki" | "mediawiki" | "rdoc" | "builder" | "nokogiri" | "erb" | "plain" -> Some (size, Text) diff --git a/integrations/logseq-native-modes/lang-haml-slim/template.ml b/integrations/logseq-native-modes/lang-haml-slim/template.ml index 9511d47..53bb3f4 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/template.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/template.ml @@ -30,6 +30,8 @@ let comment_region = node "CommentRegion" 8 [] let text_region = node "TextRegion" 9 [] 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 ruby_types = let open Highlight.Tags in @@ -51,7 +53,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 +type body = Comments | Text | Html | Ruby | Javascript | Css 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 } @@ -277,6 +279,8 @@ let region_type = function | Text -> text_region | Html -> html_region | Ruby -> ruby_region + | Javascript -> javascript_region + | Css -> css_region let column line = let rec columns i width = @@ -474,6 +478,15 @@ let language ?(after_group = fun _ -> Finish) ~name ~comment rule = let default_html = lazy (Codemirror_ml_lang_html.html ()) +let javascript_parser = + Language.Lr_language.language + Codemirror_ml_lang_javascript.javascript_language + |> Language.parser + +let css_parser = + Language.Lr_language.language Codemirror_ml_lang_css.css_language + |> Language.parser + let with_html template base = let base = match base with Some base -> base | None -> Lazy.force default_html @@ -484,13 +497,13 @@ let with_html template base = in let wrap = Mix.parse_mixed (fun node _ -> - if Node_type.is_top (Syntax_node.node_type node) then - Some - { - Mix.parser; - overlay = Some (Mix.Predicate overlay); - bracketed = false; - } + let type_ = Syntax_node.node_type node in + if Node_type.is_top type_ then + Some { Mix.parser; overlay = Some (Mix.Predicate overlay); bracketed = false } + else if type_ == javascript_region then + 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 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 3d8e80a..cd6be5b 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 +type body = Comments | Text | Html | Ruby | Javascript | Css type action = Finish | Indented of body | Following_text of int | Ruby_line val peek : ?offset:int -> line -> char -> bool diff --git a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml index d21f674..153651e 100644 --- a/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml +++ b/integrations/logseq-native-modes/lang-haml-slim/test/template_highlight_test.ml @@ -119,4 +119,9 @@ let () = (43, 53, "attribute"); (54, 63, "string"); (72, 75, "property"); (105, 107, "tag") ], [ (92, 104) ] ); - ] + ]; + check "Slim JavaScript and CSS filters" (Provider.slim ()) + ("javascript:\r\n const icon = \"😀\";\r\n\r\n const count = 42;\r\n" + ^ "css:\r\n .icon { color: red; }\r\np done\r\n") + [ (15, 20, "keyword"); (28, 32, "string"); (53, 55, "number"); + (74, 79, "property"); (89, 90, "tag"); (92, 94, "tag") ]