ferriki 1.2.0

Reusable native Rust syntax highlighter with TextMate grammars
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Inline multi-theme HTML for the private Node render operation.

use crate::render::{escape_attribute, escape_html};
use crate::{
    FontStyle, HighlightThemeToken, HighlightThemeTokenStyle, HighlightTokensWithThemesResult,
    RenderOptions,
};

pub struct MultiThemeRenderOptions {
    pub render: RenderOptions,
    pub default_color: Option<String>,
    pub css_variable_prefix: String,
}

pub fn render_html_with_themes(
    result: &HighlightTokensWithThemesResult,
    options: &MultiThemeRenderOptions,
) -> String {
    let mut html = String::from("<pre class=\"");
    let mut class = String::from("shiki shiki-themes");
    for theme in &result.themes {
        class.push(' ');
        class.push_str(&theme.name);
    }
    html.push_str(&escape_attribute(&class));
    html.push('"');
    if options.render.include_root_style {
        let style = options
            .render
            .root_style
            .as_deref()
            .filter(|style| !style.is_empty());
        let generated;
        let style = match style {
            Some(style) => style,
            None => {
                let foreground = theme_property(result, options, false);
                let background = theme_property(result, options, true);
                generated = if options.default_color.is_some() {
                    format!("background-color:{background};color:{foreground}")
                } else {
                    format!("{foreground};{background}")
                };
                &generated
            }
        };
        html.push_str(" style=\"");
        html.push_str(&escape_attribute(style));
        html.push('"');
    }
    if let Some(tabindex) = &options.render.tabindex {
        html.push_str(" tabindex=\"");
        html.push_str(&escape_attribute(tabindex));
        html.push('"');
    }
    html.push_str("><code>");
    for (index, line) in result.tokens.iter().enumerate() {
        if index > 0 {
            html.push('\n');
        }
        html.push_str("<span class=\"line\">");
        render_line(&mut html, line, result, options);
        html.push_str("</span>");
    }
    html.push_str("</code></pre>");
    html
}

fn render_line(
    html: &mut String,
    line: &[HighlightThemeToken],
    result: &HighlightTokensWithThemesResult,
    options: &MultiThemeRenderOptions,
) {
    let mut carried = None;
    let mut previous_style: Option<String> = None;
    for (index, token) in line.iter().enumerate() {
        // Combined theme tokens have no top-level fontStyle in the JS
        // pipeline: whitespace takes the following token's complete CSS.
        if options.render.merge_whitespaces
            && index + 1 < line.len()
            && !token.content.is_empty()
            && token.content.chars().all(is_js_whitespace)
        {
            carried.get_or_insert(index);
            continue;
        }
        let style = token_style(token, result, options);
        if !options.render.merge_same_style_tokens || previous_style.as_ref() != Some(&style) {
            if previous_style.is_some() {
                html.push_str("</span>");
            }
            html.push_str("<span");
            if !style.is_empty() {
                html.push_str(" style=\"");
                html.push_str(&escape_attribute(&style));
                html.push('"');
            }
            html.push('>');
            previous_style = Some(style);
        }
        let start = carried.take().unwrap_or(index);
        for piece in &line[start..=index] {
            html.push_str(&escape_html(&piece.content));
        }
    }
    if previous_style.is_some() {
        html.push_str("</span>");
    }
}

// ECMAScript \s, rather than Rust's Unicode White_Space (NEL differs and
// ECMAScript additionally accepts the BOM). Match prepareRenderTokens.
fn is_js_whitespace(character: char) -> bool {
    matches!(character, '\u{0009}'..='\u{000d}' | '\u{0020}' | '\u{00a0}' | '\u{1680}' | '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}' | '\u{202f}' | '\u{205f}' | '\u{3000}' | '\u{feff}')
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum Property {
    Color,
    Italic,
    Bold,
    Decoration,
}

impl Property {
    const ALL: [Self; 4] = [Self::Color, Self::Italic, Self::Bold, Self::Decoration];
    fn name(self) -> &'static str {
        match self {
            Self::Color => "color",
            Self::Italic => "font-style",
            Self::Bold => "font-weight",
            Self::Decoration => "text-decoration",
        }
    }
    fn suffix(self) -> &'static str {
        match self {
            Self::Color => "",
            Self::Italic => "-font-style",
            Self::Bold => "-font-weight",
            Self::Decoration => "-text-decoration",
        }
    }
    fn value(self, style: Option<&HighlightThemeTokenStyle>) -> Option<&str> {
        let style = style?;
        let font = style.font_style.unwrap_or_default();
        match self {
            Self::Color => style.color.as_deref().filter(|color| !color.is_empty()),
            Self::Italic => font.contains(FontStyle::ITALIC).then_some("italic"),
            Self::Bold => font.contains(FontStyle::BOLD).then_some("bold"),
            Self::Decoration => match (
                font.contains(FontStyle::UNDERLINE),
                font.contains(FontStyle::STRIKETHROUGH),
            ) {
                (true, true) => Some("underline line-through"),
                (true, false) => Some("underline"),
                (false, true) => Some("line-through"),
                (false, false) => None,
            },
        }
    }
}

fn separator(output: &mut String) {
    if !output.is_empty() {
        output.push(';');
    }
}

fn token_style(
    token: &HighlightThemeToken,
    result: &HighlightTokensWithThemesResult,
    options: &MultiThemeRenderOptions,
) -> String {
    // JS Set preserves the first occurrence of each property across themes.
    // Four slots suffice for the fixed property set, without per-token keys.
    let mut keys = [Property::Color; 4];
    let mut count = 0;
    for theme in &result.themes {
        let style = token.variants.get(&theme.color);
        for key in Property::ALL {
            if key.value(style).is_some() && !keys[..count].contains(&key) {
                keys[count] = key;
                count += 1;
            }
        }
    }
    let mut output = String::new();
    let light_dark = options.default_color.as_deref() == Some("light-dark()");
    for (index, theme) in result.themes.iter().enumerate() {
        for &key in &keys[..count] {
            let value = key
                .value(token.variants.get(&theme.color))
                .unwrap_or("inherit");
            if index == 0 && options.default_color.is_some() && key == Property::Color {
                separator(&mut output);
                output.push_str(key.name());
                output.push(':');
                if light_dark {
                    let light = key.value(token.variants.get("light")).unwrap_or("inherit");
                    let dark = key.value(token.variants.get("dark")).unwrap_or("inherit");
                    output.push_str("light-dark(");
                    output.push_str(light);
                    output.push_str(", ");
                    output.push_str(dark);
                    output.push(')');
                } else {
                    output.push_str(value);
                }
            }
            if index > 0 || options.default_color.is_none() || key != Property::Color || light_dark
            {
                separator(&mut output);
                output.push_str(&options.css_variable_prefix);
                output.push_str(&theme.color);
                output.push_str(key.suffix());
                output.push(':');
                output.push_str(value);
            }
        }
    }
    output
}

fn theme_property(
    result: &HighlightTokensWithThemesResult,
    options: &MultiThemeRenderOptions,
    background: bool,
) -> String {
    let read = |index: usize| {
        let theme = &result.themes[index];
        let value = if background {
            &theme.background
        } else {
            &theme.foreground
        };
        if value.is_empty() {
            "inherit"
        } else {
            value.as_str()
        }
    };
    let mut output = String::new();
    let light_dark = options.default_color.as_deref() == Some("light-dark()");
    for (index, theme) in result.themes.iter().enumerate() {
        if index == 0 && options.default_color.is_some() {
            if light_dark {
                let light = result
                    .themes
                    .iter()
                    .position(|theme| theme.color == "light")
                    .map(read)
                    .unwrap_or("inherit");
                let dark = result
                    .themes
                    .iter()
                    .position(|theme| theme.color == "dark")
                    .map(read)
                    .unwrap_or("inherit");
                output.push_str("light-dark(");
                output.push_str(light);
                output.push_str(", ");
                output.push_str(dark);
                output.push(')');
            } else {
                output.push_str(read(index));
            }
        }
        if index > 0 || options.default_color.is_none() || light_dark {
            separator(&mut output);
            output.push_str(&options.css_variable_prefix);
            output.push_str(&theme.color);
            if background {
                output.push_str("-bg");
            }
            output.push(':');
            output.push_str(read(index));
        }
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::HighlightThemeMetadata;
    use std::collections::BTreeMap;

    fn fixture() -> HighlightTokensWithThemesResult {
        HighlightTokensWithThemesResult {
            themes: vec![
                HighlightThemeMetadata {
                    color: "light".into(),
                    name: "light".into(),
                    foreground: "#111".into(),
                    background: String::new(),
                },
                HighlightThemeMetadata {
                    color: "dark".into(),
                    name: "dark<&\"".into(),
                    foreground: "#eee".into(),
                    background: "#222".into(),
                },
            ],
            tokens: vec![
                vec![HighlightThemeToken {
                    content: "<&>".into(),
                    offset: 0,
                    token_type: None,
                    scope_names: None,
                    variants: BTreeMap::from([
                        (
                            "light".into(),
                            HighlightThemeTokenStyle {
                                color: Some("#123".into()),
                                font_style: Some(FontStyle::UNDERLINE),
                            },
                        ),
                        (
                            "dark".into(),
                            HighlightThemeTokenStyle {
                                color: None,
                                font_style: Some(
                                    FontStyle::ITALIC | FontStyle::BOLD | FontStyle::STRIKETHROUGH,
                                ),
                            },
                        ),
                    ]),
                }],
                vec![],
            ],
        }
    }

    fn options(default_color: Option<&str>) -> MultiThemeRenderOptions {
        MultiThemeRenderOptions {
            render: RenderOptions::default(),
            default_color: default_color.map(str::to_owned),
            css_variable_prefix: "--shiki-".into(),
        }
    }

    #[test]
    fn ordered_css_union_escapes_html_and_keeps_empty_lines() {
        let html = render_html_with_themes(&fixture(), &options(None));
        assert!(html.starts_with(concat!(
            "<pre class=\"shiki shiki-themes light dark&#x3C;&#x26;&#x22;\" ",
            "style=\"--shiki-light:#111;--shiki-dark:#eee;--shiki-light-bg:inherit;--shiki-dark-bg:#222\" tabindex=\"0\"><code>"
        )));
        assert!(html.contains(concat!(
            "<span style=\"--shiki-light:#123;--shiki-light-text-decoration:underline;",
            "--shiki-light-font-style:inherit;--shiki-light-font-weight:inherit;",
            "--shiki-dark:inherit;--shiki-dark-text-decoration:line-through;",
            "--shiki-dark-font-style:italic;--shiki-dark-font-weight:bold\">&#x3C;&#x26;></span>"
        )));
        assert!(html.ends_with("</span>\n<span class=\"line\"></span></code></pre>"));
    }

    #[test]
    fn default_color_and_light_dark_keep_literal_and_variable_order() {
        let result = fixture();
        let html = render_html_with_themes(&result, &options(Some("light")));
        assert!(html.contains(
            "style=\"background-color:inherit;--shiki-dark-bg:#222;color:#111;--shiki-dark:#eee\""
        ));
        assert!(html.contains("<span style=\"color:#123;--shiki-light-text-decoration:underline;"));
        assert!(!html.contains("--shiki-light:#123"));
        let html = render_html_with_themes(&result, &options(Some("light-dark()")));
        assert!(html.contains("background-color:light-dark(inherit, #222);--shiki-light-bg:inherit;--shiki-dark-bg:#222;color:light-dark(#111, #eee);--shiki-light:#111;--shiki-dark:#eee"));
        assert!(html.contains("color:light-dark(#123, inherit);--shiki-light:#123;"));
        // A named variant other than light/dark still has inherited values
        // when CSS light-dark() cannot find its corresponding pair.
        let mut missing_pair = result.clone();
        missing_pair.themes[0].color = "custom".into();
        missing_pair.themes.pop();
        let html = render_html_with_themes(&missing_pair, &options(Some("light-dark()")));
        assert!(html.contains("background-color:light-dark(inherit, inherit)"));
    }

    #[test]
    fn root_controls_and_missing_styles_preserve_the_host_contract() {
        let mut result = fixture();
        result.tokens[0][0].variants.clear();
        let mut opts = options(None);
        opts.render.root_style = Some("<&\"".into());
        opts.render.tabindex = Some("<&\"".into());
        let html = render_html_with_themes(&result, &opts);
        assert!(html.contains("style=\"&#x3C;&#x26;&#x22;\" tabindex=\"&#x3C;&#x26;&#x22;\""));
        assert!(html.contains("<span>&#x3C;&#x26;></span>"));
        opts.render.include_root_style = false;
        opts.render.tabindex = None;
        let html = render_html_with_themes(&result, &opts);
        assert!(!html.contains(" style="));
        assert!(!html.contains("tabindex="));
        opts.render.include_root_style = true;
        opts.render.root_style = Some(String::new());
        opts.css_variable_prefix = "--custom-<&\"".into();
        let html = render_html_with_themes(&result, &opts);
        assert!(html.contains("--custom-&#x3C;&#x26;&#x22;light:#111"));
    }

    #[test]
    fn whitespace_and_equal_style_merging_keep_source_and_span_boundaries() {
        let mut result = fixture();
        let styled = result.tokens[0][0].clone();
        let mut plain = styled.clone();
        plain.content = "\u{feff}".into();
        plain.variants.clear();
        result.tokens = vec![vec![plain.clone(), styled.clone()]];
        let mut opts = options(None);
        let html = render_html_with_themes(&result, &opts);
        assert!(html.contains(">\u{feff}&#x3C;&#x26;></span>"));
        assert_eq!(html.matches("<span").count(), 2);
        opts.render.merge_whitespaces = false;
        assert_eq!(
            render_html_with_themes(&result, &opts)
                .matches("<span")
                .count(),
            3
        );
        opts.render.merge_whitespaces = true;
        result.tokens[0][0].content = "\u{85}".into();
        assert_eq!(
            render_html_with_themes(&result, &opts)
                .matches("<span")
                .count(),
            3
        );
        result.tokens[0] = vec![styled.clone(), styled];
        opts.render.merge_same_style_tokens = true;
        assert_eq!(
            render_html_with_themes(&result, &opts)
                .matches("<span")
                .count(),
            2
        );
        plain.content.clear();
        result.tokens[0].push(plain);
        assert_eq!(
            render_html_with_themes(&result, &opts)
                .matches("<span")
                .count(),
            3
        );
    }
}