mdbook-ts 0.2.6

An mdBook preprocessor that uses tree-sitter to extract code snippets from source files
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Integration tests for mdbook-treesitter.
//!
//! These tests exercise the full pipeline: directive parsing, query execution,
//! and chapter processing.

use std::collections::HashMap;
use std::path::Path;

use mdbook_treesitter::{
    Directive,
    language::build_registry,
    process_chapter,
    query::{apply_strip, run_jq_query, run_treesitter_query},
};

// ─── Source fixture ───────────────────────────────────────────────────────────

const GEOMETRY_RS: &str = r#"/// A simple point in 2D space.
///
/// This struct is used throughout the geometry module.
pub struct Point {
    pub x: f64,
    pub y: f64,
}

/// A rectangle defined by its top-left corner, width, and height.
#[derive(Debug, Clone, PartialEq)]
pub struct Rectangle {
    pub origin: Point,
    pub width: f64,
    pub height: f64,
}

/// Configuration for the geometry module.
#[derive(Debug)]
pub struct Config {
    pub precision: u32,
}
"#;

// ─── Query strings (mirrors what book.toml defines) ───────────────────────────

/// Tree-sitter query: captures doc-comment lines immediately before a struct.
/// Handles 0 or 1 intervening attribute items (e.g. `#[derive(...)]`).
const DOC_COMMENT: &str = r#"[
  ((line_comment)+ @doc_comment
   .
   (struct_item name: (type_identifier) @name))
  ((line_comment)+ @doc_comment
   .
   (attribute_item)
   .
   (struct_item name: (type_identifier) @name))
]"#;

/// Tree-sitter query: captures the full struct declaration.
const STRUCT: &str = r#"(struct_item name: (type_identifier) @name) @struct"#;

/// Tree-sitter query: captures each field_declaration inside a struct body.
const STRUCT_FIELDS: &str = r#"
(struct_item
  name: (type_identifier) @name
  body: (field_declaration_list
    (field_declaration) @field))
"#;

/// Strip regex for doc-comment delimiters (mirrors the book.toml value).
const COMMENT_STRIP: &str = r#"^///? ?"#;

/// jq filter: extracts the last contiguous block of doc-comment lines
/// immediately before the named struct (skipping any attribute items).
const DOC_COMMENT_JQ: &str = r#"
.params.name as $target_name |
.children as $all |
([$all | to_entries[] |
  select(
    .value.type == "struct_item" and
    (.value.children[]? | select(.type == "type_identifier") | .text) == $target_name
  )
] | .[0].key) as $idx |
if $idx == null then error("struct not found")
else
  ([$all[0:$idx] | to_entries[] |
    select(.value.type != "line_comment" and .value.type != "attribute_item")
  ] | if length > 0 then last.key else -1 end) as $last_gap |
  [$all[($last_gap+1):$idx][] |
    select(.type == "line_comment") | .text | rtrimstr("\n")] |
  join("\n")
end
"#;

// ─── Directive parsing ────────────────────────────────────────────────────────

#[test]
fn parse_directive_file_only() {
    let d = Directive::parse("../../foo.rs").unwrap();
    assert_eq!(d.file_path, "../../foo.rs");
    assert_eq!(d.query_name, None);
    assert!(d.params.is_empty());
}

#[test]
fn parse_directive_with_query() {
    let d = Directive::parse("foo.rs#doc_comment").unwrap();
    assert_eq!(d.file_path, "foo.rs");
    assert_eq!(d.query_name.as_deref(), Some("doc_comment"));
    assert!(d.params.is_empty());
}

#[test]
fn parse_directive_with_query_and_param() {
    let d = Directive::parse("foo.rs#doc_comment?name=Foo").unwrap();
    assert_eq!(d.file_path, "foo.rs");
    assert_eq!(d.query_name.as_deref(), Some("doc_comment"));
    assert_eq!(d.params.get("name").map(|s| s.as_str()), Some("Foo"));
}

#[test]
fn parse_directive_with_multiple_params() {
    let d = Directive::parse("foo.rs#struct?name=Bar&visibility=pub").unwrap();
    assert_eq!(d.query_name.as_deref(), Some("struct"));
    assert_eq!(d.params["name"], "Bar");
    assert_eq!(d.params["visibility"], "pub");
}

#[test]
fn parse_directive_spaces_around_path() {
    let d = Directive::parse("  foo.rs  ").unwrap();
    assert_eq!(d.file_path, "foo.rs");
}

// ─── Tree-sitter query: doc_comment ──────────────────────────────────────────

fn rust_language() -> tree_sitter::Language {
    tree_sitter::Language::new(tree_sitter_rust::LANGUAGE)
}

#[test]
fn ts_doc_comment_point() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Point".into());
    let result = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        DOC_COMMENT,
        &params,
        None,
        None,
    )
    .unwrap();
    assert!(
        result.contains("A simple point in 2D space."),
        "got: {result}"
    );
    assert!(result.contains("This struct is used"), "got: {result}");
    // Must NOT contain Rectangle doc comment
    assert!(!result.contains("rectangle"), "got: {result}");
    // Must NOT have blank lines between comment lines
    assert!(
        !result.contains("\n\n"),
        "unexpected blank line in: {result}"
    );
}

#[test]
fn ts_doc_comment_rectangle() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Rectangle".into());
    let result = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        DOC_COMMENT,
        &params,
        None,
        None,
    )
    .unwrap();
    assert!(result.contains("A rectangle defined"), "got: {result}");
    assert!(!result.contains("Point"), "got: {result}");
    assert!(
        !result.contains("\n\n"),
        "unexpected blank line in: {result}"
    );
}

#[test]
fn ts_doc_comment_no_match_returns_error() {
    let mut params = HashMap::new();
    params.insert("name".into(), "NonExistent".into());
    let err = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        DOC_COMMENT,
        &params,
        None,
        None,
    )
    .unwrap_err();
    assert!(err.to_string().contains("no results"), "got: {err}");
}

// ─── Tree-sitter query: struct ────────────────────────────────────────────────

#[test]
fn ts_struct_rectangle_includes_body() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Rectangle".into());
    let result =
        run_treesitter_query(&rust_language(), GEOMETRY_RS, STRUCT, &params, None, None).unwrap();
    assert!(result.contains("Rectangle"), "got: {result}");
    assert!(result.contains("width"), "got: {result}");
}

#[test]
fn ts_struct_point() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Point".into());
    let result =
        run_treesitter_query(&rust_language(), GEOMETRY_RS, STRUCT, &params, None, None).unwrap();
    assert!(result.contains("pub struct Point"), "got: {result}");
    assert!(result.contains("pub x: f64"), "got: {result}");
}

// ─── jq query: doc_comment_jq ────────────────────────────────────────────────

#[test]
fn jq_doc_comment_point() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Point".into());
    let result = run_jq_query(&rust_language(), GEOMETRY_RS, DOC_COMMENT_JQ, &params).unwrap();
    assert!(
        result.contains("A simple point in 2D space."),
        "got: {result}"
    );
    // Must NOT bleed into Rectangle's comments
    assert!(!result.contains("rectangle"), "got: {result}");
    assert!(
        !result.contains("\n\n"),
        "unexpected blank line in: {result}"
    );
}

#[test]
fn jq_doc_comment_config() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Config".into());
    let result = run_jq_query(&rust_language(), GEOMETRY_RS, DOC_COMMENT_JQ, &params).unwrap();
    assert!(
        result.contains("Configuration for the geometry module."),
        "got: {result}"
    );
    // Must NOT contain Point's or Rectangle's doc comments
    assert!(!result.contains("simple point"), "got: {result}");
    assert!(!result.contains("rectangle"), "got: {result}");
}

// ─── process_chapter ─────────────────────────────────────────────────────────

fn make_registry() -> HashMap<String, mdbook_treesitter::language::LanguageEntry> {
    build_registry(&HashMap::new(), Path::new("/")).unwrap()
}

#[test]
fn process_chapter_whole_file() {
    use tempfile::tempdir;
    let dir = tempdir().unwrap();
    let src_path = dir.path().join("geometry.rs");
    std::fs::write(&src_path, GEOMETRY_RS).unwrap();

    let registry = make_registry();
    let content = "# Test\n\n```rust\n{{ #treesitter geometry.rs }}\n```\n";
    let result = process_chapter(content, dir.path(), &registry).unwrap();
    // The fence is passed through unchanged; the directive is replaced with raw text.
    assert!(result.contains("```rust"), "fence missing: {result}");
    assert!(result.contains("pub struct Point"), "got: {result}");
    // No double-fencing.
    assert!(
        !result.contains("```rust\n```rust"),
        "double fence: {result}"
    );
}

#[test]
fn process_chapter_missing_file_returns_error() {
    use tempfile::tempdir;
    let dir = tempdir().unwrap();
    let registry = make_registry();
    let content = "{{ #treesitter nonexistent.rs }}\n";
    let err = process_chapter(content, dir.path(), &registry).unwrap_err();
    assert!(
        err.to_string().contains("nonexistent.rs") || err.to_string().contains("reading"),
        "got: {err}"
    );
}

#[test]
fn process_chapter_unknown_extension_returns_error() {
    use tempfile::tempdir;
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("data.xyz"), "hello").unwrap();
    let registry = make_registry();
    let content = "{{ #treesitter data.xyz }}\n";
    let err = process_chapter(content, dir.path(), &registry).unwrap_err();
    assert!(
        err.to_string().contains(".xyz") || err.to_string().contains("no language"),
        "got: {err}"
    );
}

#[test]
fn process_chapter_no_braces_spaces() {
    use tempfile::tempdir;
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("geometry.rs"), GEOMETRY_RS).unwrap();

    let registry = make_registry();
    // Unspaced variant — whole-file expansion needs no query in the registry.
    let content = "{{#treesitter geometry.rs}}\n";
    let result = process_chapter(content, dir.path(), &registry).unwrap();
    assert!(result.contains("pub struct Point"), "got: {result}");
    assert!(!result.contains("```"), "unexpected fence: {result}");
}

#[test]
fn process_chapter_escaped_directive_is_not_expanded() {
    use tempfile::tempdir;
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("geometry.rs"), GEOMETRY_RS).unwrap();

    let registry = make_registry();
    let content = r"\{{ #treesitter geometry.rs#doc_comment?name=Point }}";
    let result = process_chapter(content, dir.path(), &registry).unwrap();
    // Backslash consumed, directive emitted literally.
    assert_eq!(
        result.trim(),
        "{{ #treesitter geometry.rs#doc_comment?name=Point }}"
    );
}

// ─── Language registry ────────────────────────────────────────────────────────

#[test]
fn registry_has_builtin_languages() {
    let registry = make_registry();
    assert!(registry.contains_key("rs"), "missing Rust");
    assert!(registry.contains_key("toml"), "missing TOML");
    assert!(registry.contains_key("md"), "missing Markdown");
}

#[test]
fn registry_rust_has_no_builtin_queries() {
    // Queries are defined in book.toml, not compiled in.
    let registry = make_registry();
    let rust = registry.get("rs").unwrap();
    assert!(rust.queries.is_empty(), "expected no built-in queries");
}

// ─── strip post-processing ────────────────────────────────────────────────────

#[test]
fn apply_strip_removes_comment_delimiters() {
    let input = "/// A simple point in 2D space.\n///\n/// Multi-line.";
    let result = apply_strip(input, COMMENT_STRIP).unwrap();
    assert_eq!(result, "A simple point in 2D space.\n\nMulti-line.");
}

#[test]
fn apply_strip_invalid_regex_returns_error() {
    let err = apply_strip("anything", "[invalid").unwrap_err();
    assert!(
        err.to_string().contains("invalid strip regex"),
        "got: {err}"
    );
}

#[test]
fn ts_comment_text_point_no_delimiters() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Point".into());
    // Run raw query then apply strip — mirrors what run_query does with a config.
    let raw = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        DOC_COMMENT,
        &params,
        None,
        None,
    )
    .unwrap();
    let result = apply_strip(&raw, COMMENT_STRIP).unwrap();
    assert!(
        result.contains("A simple point in 2D space."),
        "got: {result}"
    );
    // No `///` left in output.
    assert!(!result.contains("///"), "delimiters not stripped: {result}");
}

// ─── template rendering ───────────────────────────────────────────────────────

const COLOR_RS: &str = r#"
/// The primary colors.
pub enum Color {
    /// The color red.
    Red,
    /// The color green.
    Green,
    /// The color blue.
    Blue,
}
"#;

const ENUM_VARIANT_DOC: &str = r#"[
  ((line_comment)+ @doc_comment
   .
   (enum_variant name: (identifier) @name))
  ((line_comment)+ @doc_comment
   .
   (attribute_item)+
   .
   (enum_variant name: (identifier) @name))
]"#;

#[test]
fn ts_template_enum_variant_list() {
    let result = run_treesitter_query(
        &rust_language(),
        COLOR_RS,
        ENUM_VARIANT_DOC,
        &HashMap::new(),
        Some(r"^///? ?"),
        Some("- {name}: {doc_comment}"),
    )
    .unwrap();
    assert!(result.contains("- Red: The color red."), "got: {result}");
    assert!(
        result.contains("- Green: The color green."),
        "got: {result}"
    );
    assert!(result.contains("- Blue: The color blue."), "got: {result}");
    // Each entry on its own line.
    assert_eq!(result.lines().count(), 3, "got: {result}");
}

// ─── struct_fields capture ────────────────────────────────────────────────────

#[test]
fn ts_struct_fields_point() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Point".into());
    let result = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        STRUCT_FIELDS,
        &params,
        None,
        None,
    )
    .unwrap();
    // Individual fields, no surrounding struct scaffolding.
    assert!(result.contains("pub x: f64"), "got: {result}");
    assert!(result.contains("pub y: f64"), "got: {result}");
    assert!(
        !result.contains("pub struct"),
        "struct header leaked: {result}"
    );
    assert!(!result.contains('{'), "brace leaked: {result}");
}

#[test]
fn ts_struct_fields_rectangle() {
    let mut params = HashMap::new();
    params.insert("name".into(), "Rectangle".into());
    let result = run_treesitter_query(
        &rust_language(),
        GEOMETRY_RS,
        STRUCT_FIELDS,
        &params,
        None,
        None,
    )
    .unwrap();
    assert!(result.contains("pub origin: Point"), "got: {result}");
    assert!(result.contains("pub width: f64"), "got: {result}");
    assert!(result.contains("pub height: f64"), "got: {result}");
    assert!(
        !result.contains("pub struct"),
        "struct header leaked: {result}"
    );
}