dmc-core 0.2.0

Engine, CLI, watch mode, and collection builds for the dmc MDX compiler
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
use std::{path::Path, sync::Arc};

use dmc_codegen::{HtmlEmitter, MdxBodyEmitter, Walker};
use dmc_diagnostic::{
  Code,
  metadata::{Origin, SourceMeta},
};
use dmc_lexer::Lexer;
use dmc_parser::{Parser, ast::Document};
use dmc_transform::{CopyLinkedFilesOptions, MathEngine, PipelineConfig, PrettyCodeOptions};
use duck_diagnostic::DiagnosticEngine;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::engine::accumulator::Accumulator;

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(default)]
pub struct CompileConfig {
  pub markdown_gfm: bool,
  pub emit_html: bool,
  pub emit_body: bool,
  pub mdx_minify: bool,
  pub mdx_output_format: Option<String>,
  pub markdown_remark_plugins: Vec<Value>,
  pub markdown_rehype_plugins: Vec<Value>,
  pub mdx_remark_plugins: Vec<Value>,
  pub mdx_rehype_plugins: Vec<Value>,
  pub copy_linked_files: bool,
  pub output_assets: Option<String>,
  pub output_base: Option<String>,
  /// Pretty-code highlighter config. `None` = bundled defaults
  /// (Catppuccin Latte/Mocha pair, dark primary, multi-mode CSS-vars
  /// output). `Some` = explicit theme spec.
  pub pretty_code: Option<PrettyCodeOptions>,
  /// LaTeX engine for `$...$` / `$$...$$`. `None` = KaTeX (slow, exact
  /// rehype-katex parity). `Some(MathEngine::Mathml)` = pulldown-latex
  /// MathML (fast, plainer visuals).
  pub math_engine: Option<MathEngine>,
  /// Global override: when `true`, no plugin is treated as native-owned,
  /// every listed JS plugin runs in the sidecar, every native transformer
  /// is dropped from the pipeline.
  pub force_sidecar: bool,
  /// Per-plugin override: names in this list are *not* stripped from the
  /// sidecar payload, and the matching native transformer is dropped from
  /// the pipeline. Use to keep one specific JS plugin and let dmc handle
  /// everything else natively.
  ///
  /// Recognised entries:
  ///   "remark-gfm", "remark-math", "remark-emoji",
  ///   "rehype-pretty-code", "shiki",
  ///   "rehype-katex", "rehype-mathjax",
  ///   "rehype-slug", "rehype-autolink-headings"
  pub prefer_sidecar: Vec<String>,
}

impl Default for CompileConfig {
  fn default() -> Self {
    Self {
      markdown_gfm: true,
      emit_html: true,
      emit_body: true,
      mdx_output_format: None,
      mdx_minify: false,
      markdown_remark_plugins: vec![],
      markdown_rehype_plugins: vec![],
      mdx_remark_plugins: vec![],
      mdx_rehype_plugins: vec![],
      copy_linked_files: false,
      output_assets: None,
      output_base: None,
      pretty_code: None,
      math_engine: None,
      force_sidecar: false,
      prefer_sidecar: vec![],
    }
  }
}

impl CompileConfig {
  pub fn new() -> Self {
    Self::default()
  }

  pub fn has_js_plugins(&self) -> bool {
    !self.effective_markdown_remark_plugins().is_empty()
      || !self.effective_mdx_remark_plugins().is_empty()
      || !self.effective_markdown_rehype_plugins().is_empty()
      || !self.effective_mdx_rehype_plugins().is_empty()
  }

  /// Plugin lists after stripping every JS plugin whose work is now done
  /// by an in-process transformer (pretty-code/shiki, math, emoji). Used
  /// both for the sidecar gate and for the request payload so the sidecar
  /// never duplicates work. When the matching feature is off, that
  /// plugin's name is left in the list and the sidecar runs it.
  pub fn effective_markdown_remark_plugins(&self) -> Vec<Value> {
    self.filter_native_owned_remark(&self.markdown_remark_plugins)
  }

  pub fn effective_mdx_remark_plugins(&self) -> Vec<Value> {
    self.filter_native_owned_remark(&self.mdx_remark_plugins)
  }

  pub fn effective_markdown_rehype_plugins(&self) -> Vec<Value> {
    self.filter_native_owned_rehype(&self.markdown_rehype_plugins)
  }

  pub fn effective_mdx_rehype_plugins(&self) -> Vec<Value> {
    self.filter_native_owned_rehype(&self.mdx_rehype_plugins)
  }

  /// `true` when the user wants the sidecar to handle this specific
  /// plugin (either via `prefer_sidecar` per-plugin list or via the
  /// global `force_sidecar` flag).
  fn user_forces_sidecar(&self, name: &str) -> bool {
    self.force_sidecar || self.prefer_sidecar.iter().any(|n| n == name)
  }

  fn filter_native_owned_remark(&self, plugins: &[Value]) -> Vec<Value> {
    plugins
      .iter()
      .filter(|p| {
        let Some(name) = plugin_name(p) else { return true };
        if self.user_forces_sidecar(name) {
          return true;
        }
        !is_native_owned_remark(p)
      })
      .cloned()
      .collect()
  }

  fn filter_native_owned_rehype(&self, plugins: &[Value]) -> Vec<Value> {
    plugins
      .iter()
      .filter(|p| {
        let Some(name) = plugin_name(p) else { return true };
        if self.user_forces_sidecar(name) {
          return true;
        }
        !is_native_owned_rehype(p)
      })
      .cloned()
      .collect()
  }

  /// Per-file compile config: turns off native HTML when sidecar will run.
  pub fn for_render(&self) -> Self {
    let mut c = self.clone();
    c.emit_html = !self.has_js_plugins();
    c
  }

  /// Build the [`PipelineConfig`] consumed by
  /// [`Pipeline::with_defaults_for`]. `path` is the compiled file's path,
  /// used to resolve relative asset paths in the `copy-linked-files`
  /// transformer.
  pub fn pipeline_config(&self, path: &Path) -> PipelineConfig {
    let copy_linked_files = if self.copy_linked_files
      && let (Some(assets), Some(public)) = (self.output_assets.as_ref(), self.output_base.as_ref())
    {
      Some(CopyLinkedFilesOptions {
        source_dir: path.parent().unwrap_or(Path::new(".")).to_path_buf(),
        assets_dir: assets.into(),
        public_base: public.clone(),
      })
    } else {
      None
    };
    // Drop the native transformer when the user prefers the JS plugin
    // for that role. Each `drop_*` flag flips the matching opt-out
    // field in `PipelineConfig` so the transformer is not pushed.
    let prefers = |needles: &[&str]| -> bool {
      self.force_sidecar || self.prefer_sidecar.iter().any(|n| needles.contains(&n.as_str()))
    };
    let drop_pretty_code = prefers(&["rehype-pretty-code", "shiki"]);
    let drop_math = prefers(&["remark-math", "rehype-katex", "rehype-mathjax"]);
    let drop_emoji = prefers(&["remark-emoji"]);
    let drop_autolink_headings = prefers(&["rehype-slug", "rehype-autolink-headings"]);
    let drop_gfm = prefers(&["remark-gfm"]);

    PipelineConfig {
      markdown_gfm: Some(if drop_gfm { false } else { self.markdown_gfm }),
      pretty_code: if drop_pretty_code { None } else { self.pretty_code.clone() },
      math_engine: if drop_math { None } else { self.math_engine },
      copy_linked_files,
      emoji: if drop_emoji { Some(false) } else { None },
      autolink_headings: if drop_autolink_headings { Some(false) } else { None },
      math: if drop_math { Some(false) } else { None },
      pretty_code_enabled: if drop_pretty_code { Some(false) } else { None },
    }
  }
}

/// Extract the plugin name from either the bare string form
/// (`"rehype-pretty-code"`) or the `[name, options]` array form used by
/// unified-style plugin configs.
fn plugin_name(plugin: &Value) -> Option<&str> {
  match plugin {
    Value::String(s) => Some(s.as_str()),
    Value::Array(a) => a.first().and_then(Value::as_str),
    _ => None,
  }
}

/// `true` when `plugin` is a remark-side plugin whose work an in-process
/// transformer now does. Stripped from the sidecar payload so the JS
/// plugin chain does not redo native work.
fn is_native_owned_remark(plugin: &Value) -> bool {
  let Some(name) = plugin_name(plugin) else { return false };
  match name {
    // GFM tables, strikethrough, autolinks, task lists are handled by
    // the dmc parser; remark-gfm in the sidecar is redundant.
    "remark-gfm" => true,
    "remark-math" => cfg!(feature = "math"),
    "remark-emoji" => cfg!(feature = "emoji"),
    _ => false,
  }
}

/// Same for rehype-side plugins.
fn is_native_owned_rehype(plugin: &Value) -> bool {
  let Some(name) = plugin_name(plugin) else { return false };
  match name {
    "rehype-pretty-code" | "shiki" => cfg!(feature = "pretty-code"),
    "rehype-katex" | "rehype-mathjax" => cfg!(feature = "math"),
    // Heading slugs + anchor links handled by the AutolinkHeadings
    // transformer in `Pipeline::with_defaults`.
    "rehype-slug" | "rehype-autolink-headings" => true,
    _ => false,
  }
}

pub struct Compiler;

impl Compiler {
  /// One-shot compile of `source` with the default pipeline. Use
  /// `compile_with_pipeline` for file-aware compilation with real spans.
  pub fn compile(source: &str, diag_engine: &mut DiagnosticEngine<Code>) -> CompileOutput {
    // FIX:
    Self::compile_with_pipeline(source, Path::new("."), &CompileConfig::new(), diag_engine)
  }

  /// Like [`compile`] with a caller-supplied pipeline + path for spans.
  pub fn compile_with_pipeline(
    source: &str,
    path: &Path,
    compile_cfg: &CompileConfig,
    diag_engine: &mut DiagnosticEngine<Code>,
  ) -> CompileOutput {
    // Each layer holds its own DiagnosticEngine, mirroring the Lexer pattern.
    let meta = Arc::from(SourceMeta { path: Arc::from(path.display().to_string()), origin: Origin::File(path.into()) });
    // Source-level math: rewrite `$...$` / `$$...$$` to `<MathMl/>` JSX
    // so the parser does not interpret `_` or `^` inside math as Markdown
    // emphasis markers.
    #[cfg(feature = "math")]
    let preprocessed = dmc_transform::Math::preprocess_source(source);
    #[cfg(feature = "math")]
    let source: &str = &preprocessed;
    let mut lexer = Lexer::new(source, meta.clone(), diag_engine);
    let _ = lexer.scan_tokens();

    let mut doc = {
      let mut parser = Parser::new(lexer.tokens, meta.clone(), diag_engine);
      parser.parse()
    };

    let pipeline_cfg = compile_cfg.pipeline_config(path);
    let pipeline = dmc_transform::Pipeline::with_defaults_for(&pipeline_cfg);

    pipeline.run(&mut doc, &meta, diag_engine);

    Self::finalize(source, doc, compile_cfg, diag_engine)
  }

  /// Pull frontmatter + imports/exports, render HTML + MDX body, derive
  /// excerpt / metadata / TOC, pack into a `CompileOutput`. Each sink
  /// owns a private `DiagnosticEngine` during the walk; we merge them
  /// into the caller's `diag_engine` after the walk completes (avoids
  /// `RefCell` overhead on every sink emit).
  fn finalize(
    source: &str,
    doc: Document,
    compile_cfg: &CompileConfig,
    diag_engine: &mut DiagnosticEngine<Code>,
  ) -> CompileOutput {
    let mut acc = Accumulator::new();
    let mut html_sink = if compile_cfg.emit_html { Some(HtmlEmitter::new()) } else { None };
    let mut body_sink = if compile_cfg.emit_body { Some(MdxBodyEmitter::new()) } else { None };

    let mut sinks: Vec<&mut dyn dmc_codegen::NodeSink> = Vec::with_capacity(3);
    sinks.push(&mut acc);
    if let Some(ref mut h) = html_sink {
      sinks.push(h);
    }
    if let Some(ref mut b) = body_sink {
      sinks.push(b);
    }

    Walker::new(&doc).walk(sinks.as_mut_slice());

    let (html, body) = match (html_sink, body_sink) {
      (Some(h), Some(b)) => {
        let (s, hd) = h.into_parts();
        let (m, bd) = b.into_parts();
        diag_engine.extend(hd);
        diag_engine.extend(bd);
        (s, m)
      },
      (Some(h), None) => {
        let (s, hd) = h.into_parts();
        diag_engine.extend(hd);
        (s, String::new())
      },
      (None, Some(b)) => {
        let (m, bd) = b.into_parts();
        diag_engine.extend(bd);
        (String::new(), m)
      },
      (None, None) => (String::new(), String::new()),
    };

    acc.into_compile_output(source, html, body, compile_cfg)
  }
}

/// Reading-time + word-count from plain text. `reading_time` in minutes,
/// ceil, min 1.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
  pub reading_time: u32,
  pub word_count: u32,
}

/// One TOC node. `url` is `#<heading-slug>`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TocItem {
  pub title: String,
  pub url: String,
  pub items: Vec<TocItem>,
}

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

  #[test]
  fn empty_plugin_lists_no_sidecar() {
    let cfg = CompileConfig::default();
    assert!(!cfg.has_js_plugins());
  }

  #[test]
  fn arbitrary_remark_plugin_triggers_sidecar() {
    let mut cfg = CompileConfig::default();
    // Pick a plugin not covered by any native transformer.
    cfg.markdown_remark_plugins.push(json!("remark-frontmatter"));
    assert!(cfg.has_js_plugins());
  }

  #[test]
  fn remark_gfm_alone_skips_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_remark_plugins.push(json!("remark-gfm"));
    assert!(!cfg.has_js_plugins(), "dmc parser handles GFM natively");
  }

  #[test]
  fn rehype_slug_and_autolink_alone_skip_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_rehype_plugins.push(json!("rehype-slug"));
    cfg.markdown_rehype_plugins.push(json!(["rehype-autolink-headings", { "behavior": "wrap" }]));
    assert!(!cfg.has_js_plugins(), "AutolinkHeadings transformer handles slug + anchor natively");
  }

  #[cfg(feature = "math")]
  #[test]
  fn remark_math_alone_with_native_skips_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_remark_plugins.push(json!("remark-math"));
    cfg.markdown_rehype_plugins.push(json!(["rehype-katex", { "errorColor": "red" }]));
    assert!(!cfg.has_js_plugins(), "native math should absorb remark-math + rehype-katex");
  }

  #[cfg(feature = "emoji")]
  #[test]
  fn remark_emoji_alone_with_native_skips_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_remark_plugins.push(json!("remark-emoji"));
    assert!(!cfg.has_js_plugins(), "native emoji should absorb remark-emoji");
  }

  #[cfg(feature = "pretty-code")]
  #[test]
  fn rehype_pretty_code_alone_with_native_skips_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_rehype_plugins.push(json!("rehype-pretty-code"));
    cfg.mdx_rehype_plugins.push(json!(["rehype-pretty-code", { "theme": "github-dark" }]));
    cfg.mdx_rehype_plugins.push(json!("shiki"));
    assert!(!cfg.has_js_plugins(), "native should absorb rehype-pretty-code/shiki");
  }

  #[cfg(feature = "pretty-code")]
  #[test]
  fn other_rehype_plugin_still_triggers_sidecar_even_with_native() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_rehype_plugins.push(json!("rehype-pretty-code"));
    // Any rehype plugin not absorbed by a native transformer keeps the
    // sidecar alive. Pick something that no current native pass owns.
    cfg.markdown_rehype_plugins.push(json!("rehype-external-links"));
    assert!(cfg.has_js_plugins());
  }

  #[cfg(not(feature = "pretty-code"))]
  #[test]
  fn pretty_code_feature_off_means_rehype_pretty_code_routes_to_sidecar() {
    let mut cfg = CompileConfig::default();
    cfg.markdown_rehype_plugins.push(json!("rehype-pretty-code"));
    assert!(cfg.has_js_plugins());
  }
}

/// Compiled `.mdx` output. Every field is always populated; serialised
/// camelCase for JS parity.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompileOutput {
  pub frontmatter: serde_json::Value,
  pub frontmatter_raw: String,
  pub content: String,
  pub html: String,
  pub body: String,
  pub excerpt: String,
  pub metadata: Metadata,
  pub toc: Vec<TocItem>,
  pub imports: Vec<String>,
  pub exports: Vec<String>,
}