Skip to main content

knot_editor_host/
lib.rs

1// Copyright 2026 Mark AB (markik)
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Host-side reuse-lexers for the knot editor.
5//!
6//! The portable [`illume`] crate stays lean (jotdown + logos). This crate is
7//! the precision layer: `InjectionLexer` implementations that reuse a parser the
8//! app already ships, registered as overrides on the portable pack. They are
9//! optional precision over the always-present logos floor, so highlighting never
10//! depends on one of these parsers being compiled in.
11//!
12//! Today: Markdown via [`pulldown_cmark`]. To follow, each over its existing host
13//! tokenizer: CSS (`cssparser`), HTML (`html5ever`), precise JS (`boa_parser`),
14//! Turtle/RDF (`oxttl`). [`full_pack`] returns the portable pack plus the reuse
15//! overrides registered here.
16
17use illume::{InjectionLexer, InjectionRegistry, Span, SyntaxKind};
18use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
19
20pub mod editor;
21mod model;
22
23pub use editor::KnotReadout;
24pub use model::{EditOutcome, KnotEditor};
25
26/// The portable pack plus the host reuse-lexers registered in this crate. The host
27/// calls this to get the full editor injection registry; mods register on top.
28pub fn full_pack() -> InjectionRegistry {
29    let mut reg = illume::default_pack();
30    for label in ["markdown", "md", "commonmark"] {
31        reg.register(label, Box::new(MarkdownLexer));
32    }
33    reg
34}
35
36/// CommonMark / Markdown highlighter, reusing [`pulldown_cmark`]'s offset-iter
37/// event stream (the same shape jotdown gives the djot highlighter). Each colored
38/// construct emits one span from its `Start` to its `End`.
39pub struct MarkdownLexer;
40
41/// The highlight class for a pulldown-cmark start tag, or `None` for tags the
42/// editor does not color (paragraphs, lists, tables, …).
43fn tag_kind(tag: &Tag) -> Option<SyntaxKind> {
44    Some(match tag {
45        Tag::Heading { .. } => SyntaxKind::Heading,
46        Tag::Emphasis => SyntaxKind::Emphasis,
47        Tag::Strong => SyntaxKind::Strong,
48        Tag::Strikethrough => SyntaxKind::Strikethrough,
49        Tag::Link { .. } => SyntaxKind::Link,
50        Tag::Image { .. } => SyntaxKind::Image,
51        Tag::CodeBlock(_) => SyntaxKind::CodeBlock,
52        Tag::BlockQuote(_) => SyntaxKind::Blockquote,
53        _ => return None,
54    })
55}
56
57/// Whether an end tag closes a tracked construct (the symmetric counterpart of
58/// [`tag_kind`]; `TagEnd` is a lighter enum than `Tag`).
59fn tag_end_tracked(end: &TagEnd) -> bool {
60    matches!(
61        end,
62        TagEnd::Heading(_)
63            | TagEnd::Emphasis
64            | TagEnd::Strong
65            | TagEnd::Strikethrough
66            | TagEnd::Link
67            | TagEnd::Image
68            | TagEnd::CodeBlock
69            | TagEnd::BlockQuote(_)
70    )
71}
72
73impl InjectionLexer for MarkdownLexer {
74    fn lex(&self, inner: &str) -> Vec<Span> {
75        let mut spans = Vec::new();
76        let mut stack: Vec<(SyntaxKind, usize)> = Vec::new();
77        for (event, range) in Parser::new_ext(inner, Options::all()).into_offset_iter() {
78            match event {
79                Event::Start(tag) => {
80                    if let Some(kind) = tag_kind(&tag) {
81                        stack.push((kind, range.start));
82                    }
83                },
84                Event::End(end) => {
85                    if tag_end_tracked(&end) {
86                        if let Some((kind, start)) = stack.pop() {
87                            spans.push(Span {
88                                range: start..range.end,
89                                kind,
90                            });
91                        }
92                    }
93                },
94                // Inline code spans are atomic (no Start/End pair).
95                Event::Code(_) => spans.push(Span {
96                    range,
97                    kind: SyntaxKind::Verbatim,
98                }),
99                _ => {},
100            }
101        }
102        spans
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn markdown_lexer_colors_structure() {
112        let src = "# H\n\n*em* **st** `code`\n\n> quote\n";
113        let kinds: Vec<_> = MarkdownLexer.lex(src).into_iter().map(|s| s.kind).collect();
114        assert!(kinds.contains(&SyntaxKind::Heading), "{kinds:?}");
115        assert!(kinds.contains(&SyntaxKind::Emphasis), "{kinds:?}");
116        assert!(kinds.contains(&SyntaxKind::Strong), "{kinds:?}");
117        assert!(kinds.contains(&SyntaxKind::Verbatim), "{kinds:?}");
118        assert!(kinds.contains(&SyntaxKind::Blockquote), "{kinds:?}");
119    }
120
121    #[test]
122    fn full_pack_is_the_portable_pack_plus_reuse() {
123        let reg = full_pack();
124        // Portable pack still present.
125        assert!(reg.has("json"));
126        assert!(reg.has("rust"));
127        assert!(reg.has("lua"));
128        // Reuse override added here.
129        assert!(reg.has("markdown"));
130        assert!(reg.has("md"));
131    }
132}