markdown-viewer-rs 0.7.1

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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! Obsidian notes: resolving `[[wiki links]]` and `![[embeds]]` the way
//! Obsidian does (by note name anywhere in the vault, preferring the
//! closest match). Callouts, `==highlights==` and `%%comments%%` are parsed
//! by comrak; callouts are styled in theme.rs.

use std::path::{Component, Path, PathBuf};

use percent_encoding::{AsciiSet, CONTROLS, percent_decode_str, utf8_percent_encode};

use crate::render::{escape_attr, escape_text};

const IMAGE_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "svg", "webp", "avif", "bmp"];

pub const SEGMENT: &AsciiSet = &CONTROLS
    .add(b' ')
    .add(b'"')
    .add(b'#')
    .add(b'%')
    .add(b'<')
    .add(b'>')
    .add(b'?')
    .add(b'`')
    .add(b'{')
    .add(b'}')
    .add(b'/');

/// Renders an embedded note: (root-relative path, optional heading) → HTML.
pub type EmbedFn<'a> = dyn Fn(&str, Option<&str>) -> Option<String> + 'a;

/// Where a document is, for resolving its links.
#[derive(Clone, Copy)]
pub struct LinkContext<'a> {
    /// The vault (or served folder) root.
    pub root: &'a Path,
    /// The document's own folder.
    pub dir: &'a Path,
    /// Markdown files under the root, `/`-separated and root-relative.
    pub files: &'a [String],
    /// Served pages link root-relative (`/notes/a.md`); standalone pages
    /// link relative to the document's folder.
    pub served: bool,
    /// How many notes deep we are in `![[embeds]]` (the page itself is 0).
    pub depth: usize,
}

/// Embedded notes go at most this deep (so notes embedding each other stop).
pub const MAX_EMBED_DEPTH: usize = 2;

/// Is this inside an Obsidian vault (a `.obsidian` folder here or above)?
pub fn in_vault(path: &Path) -> bool {
    path.ancestors().any(|dir| dir.join(".obsidian").is_dir())
}

/// The vault root containing `path`, if any.
pub fn vault_root(path: &Path) -> Option<PathBuf> {
    path.ancestors()
        .find(|dir| dir.join(".obsidian").is_dir())
        .map(Path::to_path_buf)
}

/// The Markdown of `text` under the heading `heading` (up to the next
/// heading of the same or a higher level), for `![[Note#Heading]]`.
pub fn section(text: &str, heading: &str) -> Option<String> {
    let wanted = comrak::Anchorizer::new().anchorize(heading);
    let mut out = Vec::new();
    let mut level = None;
    for line in text.lines() {
        let hashes = line.chars().take_while(|c| *c == '#').count();
        let is_heading = (1..=6).contains(&hashes) && line[hashes..].starts_with(' ');
        if let Some(l) = level {
            if is_heading && hashes <= l {
                break;
            }
            out.push(line);
        } else if is_heading && comrak::Anchorizer::new().anchorize(line[hashes..].trim()) == wanted
        {
            level = Some(hashes);
            out.push(line);
        }
    }
    level.map(|_| out.join("\n"))
}

/// Drop `data-sourcepos` attributes (an embedded note's line numbers would
/// point into the wrong file).
pub fn strip_sourcepos(html: &str) -> String {
    let mut out = String::with_capacity(html.len());
    let mut rest = html;
    while let Some(at) = rest.find(" data-sourcepos=\"") {
        out.push_str(&rest[..at]);
        let after = &rest[at + " data-sourcepos=\"".len()..];
        rest = after.find('"').map_or("", |end| &after[end + 1..]);
    }
    out.push_str(rest);
    out
}

/// Backlinks: lines in other notes that link to `target` (root-relative),
/// by `[[wiki link]]` or a Markdown link.
pub fn backlinks(root: &Path, files: &[String], target: &str) -> Vec<(String, usize, String)> {
    let mut hits = Vec::new();
    for file in files {
        if file == target {
            continue;
        }
        let path = root.join(file);
        let Ok(text) = std::fs::read_to_string(&path) else {
            continue;
        };
        let dir = path.parent().unwrap_or(root);
        let ctx = LinkContext {
            root,
            dir,
            files,
            served: true,
            depth: 0,
        };
        for (i, line) in text.lines().enumerate() {
            if links_in(line).iter().any(|l| links_to(l, &ctx, target)) {
                hits.push((file.clone(), i + 1, line.trim().chars().take(160).collect()));
            }
        }
    }
    hits
}

/// Link targets on a line: `[[target|…]]` (true) and `](target)` (false).
fn links_in(line: &str) -> Vec<(bool, String)> {
    let mut found = Vec::new();
    let mut rest = line;
    while let Some(at) = rest.find("[[") {
        let after = &rest[at + 2..];
        let Some(end) = after.find("]]") else { break };
        let target = after[..end].split(['|', '#']).next().unwrap_or("").trim();
        if !target.is_empty() {
            found.push((true, target.to_string()));
        }
        rest = &after[end + 2..];
    }
    let mut rest = line;
    while let Some(at) = rest.find("](") {
        let after = &rest[at + 2..];
        let Some(end) = after.find(')') else { break };
        let target = after[..end].split_whitespace().next().unwrap_or("");
        let target = target
            .trim_matches(['<', '>'])
            .split('#')
            .next()
            .unwrap_or("");
        if !target.is_empty() && !target.contains("://") && !target.starts_with("mailto:") {
            found.push((
                false,
                percent_decode_str(target).decode_utf8_lossy().into_owned(),
            ));
        }
        rest = &after[end + 1..];
    }
    found
}

fn links_to((wiki, target): &(bool, String), ctx: &LinkContext, wanted: &str) -> bool {
    if *wiki {
        return find_note(target, ctx).as_deref() == Some(wanted);
    }
    let path = if target.starts_with('/') {
        ctx.root.join(target.trim_start_matches('/'))
    } else {
        ctx.dir.join(target)
    };
    normalize(&path)
        .strip_prefix(ctx.root)
        .is_ok_and(|rel| rel.to_string_lossy().replace('\\', "/") == wanted)
}

/// Resolve `.` and `..` without touching the filesystem.
fn normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for c in path.components() {
        match c {
            Component::ParentDir => {
                out.pop();
            }
            Component::CurDir => {}
            other => out.push(other),
        }
    }
    out
}

/// Resolve wiki links and embeds in rendered HTML.
pub fn finish(html: String, ctx: &LinkContext, embed: &EmbedFn) -> String {
    let html = if html.contains("data-wikilink=\"true\"") {
        resolve_links(&html, ctx)
    } else {
        html
    };
    if html.contains("![[") {
        resolve_embeds(&html, ctx, embed)
    } else {
        html
    }
}

/// Rewrite the href of every `<a … data-wikilink="true">`.
fn resolve_links(html: &str, ctx: &LinkContext) -> String {
    let mut out = String::with_capacity(html.len());
    let mut rest = html;
    while let Some(marker) = rest.find("data-wikilink=\"true\"") {
        let tag_start = rest[..marker].rfind("<a ").unwrap_or(marker);
        let tag = &rest[tag_start..marker];
        let Some(href_at) = tag.find("href=\"") else {
            out.push_str(&rest[..marker + 1]);
            rest = &rest[marker + 1..];
            continue;
        };
        let value_start = tag_start + href_at + 6;
        let value_end = value_start + rest[value_start..].find('"').unwrap_or(0);
        let target = percent_decode_str(&rest[value_start..value_end]).decode_utf8_lossy();
        out.push_str(&rest[..value_start]);
        match resolve(&target, ctx) {
            Some(url) => {
                out.push_str(&escape_attr(&url));
                out.push_str(&rest[value_end..marker]);
            }
            None => {
                out.push_str(&rest[value_start..value_end]);
                out.push_str(&rest[value_end..marker]);
                out.push_str("class=\"mdv-wikilink-missing\" title=\"Note not found\" ");
            }
        }
        out.push_str("data-wikilink=\"true\"");
        rest = &rest[marker + "data-wikilink=\"true\"".len()..];
    }
    out.push_str(rest);
    out
}

/// Turn `![[name]]` / `![[name|300]]` text into images (or links, for notes).
fn resolve_embeds(html: &str, ctx: &LinkContext, embed: &EmbedFn) -> String {
    let mut out = String::with_capacity(html.len());
    let mut rest = html;
    while let Some(start) = rest.find("![[") {
        let Some(len) = rest[start + 3..].find("]]") else {
            break;
        };
        let inner = &rest[start + 3..start + 3 + len];
        if inner.contains('<') || inner.contains('\n') {
            out.push_str(&rest[..start + 3]);
            rest = &rest[start + 3..];
            continue;
        }
        out.push_str(&rest[..start]);
        let (target, label) = match inner.split_once('|') {
            Some((t, l)) => (t.trim(), Some(l.trim())),
            None => (inner.trim(), None),
        };
        let target = html_unescape(target);
        let url = resolve(&target, ctx);
        // A note: show it inline. If the embed is a paragraph of its own,
        // replace the paragraph, since a block can't sit inside one.
        let (note_path, heading) = match target.split_once('#') {
            Some((p, h)) => (p.trim(), Some(h.trim())),
            None => (target.trim(), None),
        };
        let note = (!note_path.is_empty())
            .then(|| find_note(note_path, ctx))
            .flatten()
            .and_then(|rel| embed(&rel, heading));
        if let (Some(note_html), Some(url)) = (&note, &url) {
            let embed_html = format!(
                "<div class=\"mdv-embed-note\"><div class=\"mdv-embed-title\"><a href=\"{}\">{}</a></div>{}</div>",
                escape_attr(url),
                escape_text(&target),
                note_html
            );
            let p_open = out.rfind("<p").filter(|&p| {
                let tail = &out[p..];
                tail.ends_with('>') && tail.matches('<').count() == 1
            });
            let after = &rest[start + 3 + len + 2..];
            if let (Some(p), Some(stripped)) = (p_open, after.strip_prefix("</p>")) {
                out.truncate(p);
                out.push_str(&embed_html);
                rest = stripped;
            } else {
                out.push_str(&embed_html);
                rest = after;
            }
            continue;
        }
        let is_image = Path::new(target.split('#').next().unwrap_or(""))
            .extension()
            .and_then(|e| e.to_str())
            .is_some_and(|e| IMAGE_EXTENSIONS.contains(&e.to_ascii_lowercase().as_str()));
        match (url, is_image) {
            (Some(url), true) => {
                let width = label
                    .and_then(|l| l.split('x').next())
                    .filter(|w| w.chars().all(|c| c.is_ascii_digit()) && !w.is_empty())
                    .map(|w| format!(" width=\"{w}\""))
                    .unwrap_or_default();
                out.push_str(&format!(
                    "<img src=\"{}\" alt=\"{}\"{width}>",
                    escape_attr(&url),
                    escape_attr(&target)
                ));
            }
            (Some(url), false) => out.push_str(&format!(
                "<a class=\"mdv-embed\" href=\"{}\">{}</a>",
                escape_attr(&url),
                escape_text(&target)
            )),
            (None, _) => out.push_str(&format!(
                "<span class=\"mdv-wikilink-missing\" title=\"Not found\">{}</span>",
                escape_text(&target)
            )),
        }
        rest = &rest[start + 3 + len + 2..];
    }
    out.push_str(rest);
    out
}

fn html_unescape(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
}

/// Resolve a wiki-link target (`Note`, `dir/Note`, `Note#Heading`,
/// `image.png`) to a URL.
fn resolve(target: &str, ctx: &LinkContext) -> Option<String> {
    let (path, anchor) = match target.split_once('#') {
        Some((p, a)) => (p.trim(), Some(a.trim())),
        None => (target.trim(), None),
    };
    let fragment = anchor
        .map(|a| {
            if a.starts_with('^') {
                format!("#{a}")
            } else {
                format!("#{}", comrak::Anchorizer::new().anchorize(a))
            }
        })
        .unwrap_or_default();
    if path.is_empty() {
        return Some(fragment);
    }
    let rel = find_note(path, ctx).or_else(|| find_file(path, ctx))?;
    Some(format!("{}{fragment}", url_for(&rel, ctx)))
}

/// A Markdown note by name or partial path, Obsidian-style: an exact
/// root-relative path wins, then notes in the current folder, then the
/// shortest path anywhere.
pub fn find_note(path: &str, ctx: &LinkContext) -> Option<String> {
    let wanted = if crate::docs::is_markdown(Path::new(path)) {
        path.to_string()
    } else {
        format!("{path}.md")
    };
    let wanted_lc = wanted.to_lowercase();
    let suffix = format!("/{wanted_lc}");
    let here = ctx
        .dir
        .strip_prefix(ctx.root)
        .map(|p| p.to_string_lossy().replace('\\', "/"))
        .unwrap_or_default();
    let mut matches: Vec<&String> = ctx
        .files
        .iter()
        .filter(|f| {
            let lc = f.to_lowercase();
            lc == wanted_lc || lc.ends_with(&suffix)
        })
        .collect();
    matches.sort_by_key(|f| {
        let in_here = Path::new(f.as_str())
            .parent()
            .is_some_and(|p| p.to_string_lossy() == here);
        let exact = f.to_lowercase() == wanted_lc;
        (!exact, !in_here, f.matches('/').count(), f.len())
    });
    matches.first().map(|f| f.to_string())
}

/// Any other file (images, PDFs): next to the document, root-relative, or
/// (as Obsidian does for attachments) by file name anywhere in the vault.
fn find_file(path: &str, ctx: &LinkContext) -> Option<String> {
    for base in [ctx.dir, ctx.root] {
        let candidate = base.join(path);
        if candidate.is_file()
            && let Ok(rel) = candidate.strip_prefix(ctx.root)
            && !rel.components().any(|c| matches!(c, Component::ParentDir))
        {
            return Some(rel.to_string_lossy().replace('\\', "/"));
        }
    }
    if path.contains('/') {
        return None;
    }
    let mut found = Vec::new();
    let mut budget = 5000;
    find_by_name(ctx.root, &path.to_lowercase(), 0, &mut budget, &mut found);
    found.sort_by_key(|p| (p.components().count(), p.as_os_str().len()));
    let rel = found.first()?.strip_prefix(ctx.root).ok()?;
    Some(rel.to_string_lossy().replace('\\', "/"))
}

/// Files named `name` (lowercase) under `dir`, skipping hidden folders and
/// symlinks, within a budget of directory entries.
fn find_by_name(dir: &Path, name: &str, depth: usize, budget: &mut usize, out: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        if *budget == 0 || depth > 8 {
            return;
        }
        *budget -= 1;
        let file_name = entry.file_name().to_string_lossy().to_lowercase();
        if file_name.starts_with('.') {
            continue;
        }
        match entry.file_type() {
            Ok(t) if t.is_dir() => find_by_name(&entry.path(), name, depth + 1, budget, out),
            Ok(t) if t.is_file() && file_name == name => out.push(entry.path()),
            _ => {}
        }
    }
}

/// URL for a root-relative path, from this document.
fn url_for(rel: &str, ctx: &LinkContext) -> String {
    let encode = |p: &str| {
        p.split('/')
            .map(|seg| utf8_percent_encode(seg, SEGMENT).to_string())
            .collect::<Vec<_>>()
            .join("/")
    };
    if ctx.served {
        return format!("/{}", encode(rel));
    }
    // Relative to the document's folder: up to the root, then down.
    let here = ctx.dir.strip_prefix(ctx.root).unwrap_or(Path::new(""));
    let ups = here.components().count();
    format!("{}{}", "../".repeat(ups), encode(rel))
}

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

    fn ctx<'a>(
        root: &'a Path,
        dir: &'a Path,
        files: &'a [String],
        served: bool,
    ) -> LinkContext<'a> {
        LinkContext {
            root,
            dir,
            files,
            served,
            depth: 0,
        }
    }

    fn no_embed(_: &str, _: Option<&str>) -> Option<String> {
        None
    }

    fn files() -> Vec<String> {
        [
            "Home.md",
            "notes/My Note.md",
            "notes/deep/Other.md",
            "archive/My Note.md",
        ]
        .map(String::from)
        .to_vec()
    }

    #[test]
    fn resolves_notes_by_name_preferring_closest() {
        let f = files();
        let root = Path::new("/vault");
        let c = ctx(root, Path::new("/vault/notes"), &f, true);
        assert_eq!(
            resolve("My Note", &c).as_deref(),
            Some("/notes/My%20Note.md")
        );
        assert_eq!(
            resolve("other", &c).as_deref(),
            Some("/notes/deep/Other.md")
        );
        assert_eq!(
            resolve("archive/My Note", &c).as_deref(),
            Some("/archive/My%20Note.md")
        );
        assert_eq!(
            resolve("Home#Getting Started", &c).as_deref(),
            Some("/Home.md#getting-started")
        );
        assert_eq!(resolve("#Local", &c).as_deref(), Some("#local"));
        assert_eq!(resolve("Missing", &c), None);
    }

    #[test]
    fn standalone_links_are_relative() {
        let f = files();
        let c = ctx(
            Path::new("/vault"),
            Path::new("/vault/notes/deep"),
            &f,
            false,
        );
        assert_eq!(resolve("Home", &c).as_deref(), Some("../../Home.md"));
    }

    #[test]
    fn rewrites_links_and_marks_missing_ones() {
        let f = files();
        let c = ctx(Path::new("/vault"), Path::new("/vault"), &f, true);
        let html = "<p><a href=\"My%20Note\" data-wikilink=\"true\">My Note</a> \
                    <a data-sourcepos=\"1:1-1:2\" href=\"Nope\" data-wikilink=\"true\">Nope</a></p>";
        let out = finish(html.into(), &c, &no_embed);
        assert!(
            out.contains("href=\"/notes/My%20Note.md\" data-wikilink"),
            "{out}"
        );
        assert!(out.contains("class=\"mdv-wikilink-missing\""), "{out}");
    }

    #[test]
    fn embeds_images_with_size() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("pic.png"), b"x").unwrap();
        let f = files();
        let c = ctx(dir.path(), dir.path(), &f, true);
        let out = finish(
            "<p>![[pic.png|300]] and ![[Home]] and ![[gone.png]]</p>".into(),
            &c,
            &no_embed,
        );
        assert!(
            out.contains("<img src=\"/pic.png\" alt=\"pic.png\" width=\"300\">"),
            "{out}"
        );
        assert!(
            out.contains("<a class=\"mdv-embed\" href=\"/Home.md\">Home</a>"),
            "{out}"
        );
        assert!(out.contains("mdv-wikilink-missing"), "{out}");
    }

    #[test]
    fn finds_attachments_anywhere_in_the_vault() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("attachments/img")).unwrap();
        std::fs::create_dir_all(dir.path().join("notes")).unwrap();
        std::fs::write(dir.path().join("attachments/img/Pic.PNG"), b"x").unwrap();
        let f = files();
        let notes = dir.path().join("notes");
        let c = ctx(dir.path(), &notes, &f, true);
        let out = finish("<p>![[pic.png]]</p>".into(), &c, &no_embed);
        assert!(
            out.contains("<img src=\"/attachments/img/Pic.PNG\""),
            "{out}"
        );
    }

    #[test]
    fn embeds_notes_in_place_of_their_paragraph() {
        let f = files();
        let c = ctx(Path::new("/vault"), Path::new("/vault"), &f, true);
        let embed =
            |rel: &str, heading: Option<&str>| Some(format!("<p>BODY of {rel} {heading:?}</p>"));
        let out = finish(
            "<p data-sourcepos=\"1:1-1:9\">![[Home]]</p>\n<p>Inline ![[Home#Intro]] here</p>"
                .into(),
            &c,
            &embed,
        );
        assert!(out.starts_with("<div class=\"mdv-embed-note\"><div class=\"mdv-embed-title\"><a href=\"/Home.md\">Home</a></div><p>BODY of Home.md None</p></div>\n"), "{out}");
        assert!(
            out.contains("<p>Inline <div class=\"mdv-embed-note\">"),
            "inline embeds stay put: {out}"
        );
        assert!(out.contains("Some(\"Intro\")"));
    }

    #[test]
    fn extracts_sections() {
        let text = "# A\nintro\n## B\nb text\n### B1\nmore\n## C\nc text";
        assert_eq!(section(text, "B").unwrap(), "## B\nb text\n### B1\nmore");
        assert_eq!(section(text, "missing"), None);
    }

    #[test]
    fn strips_source_positions() {
        assert_eq!(
            strip_sourcepos(
                "<p data-sourcepos=\"1:1-2:3\">x <a data-sourcepos=\"1:3-1:4\" href=\"y\">y</a></p>"
            ),
            "<p>x <a href=\"y\">y</a></p>"
        );
    }

    #[test]
    fn finds_backlinks_from_wiki_and_markdown_links() {
        let dir = tempfile::tempdir().unwrap();
        let r = dir.path();
        std::fs::create_dir_all(r.join("notes/sub")).unwrap();
        std::fs::write(r.join("notes/Target.md"), "# Target").unwrap();
        std::fs::write(r.join("notes/a.md"), "intro\nsee [[Target|it]] here").unwrap();
        std::fs::write(r.join("notes/sub/b.md"), "[link](../Target.md#x)").unwrap();
        std::fs::write(r.join("c.md"), "[[Other]] and [web](https://x/Target.md)").unwrap();
        let files: Vec<String> = ["c.md", "notes/Target.md", "notes/a.md", "notes/sub/b.md"]
            .map(String::from)
            .to_vec();
        let hits = backlinks(r, &files, "notes/Target.md");
        let found: Vec<_> = hits.iter().map(|(f, l, _)| (f.as_str(), *l)).collect();
        assert_eq!(found, [("notes/a.md", 2), ("notes/sub/b.md", 1)]);
    }

    #[test]
    fn finds_vaults() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join(".obsidian")).unwrap();
        std::fs::create_dir_all(dir.path().join("notes")).unwrap();
        assert!(in_vault(&dir.path().join("notes")));
        assert_eq!(vault_root(&dir.path().join("notes")).unwrap(), dir.path());
    }
}