markdown-viewer-rs 0.9.0

Render a Markdown file with GitHub styling and live-reload it in your browser
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
//! Page CSS: GitHub's stylesheet and syntax colours for every theme at
//! once, switched by `<html data-mdv-theme="auto|light|dark">`, so the
//! viewer can change theme instantly without re-rendering. Also the
//! built-in styles (`--style`).

use std::sync::OnceLock;

use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use syntect::highlighting::ThemeSet;
use syntect::html::{ClassStyle, css_for_theme_with_class_style};

/// GitHub's stylesheet (sindresorhus/github-markdown-css, auto variant):
/// shared rules plus one block of colour variables per scheme.
const GITHUB_CSS: &str = include_str!("../assets/github-markdown.css");

pub const HL_PREFIX: &str = "hl-";
const HL_LIGHT_THEME: &str = "InspiredGitHub";
const HL_DARK_THEME: &str = "base16-ocean.dark";

/// Page colour scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Theme {
    /// Follow the operating system's light/dark setting.
    #[default]
    Auto,
    Light,
    Dark,
}

impl Theme {
    pub fn as_str(self) -> &'static str {
        match self {
            Theme::Auto => "auto",
            Theme::Light => "light",
            Theme::Dark => "dark",
        }
    }
}

/// Built-in page styles, layered on top of GitHub's.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Style {
    /// GitHub's README look.
    #[default]
    Github,
    /// GitHub's look, using the full window width.
    Wide,
    /// Serif type and a narrower column, for long-form reading.
    Book,
    /// Smaller type and tighter spacing, for dense documents.
    Compact,
}

impl Style {
    pub fn css(self) -> &'static str {
        match self {
            Style::Github => "",
            Style::Wide => {
                ".markdown-body { max-width: none; padding-left: 60px; padding-right: 60px; }\n"
            }
            Style::Book => {
                ".markdown-body { max-width: 760px; font-size: 18px; line-height: 1.7; \
                 font-family: Charter, \"Bitstream Charter\", \"Iowan Old Style\", \
                 \"Palatino Linotype\", Palatino, Georgia, serif; }\n\
                 .markdown-body h1, .markdown-body h2 { border-bottom: none; padding-bottom: 0; }\n\
                 .markdown-body h1, .markdown-body h2, .markdown-body h3 { font-weight: 600; }\n\
                 .markdown-body p { hyphens: auto; }\n"
            }
            Style::Compact => {
                ".markdown-body { max-width: 1100px; font-size: 14px; line-height: 1.45; \
                 padding: 24px 32px; }\n\
                 .markdown-body p, .markdown-body ul, .markdown-body ol, .markdown-body pre, \
                 .markdown-body table, .markdown-body blockquote { margin-bottom: 10px; }\n\
                 .markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 18px; \
                 margin-bottom: 10px; }\n"
            }
        }
    }
}

/// The page's base stylesheet for all themes. Built once.
pub fn base_css() -> &'static str {
    static CSS: OnceLock<String> = OnceLock::new();
    CSS.get_or_init(build_base_css)
}

fn build_base_css() -> String {
    let (shared, light_vars, dark_vars) = split_github_css(GITHUB_CSS);
    let (hl_light, hl_dark) = highlight_css();

    let mut css = String::with_capacity(GITHUB_CSS.len() + hl_light.len() * 3);
    css.push_str(&shared);
    // The colour variables go on the root too, so the viewer's own UI
    // (toolbar, sidebar) can use them, and on .markdown-body to beat the
    // defaults GitHub's shared rules set there.
    let light = format!("html, .markdown-body {{\n{light_vars}\n}}\n{hl_light}{LIGHT_EXTRAS}");
    let dark = format!("html, .markdown-body {{\n{dark_vars}\n}}\n{hl_dark}{DARK_EXTRAS}");
    css.push_str(&themed(&light, &dark));
    css.push_str(LAYOUT);
    css.push_str(CALLOUTS);
    css.push_str(CODE_AND_TOC);
    css.push_str(&numbering_css());
    // Print (and PDF) always in light colours, whatever the screen theme.
    css.push_str("@media print {\n");
    css.push_str(&scope_rules(&light, ":where(:root[data-mdv-theme])"));
    css.push_str(PRINT);
    css.push_str("}\n");
    css
}

/// Rules that differ per scheme besides colour variables: the page
/// background, and GitHub's `#gh-dark-mode-only` / `#gh-light-mode-only`
/// image fragments.
const LIGHT_EXTRAS: &str = "html { background: #ffffff; }\n\
    .markdown-body img[src$=\"#gh-dark-mode-only\"] { display: none; }\n";
const DARK_EXTRAS: &str = "html { background: #0d1117; }\n\
    .markdown-body img[src$=\"#gh-light-mode-only\"] { display: none; }\n";

const LAYOUT: &str = "\nbody { margin: 0; }\n\
    .markdown-body { box-sizing: border-box; min-width: 200px; max-width: 980px; \
    margin: 0 auto; padding: 45px; }\n\
    @media (max-width: 767px) { .markdown-body { padding: 15px; } }\n\
    .markdown-body div.mermaid { margin-bottom: 16px; text-align: center; overflow-x: auto; }\n\
    .markdown-body pre.mermaid-error { border-left: 4px solid #d1242f; }\n\
    .markdown-body .math-display { overflow-x: auto; overflow-y: hidden; margin-bottom: 16px; }\n\
    .markdown-body .markdown-alert-title .octicon { margin-right: 8px; }\n";

/// Obsidian callouts, styled like GitHub alerts. Folded ones start closed.
const CALLOUTS: &str = "\
.markdown-body .callout { --mdv-callout: var(--borderColor-accent-emphasis); \
  padding: 8px 16px; margin-bottom: 16px; border-left: .25em solid var(--mdv-callout); }\n\
.markdown-body .callout-title { color: var(--mdv-callout); font-weight: 500; margin-bottom: 4px; }\n\
.markdown-body .callout[data-callout-fold=\"collapsed\"] .callout-content { display: none; }\n\
.markdown-body .callout:not([data-callout-fold=\"none\"]) .callout-title { cursor: pointer; }\n\
.markdown-body .callout:not([data-callout-fold=\"none\"]) .callout-title::after { content: \" â–¾\"; }\n\
.markdown-body .callout[data-callout-fold=\"collapsed\"] .callout-title::after { content: \" â–¸\"; }\n\
.markdown-body .callout-content > :last-child { margin-bottom: 0; }\n\
.markdown-body .callout:is([data-callout=\"tip\"], [data-callout=\"hint\"], [data-callout=\"success\"], \
  [data-callout=\"check\"], [data-callout=\"done\"]) { --mdv-callout: var(--borderColor-success-emphasis); }\n\
.markdown-body .callout:is([data-callout=\"important\"], [data-callout=\"example\"], \
  [data-callout=\"abstract\"], [data-callout=\"summary\"], [data-callout=\"tldr\"]) \
  { --mdv-callout: var(--borderColor-done-emphasis); }\n\
.markdown-body .callout:is([data-callout=\"warning\"], [data-callout=\"caution\"], \
  [data-callout=\"attention\"], [data-callout=\"question\"], [data-callout=\"help\"], \
  [data-callout=\"faq\"]) { --mdv-callout: var(--borderColor-attention-emphasis); }\n\
.markdown-body .callout:is([data-callout=\"failure\"], [data-callout=\"fail\"], [data-callout=\"missing\"], \
  [data-callout=\"danger\"], [data-callout=\"error\"], [data-callout=\"bug\"]) \
  { --mdv-callout: var(--borderColor-danger-emphasis); }\n\
.markdown-body .callout:is([data-callout=\"quote\"], [data-callout=\"cite\"]) \
  { --mdv-callout: var(--borderColor-default); }\n\
.markdown-body .mdv-wikilink-missing { opacity: .65; text-decoration: underline dashed; }\n\
.markdown-body .mdv-front-matter { margin-bottom: 16px; overflow-x: auto; }\n\
.markdown-body .mdv-front-matter table { margin-bottom: 0; }\n\
.markdown-body .mdv-front-matter-error pre { border-left: 4px solid #d1242f; }\n";

/// Code block extras (codeblocks.rs) and the `[TOC]` contents (toc.rs).
const CODE_AND_TOC: &str = "\
.markdown-body .mdv-code-figure { margin: 0 0 16px; }\n\
.markdown-body .mdv-code-title { font: 12px var(--fontStack-monospace, monospace); \
  color: var(--fgColor-muted); padding: 6px 16px; background: var(--bgColor-muted); \
  border: 1px solid var(--borderColor-muted); border-bottom: none; border-radius: 6px 6px 0 0; }\n\
.markdown-body .mdv-code-figure pre { border-top-left-radius: 0; border-top-right-radius: 0; margin-bottom: 0; }\n\
.markdown-body pre.mdv-lines .mdv-line { display: block; min-height: 1.45em; }\n\
.markdown-body pre.mdv-lines { counter-reset: mdv-line; }\n\
.markdown-body pre.mdv-numbered .mdv-line::before { counter-increment: mdv-line; content: counter(mdv-line); \
  display: inline-block; width: 2.5em; margin-right: 1em; text-align: right; color: var(--fgColor-muted); \
  opacity: .7; user-select: none; }\n\
.markdown-body pre.mdv-lines .mdv-hl { background: var(--bgColor-attention-muted); \
  margin: 0 -16px; padding: 0 16px; }\n\
.markdown-body pre.mdv-wrapped, .markdown-body pre.mdv-wrapped code { white-space: pre-wrap; word-break: break-word; }\n\
.markdown-body .mdv-toc { margin-bottom: 16px; border: 1px solid var(--borderColor-default); \
  border-radius: 6px; background: var(--bgColor-muted); font-size: 14px; line-height: 1.5; }\n\
.markdown-body .mdv-toc summary { list-style: none; cursor: pointer; display: flex; align-items: center; }\n\
.markdown-body .mdv-toc summary::-webkit-details-marker { display: none; }\n\
.markdown-body .mdv-toc summary::before { content: \"\"; flex: none; width: 5px; height: 5px; \
  margin: 0 7px 0 3px; border-right: 1.5px solid var(--fgColor-muted); \
  border-bottom: 1.5px solid var(--fgColor-muted); transform: rotate(-45deg); transition: transform .15s; }\n\
.markdown-body .mdv-toc details[open] > summary::before { transform: rotate(45deg); }\n\
.markdown-body .mdv-toc > details > summary { padding: 8px 12px; font-weight: 600; user-select: none; }\n\
.markdown-body .mdv-toc > details[open] > summary { border-bottom: 1px solid var(--borderColor-muted); }\n\
.markdown-body .mdv-toc > details > ul { padding: 6px 12px 8px; }\n\
.markdown-body .mdv-toc ul { list-style: none; margin: 0; padding-left: 0; }\n\
.markdown-body .mdv-toc ul ul { margin-left: 8px; padding-left: 8px; border-left: 1px solid var(--borderColor-muted); }\n\
.markdown-body .mdv-toc li, .markdown-body .mdv-toc li + li, .markdown-body .mdv-toc details { margin: 0; }\n\
.markdown-body .mdv-toc a { display: block; flex: 1; min-width: 0; padding: 2px 6px; border-radius: 4px; \
  color: var(--fgColor-default); text-decoration: none; }\n\
.markdown-body .mdv-toc li > a { margin-left: 15px; }\n\
.markdown-body .mdv-toc a:hover { background: var(--bgColor-neutral-muted); color: var(--fgColor-accent); }\n\
.markdown-body .mdv-toc > details > ul > li > a, \
.markdown-body .mdv-toc > details > ul > li > details > summary > a { font-weight: 500; }\n\
@media print { .markdown-body .mdv-toc { break-inside: avoid; background: none; } \
  .markdown-body .mdv-toc summary::before { display: none; } }\n\
.markdown-body .mdv-data-note { color: var(--fgColor-muted); font-size: 12px; }\n\
.markdown-body table.mdv-data td { white-space: pre-wrap; }\n";

/// CSS counters for `data-mdv-number="h1"` (number from h1) or `"h2"`
/// (a single h1 is the title). Four levels deep.
fn numbering_css() -> String {
    let mut css = String::new();
    for top in [1usize, 2] {
        let root = format!(":root[data-mdv-number=\"h{top}\"] .markdown-body");
        css.push_str(&format!("{root} {{ counter-reset: mdv-h{top}; }}\n"));
        let levels: Vec<usize> = (top..top + 4).filter(|l| *l <= 6).collect();
        for (i, level) in levels.iter().enumerate() {
            let next = levels.get(i + 1);
            let reset = next
                .map(|n| format!(" counter-reset: mdv-h{n};"))
                .unwrap_or_default();
            let content: Vec<String> = levels[..=i]
                .iter()
                .map(|l| format!("counter(mdv-h{l})"))
                .collect();
            css.push_str(&format!(
                "{root} h{level} {{{reset} }}\n\
                 {root} h{level}::before {{ counter-increment: mdv-h{level}; \
                 content: {} \". \"; }}\n",
                content.join(" \".\" ")
            ));
        }
    }
    css
}

/// Print layout: full width, no UI, and no page breaks inside blocks.
const PRINT: &str = "@page { margin: 16mm 14mm; }\n\
    .mdv-ui { display: none !important; }\n\
    body { padding: 0 !important; }\n\
    .markdown-body { max-width: none; padding: 0; font-size: 12pt; }\n\
    .markdown-body pre { white-space: pre-wrap; word-break: break-word; }\n\
    .markdown-body pre, .markdown-body table, .markdown-body blockquote, \
    .markdown-body .markdown-alert, .markdown-body div.mermaid, .markdown-body .math-display, \
    .markdown-body img, .markdown-body tr { break-inside: avoid; }\n\
    .markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { break-after: avoid; }\n\
    .markdown-body .anchor { display: none; }\n";

/// Scope two rule sets to the page's theme attribute, with `auto`
/// following the OS.
///
/// The theme selector is wrapped in `:where()`, which adds nothing to a
/// rule's specificity: `--css` stylesheets come last, so a plain
/// `.markdown-body { --fgColor-accent: … }` there overrides the theme.
fn themed(light: &str, dark: &str) -> String {
    let light_root = ":where(:root[data-mdv-theme=\"light\"])";
    let dark_root = ":where(:root[data-mdv-theme=\"dark\"])";
    let auto_root = ":where(:root[data-mdv-theme=\"auto\"])";
    format!(
        "{}{}@media (prefers-color-scheme: light) {{\n{}}}\n@media (prefers-color-scheme: dark) {{\n{}}}\n",
        scope_rules(light, light_root),
        scope_rules(dark, dark_root),
        scope_rules(light, auto_root),
        scope_rules(dark, auto_root),
    )
}

/// Prefix every selector of every rule with `scope`. Handles the flat,
/// one-rule-per-block CSS we generate here (no nested at-rules). A selector
/// starting with `html` becomes `scope` itself, since `:root` is `html`.
fn scope_rules(css: &str, scope: &str) -> String {
    let mut out = String::with_capacity(css.len() * 2);
    let mut rest = css;
    while let Some(open) = rest.find('{') {
        let close = rest[open..]
            .find('}')
            .map(|i| open + i)
            .unwrap_or(rest.len() - 1);
        let (selectors, body) = (&rest[..open], &rest[open..=close]);
        let (comments, selectors) = split_leading_comments(selectors);
        out.push_str(comments);
        let scoped: Vec<String> = selectors
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(|s| match s.strip_prefix("html") {
                Some(tail) => format!("{scope}{tail}"),
                None => format!("{scope} {s}"),
            })
            .collect();
        out.push_str(&scoped.join(",\n"));
        out.push(' ');
        out.push_str(body);
        out.push('\n');
        rest = &rest[close + 1..];
    }
    out
}

/// Separate `/* … */` comments preceding a selector from the selector.
fn split_leading_comments(s: &str) -> (&str, &str) {
    let mut end = 0;
    let mut rest = s;
    loop {
        let trimmed = rest.trim_start();
        if let Some(after) = trimmed.strip_prefix("/*") {
            match after.find("*/") {
                Some(i) => {
                    let consumed = rest.len() - trimmed.len() + 2 + i + 2;
                    end += consumed;
                    rest = &rest[consumed..];
                }
                None => break,
            }
        } else {
            break;
        }
    }
    (&s[..end], &s[end..])
}

/// Split GitHub's auto stylesheet into (shared rules, light variables,
/// dark variables). The variables live in two
/// `@media (prefers-color-scheme: …) { .markdown-body, [data-theme="…"] { … } }`
/// blocks; everything else is shared.
fn split_github_css(css: &str) -> (String, String, String) {
    let mut shared = css.to_string();
    let mut take = |scheme: &str| -> String {
        let start = shared
            .find(&format!("@media (prefers-color-scheme: {scheme}) {{"))
            .unwrap_or_else(|| panic!("github-markdown.css: no {scheme} block"));
        let inner_open = start + shared[start..].find('{').unwrap() + 1;
        let decl_open = inner_open + shared[inner_open..].find('{').unwrap() + 1;
        let decl_close = decl_open + shared[decl_open..].find('}').unwrap();
        let media_close = decl_close + 1 + shared[decl_close + 1..].find('}').unwrap();
        let vars = shared[decl_open..decl_close].trim().to_string();
        shared.replace_range(start..=media_close, "");
        vars
    };
    let dark = take("dark");
    let light = take("light");
    (shared, light, dark)
}

/// Syntax colours for (light, dark), without the theme's background so code
/// blocks keep GitHub's.
fn highlight_css() -> (String, String) {
    let themes = ThemeSet::load_defaults();
    let style = ClassStyle::SpacedPrefixed { prefix: HL_PREFIX };
    let css = |name: &str| {
        let raw = css_for_theme_with_class_style(&themes.themes[name], style).unwrap_or_default();
        strip_code_background(&raw)
    };
    (css(HL_LIGHT_THEME), css(HL_DARK_THEME))
}

/// Syntect themes paint a background on the outer `.code` scope; drop it.
fn strip_code_background(css: &str) -> String {
    let code_sel = format!(".{HL_PREFIX}code {{");
    let mut out = String::with_capacity(css.len());
    let mut in_code_rule = false;
    for line in css.lines() {
        if line.trim_start().starts_with(&code_sel) {
            in_code_rule = true;
        }
        if in_code_rule && line.trim_start().starts_with("background-color") {
            continue;
        }
        if line.contains('}') {
            in_code_rule = false;
        }
        out.push_str(line);
        out.push('\n');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn splits_github_css_into_shared_and_variables() {
        let (shared, light, dark) = split_github_css(GITHUB_CSS);
        assert!(
            !shared.contains("prefers-color-scheme"),
            "both media blocks removed"
        );
        assert!(light.contains("--bgColor-default: #ffffff"), "{light}");
        assert!(dark.contains("--bgColor-default: #0d1117"), "{dark}");
        assert!(shared.contains(".markdown-body h1"), "shared rules kept");
    }

    #[test]
    fn every_theme_is_switchable_at_runtime() {
        let css = base_css();
        for root in [
            ":where(:root[data-mdv-theme=\"light\"]),\n:where(:root[data-mdv-theme=\"light\"]) .markdown-body",
            ":where(:root[data-mdv-theme=\"dark\"]) .markdown-body",
            ":where(:root[data-mdv-theme=\"auto\"]) .markdown-body",
            ":where(:root[data-mdv-theme=\"dark\"]) .hl-",
        ] {
            assert!(css.contains(root), "missing {root}");
        }
        assert!(css.contains(":where(:root[data-mdv-theme=\"dark\"]) { background: #0d1117; }"));
        assert!(css.contains("img[src$=\"#gh-dark-mode-only\"] { display: none; }"));
    }

    #[test]
    fn numbering_counts_four_levels() {
        let css = numbering_css();
        assert!(css.contains(":root[data-mdv-number=\"h2\"] .markdown-body h2::before"));
        assert!(
            css.contains("content: counter(mdv-h2) \".\" counter(mdv-h3) \". \""),
            "{css}"
        );
        assert!(
            !css.contains("h2\"] .markdown-body h6::before"),
            "only four levels"
        );
    }

    #[test]
    fn scope_rules_handles_lists_comments_and_html() {
        let out = scope_rules("/* c */\n.a, .b { x: 1; }\nhtml { y: 2; }\n", ":root[t]");
        assert!(out.contains("/* c */"), "{out}");
        assert!(out.contains(":root[t] .a,\n:root[t] .b { x: 1; }"), "{out}");
        assert!(out.contains(":root[t] { y: 2; }"), "{out}");
    }

    #[test]
    fn strips_only_code_background() {
        let css = ".hl-code {\n color: #000;\n background-color: #fff;\n}\n.hl-x {\n background-color: #f00;\n}\n";
        let out = strip_code_background(css);
        assert!(!out.contains("#fff"));
        assert!(out.contains("#f00"));
    }
}