Skip to main content

prov_graph/
link.rs

1//! Link text — the raw strings a relation field holds, the wikilinks embedded
2//! in body prose, and the path arithmetic around them.
3//!
4//! A link target as written in metadata is either a bare path or a
5//! markdown-style labeled link (`[Design](docs/design.md)`), and its path may be
6//! **relative** to the document (`notes/a.md`), **workspace-absolute** from the
7//! root (`/Blog/Blog.md`), or wrapped in Markdown **angle brackets** when it
8//! contains spaces (`</Creative Writing/index.md>`, `[Notes](</My Notes/x.md>)`).
9//! This is prov's *link-syntax layer* — the analogue of `fig`'s format
10//! layer: it recognizes the conventions a real workspace mixes and round-trips
11//! them on write (spaces re-acquire their brackets). A [`Wikilink`] is the
12//! body-text counterpart (`[[notes/a.md]]`, `[[colophon:ajp7eq|My file]]`).
13//! Everything here is *lexical*: no filesystem
14//! access, no symlink resolution, and no markdown-structure awareness (a `[[…]]`
15//! inside a code span is still scanned) — resolution and code-fence discipline
16//! belong to the traversal and validation layers, which can report what they
17//! find.
18
19use std::ops::Range;
20use std::path::{Path, PathBuf};
21
22/// A parsed link string: an optional human label and the target it points at.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct Link {
25    /// The display label, when written as `[label](target)` or `[[target|label]]`.
26    pub label: Option<String>,
27    /// The target exactly as written (a relative path, an `id:<id>` handle, or a
28    /// URL for overlay relations that point off-workspace).
29    pub target: String,
30    /// `true` when the scalar was written as an Obsidian wikilink
31    /// (`[[target]]` / `[[target|label]]`) rather than a markdown link or bare
32    /// target — preserved so [`render`](Link::render) round-trips the wrapper.
33    pub wikilink: bool,
34}
35
36impl Link {
37    /// Parse a raw link string. `[label](target)` yields both parts; anything
38    /// else is a bare target with no label. A target wrapped in Markdown angle
39    /// brackets (`<…>`, used when it contains spaces) is unwrapped *only* when
40    /// it appears as the URL portion of a successfully parsed `[label](<target>)`
41    /// — never when it wraps a bare, unlabeled value, which stays byte-literal
42    /// (diaryx reads a bare `<…>` as a literal path, angle brackets and all: see
43    /// [`parse_path_only`](Link::parse_path_only) and the `link_style` render
44    /// tests). A `[[target]]` / `[[target|label]]` Obsidian wikilink scalar is
45    /// also recognized here; use [`parse_path_only`](Link::parse_path_only) when
46    /// the caller's value is a frontmatter *path* field that must not
47    /// reinterpret a literal `[[…]]` string as a wikilink.
48    pub fn parse(raw: &str) -> Self {
49        Self::parse_impl(raw, true)
50    }
51
52    /// [`parse`](Link::parse), but never treats `"[[target]]"` as an Obsidian
53    /// wikilink — such a value is left exactly as written (a bare literal, or,
54    /// if it happens to also match `[label](target)`, a markdown link). This is
55    /// the opt-out a frontmatter *path* field needs: diaryx's own path-value
56    /// parser has no wikilink convention at all, so a workspace that stores a
57    /// literal `"[[…]]"`-shaped string in a path property (unusual, but legal
58    /// input data) must round-trip it untouched rather than have `parse`
59    /// silently reinterpret it as a link. Every other rule of `parse` —
60    /// `[label](target)` splitting, balanced parens, angle-bracket unwrapping of
61    /// a parsed URL — applies unchanged.
62    pub fn parse_path_only(raw: &str) -> Self {
63        Self::parse_impl(raw, false)
64    }
65
66    /// Shared implementation behind [`parse`](Link::parse) and
67    /// [`parse_path_only`](Link::parse_path_only); `wikilink` gates the
68    /// `[[target]]` recognition branch.
69    fn parse_impl(raw: &str, wikilink: bool) -> Self {
70        let raw = raw.trim();
71        // A wikilink scalar — `[[target]]` / `[[target|label]]` — the Obsidian
72        // wrapper permitted in metadata as well as body prose. Skipped entirely
73        // by `parse_path_only`.
74        if wikilink && let Some(inner) = raw.strip_prefix("[[").and_then(|r| r.strip_suffix("]]")) {
75            let (target, label) = match inner.split_once('|') {
76                Some((target, label)) => (target.trim(), Some(label.trim().to_string())),
77                None => (inner.trim(), None),
78            };
79            return Self {
80                label,
81                target: target.to_string(),
82                wikilink: true,
83            };
84        }
85        if let Some((label, target)) = split_markdown_link(raw) {
86            return Self {
87                label: Some(label),
88                target,
89                wikilink: false,
90            };
91        }
92        // Bare value: stored byte-literal, angle brackets and all. Unlike the
93        // markdown-link branch above, there is no URL position here to unwrap —
94        // a bare `<…>`-shaped string is data, not delimiters (see `parse`'s doc
95        // comment and the C2 regression tests).
96        Self {
97            label: None,
98            target: raw.to_string(),
99            wikilink: false,
100        }
101    }
102
103    /// Render back to a writable link string. A labeled link keeps its label and
104    /// wraps the URL in Markdown angle brackets when it holds a space or paren
105    /// (so `]` / `)` in the path cannot break parsing); a bare target is emitted
106    /// verbatim — brackets belong *inside* `[label](…)`, never around a bare
107    /// value (matching diaryx, which reads a bare `<…>` as a literal path).
108    pub fn render(&self) -> String {
109        match (&self.label, self.wikilink) {
110            (Some(label), true) => format!("[[{}|{label}]]", self.target),
111            (None, true) => format!("[[{}]]", self.target),
112            (Some(label), false) => format!("[{label}]({})", emit_target(&self.target)),
113            (None, false) => self.target.clone(),
114        }
115    }
116
117    /// This link with a different target, keeping the label and wrapper. The
118    /// rename path uses this so `[Design](old.md)` becomes `[Design](new.md)`,
119    /// never a bare `new.md`.
120    pub fn with_target(&self, target: impl Into<String>) -> Self {
121        Self {
122            label: self.label.clone(),
123            target: target.into(),
124            wikilink: self.wikilink,
125        }
126    }
127
128    /// This link with a different display label, keeping the target and wrapper.
129    /// The retitle path uses this so `[Old Title](id:abc)` becomes
130    /// `[New Title](id:abc)` when the target is renamed — the label follows the
131    /// title while the (id or path) target stays exactly as written.
132    pub fn with_label(&self, label: impl Into<String>) -> Self {
133        Self {
134            label: Some(label.into()),
135            target: self.target.clone(),
136            wikilink: self.wikilink,
137        }
138    }
139
140    /// `true` when the target points off-workspace (a URL or mail address)
141    /// rather than at a file — such links are never resolved against the
142    /// filesystem or rewritten by moves.
143    pub fn is_external(&self) -> bool {
144        self.target.contains("://") || self.target.starts_with("mailto:")
145    }
146
147    /// The **sub-document locator** this target carries — the text after a `#`,
148    /// naming a place *inside* a document rather than a document.
149    ///
150    /// A locator is carried, never resolved. prov strips it before resolving the
151    /// target and re-attaches it on rewrite, which is the same contract §4 gives
152    /// an external URL: recognized by syntax, never validated. What it *means*
153    /// is the workspace's business — a verse number in a chapter, a heading
154    /// slug, a line range — so a locator naming nothing is not a `check`
155    /// finding. That is the price of not teaching prov every document format's
156    /// internal address space.
157    ///
158    /// `None` for an external target (a URL's fragment belongs to the URL) and
159    /// for a target that is *only* a locator (`#3`), which stays byte-literal —
160    /// see [`split_locator`].
161    pub fn locator(&self) -> Option<&str> {
162        if self.is_external() {
163            return None;
164        }
165        split_locator(&self.target).1
166    }
167
168    /// This link's target with any [`locator`](Self::locator) removed — the part
169    /// that names a *document*, and so the only part that resolves.
170    pub fn addressed_target(&self) -> &str {
171        if self.is_external() {
172            return &self.target;
173        }
174        split_locator(&self.target).0
175    }
176
177    /// This link with its document part replaced, **preserving the locator**.
178    ///
179    /// The rewrite passes (rename, re-relativize, restyle) use this rather than
180    /// [`with_target`](Self::with_target), which sets the target verbatim: a
181    /// move changes where a document lives, never which part of it was pointed
182    /// at.
183    pub fn with_path(&self, path: impl Into<String>) -> Self {
184        self.with_target(join_locator(path, self.locator()))
185    }
186
187    /// What this link's `id:`-scheme target names — local, foreign, or
188    /// malformed. `None` when the target carries no id scheme at all (a path,
189    /// an alias, a URL).
190    ///
191    /// Any [`locator`](Self::locator) is stripped first, so `id:abc1234#2-3`
192    /// names the same document as `id:abc1234`.
193    pub fn id_ref(&self) -> Option<IdRef> {
194        strip_id_scheme(self.addressed_target()).map(parse_id_body)
195    }
196
197    /// The stable ID this link names, when the target uses the `id:<id>`
198    /// scheme (or the legacy `colophon:<id>` spelling) — the
199    /// location-independent alternative to a relative path. Such targets
200    /// resolve through the workspace's ID registry, never against the
201    /// filesystem, and are deliberately *not* rewritten by moves: staying valid
202    /// across moves is their entire point.
203    ///
204    /// **Local ids only.** A cross-workspace `id:<workspace>/<id>` yields
205    /// `None`, because the registry this would be resolved against is not the
206    /// one that issued the id — see [`id_ref`](Self::id_ref). Callers asking
207    /// "must a move leave this alone?" want [`is_path_target`](Self::is_path_target),
208    /// which covers every id form.
209    pub fn id_target(&self) -> Option<crate::identity::Id> {
210        match self.id_ref() {
211            Some(IdRef::Local(id)) => Some(id),
212            _ => None,
213        }
214    }
215
216    /// The workspace and id this link names, when it is a cross-workspace
217    /// reference (`id:<workspace>/<id>`).
218    pub fn foreign_target(&self) -> Option<(String, crate::identity::Id)> {
219        match self.id_ref() {
220            Some(IdRef::Foreign { workspace, id }) => Some((workspace, id)),
221            _ => None,
222        }
223    }
224
225    /// Whether this link's target is a **path** — the only kind that says where
226    /// its target lives, and so the only kind a move may rewrite.
227    ///
228    /// False for an external URL and for every `id:` reference alike (local,
229    /// foreign, and malformed): none of them encodes a location, so
230    /// re-relativizing one could only damage it. This is the predicate the
231    /// rename, re-relativize and restyle passes filter on.
232    pub fn is_path_target(&self) -> bool {
233        !self.is_external() && self.id_ref().is_none()
234    }
235}
236
237/// Try to split `raw` as a Markdown link `[label](target)` (or, when the URL
238/// holds a space or paren, `[label](<target>)`). Returns the label and the
239/// unwrapped target, or `None` when `raw` doesn't have this shape.
240///
241/// Ports diaryx_core's `link_parser::try_parse_markdown_link` byte-for-byte
242/// (see module doc comment) rather than re-deriving it, because its two
243/// corrected behaviors are exactly what C2/C3 need and diaryx's test suite is
244/// the ground truth for their edge cases:
245/// - The label is whatever sits between the first `[` and the first `]`
246///   immediately followed by `(` — *not* whatever precedes the last `)` in the
247///   whole string, so trailing prose after the link (`"[Title](/a.md) note"`)
248///   never gets swept into the target.
249/// - The target's closing paren is found by depth-counting (see
250///   [`find_closing_paren`]), so a target containing its own parens
251///   (`/file (1).md`, even nested) still closes at the right `)`; any text
252///   after that `)` is deliberately never inspected — tolerated, not merely
253///   permitted.
254/// - Angle brackets are only unwrapped here, on the URL of a link that already
255///   parsed as `[label](…)` — never on a bare value (that's C2; see
256///   [`Link::parse`]'s doc comment and `parse_impl`'s bare-value branch).
257fn split_markdown_link(raw: &str) -> Option<(String, String)> {
258    if !raw.starts_with('[') {
259        return None;
260    }
261    let close_bracket = raw.find(']')?;
262    if !raw[close_bracket..].starts_with("](") {
263        return None;
264    }
265    let label = raw[1..close_bracket].to_string();
266    let after = &raw[close_bracket + 2..];
267    let target = if let Some(inner) = after.strip_prefix('<') {
268        // `](<target>)`: the closing `>` must be immediately followed by `)` —
269        // otherwise this isn't really an angle-bracketed URL and the whole
270        // markdown-link parse fails (falls through to the bare branch).
271        let close_angle = inner.find('>')?;
272        if inner.get(close_angle + 1..close_angle + 2) != Some(")") {
273            return None;
274        }
275        inner[..close_angle].to_string()
276    } else {
277        let close_paren = find_closing_paren(after)?;
278        after[..close_paren].to_string()
279    };
280    Some((label, target))
281}
282
283/// Find the byte offset of the `)` that balances the *implicit* open paren at
284/// the start of a Markdown link URL — i.e. the first `)` encountered at
285/// nesting depth zero, treating every `(` as opening one more level. A target
286/// with no closing paren at all (an unterminated link) yields `None`.
287///
288/// This is the crux of the C3 fix: the old code demanded the link be the very
289/// end of the input (`raw.strip_suffix(')')` on the whole trimmed string), so
290/// `"[Title](/a.md) trailing junk"` fell through to a bare target holding the
291/// entire string. Scanning for the *matching* close paren instead — and never
292/// examining what follows it — makes the split correct both for a target
293/// containing its own balanced parens (`/file (1).md`, `/file (a (b)).md`) and
294/// for trailing prose after the link.
295fn find_closing_paren(s: &str) -> Option<usize> {
296    let mut depth = 0u32;
297    for (i, c) in s.char_indices() {
298        match c {
299            '(' => depth += 1,
300            ')' => {
301                if depth == 0 {
302                    return Some(i);
303                }
304                depth -= 1;
305            }
306            _ => {}
307        }
308    }
309    None
310}
311
312/// The writable spelling of a Markdown-link URL: wrapped in angle brackets when
313/// it holds a space or parenthesis (which would otherwise break `[label](url)`),
314/// bare otherwise. For URLs *inside* `[label](…)` only.
315fn emit_target(target: &str) -> String {
316    if target.contains([' ', '(', ')']) {
317        format!("<{target}>")
318    } else {
319        target.to_string()
320    }
321}
322
323/// The write style for links a workspace authors — prov's analogue of
324/// diaryx's `LinkFormat`, and read from the same place: the `link_format` key in
325/// the root document's frontmatter (a fact declared *in* the workspace, not an
326/// app-private config). Every link prov writes (autofix today; create/rename
327/// in time) uses this, so a repair never introduces a foreign style.
328#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
329pub enum LinkStyle {
330    /// `[Title](/path/file.md)` — workspace-absolute Markdown link. diaryx's
331    /// default, and the most portable/self-documenting.
332    #[default]
333    MarkdownRoot,
334    /// `[Title](../path/file.md)` — relative Markdown link.
335    MarkdownRelative,
336    /// `/path/file.md` — bare workspace-absolute path.
337    PlainRoot,
338    /// `../path/file.md` — bare relative path.
339    PlainRelative,
340}
341
342impl LinkStyle {
343    /// This style's notation (bracketed Markdown vs bare path) and path
344    /// resolution — the two orthogonal axes the config `references.notation` /
345    /// `references.path_style` keys expose (see [`Notation`] / [`PathStyle`]).
346    /// `LinkStyle` is the fused internal carrier; these split it back out.
347    pub fn axes(self) -> (Notation, PathStyle) {
348        match self {
349            Self::MarkdownRoot => (Notation::Markdown, PathStyle::Root),
350            Self::MarkdownRelative => (Notation::Markdown, PathStyle::Relative),
351            Self::PlainRoot => (Notation::Bare, PathStyle::Root),
352            Self::PlainRelative => (Notation::Bare, PathStyle::Relative),
353        }
354    }
355
356    /// The fused [`LinkStyle`] for a bracketed-vs-bare notation and a path
357    /// resolution. `Wikilink` has no bare/bracketed distinction, so it maps
358    /// through the Markdown family (only the path-text shape matters for it).
359    pub fn from_axes(notation: Notation, path_style: PathStyle) -> Self {
360        match (notation, path_style) {
361            (Notation::Markdown | Notation::Wikilink, PathStyle::Root) => Self::MarkdownRoot,
362            (Notation::Markdown | Notation::Wikilink, PathStyle::Relative) => {
363                Self::MarkdownRelative
364            }
365            (Notation::Bare, PathStyle::Root) => Self::PlainRoot,
366            (Notation::Bare, PathStyle::Relative) => Self::PlainRelative,
367        }
368    }
369}
370
371/// The syntactic form a reference is written in — the config-facing notation
372/// axis (`references.notation`), orthogonal to [`PathStyle`]. This is the clean
373/// split of what the internal [`Wrapper`] + `plain_`/`markdown_` [`LinkStyle`]
374/// prefix fused: `Bare` is a path with no brackets, `Markdown` is `[Title](…)`,
375/// `Wikilink` is `[[…]]`.
376#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
377pub enum Notation {
378    /// `[Title](target)`.
379    #[default]
380    Markdown,
381    /// `[[target]]` / `[[target|Title]]`.
382    Wikilink,
383    /// A bare `target`, no brackets — what the old `plain_*` link formats wrote.
384    Bare,
385}
386
387impl Notation {
388    /// Parse the `references.notation` config spelling; unknown → `None`.
389    pub fn from_config_str(value: &str) -> Option<Self> {
390        match value {
391            "markdown" => Some(Self::Markdown),
392            "wikilink" => Some(Self::Wikilink),
393            "bare" => Some(Self::Bare),
394            _ => None,
395        }
396    }
397
398    /// The `references.notation` config spelling.
399    pub fn as_config_str(self) -> &'static str {
400        match self {
401            Self::Markdown => "markdown",
402            Self::Wikilink => "wikilink",
403            Self::Bare => "bare",
404        }
405    }
406
407    /// The internal [`Wrapper`] this notation renders through. `Markdown` and
408    /// `Bare` share the Markdown wrapper (the bracket-vs-bare choice lives in the
409    /// path style); `Wikilink` is its own wrapper.
410    pub fn wrapper(self) -> Wrapper {
411        match self {
412            Self::Wikilink => Wrapper::Wikilink,
413            Self::Markdown | Self::Bare => Wrapper::Markdown,
414        }
415    }
416
417    /// Recover the notation from a fused [`Wrapper`] + [`LinkStyle`] — the inverse
418    /// direction, for serializing an internal style back to config.
419    pub fn from_wrapper(wrapper: Wrapper, style: LinkStyle) -> Self {
420        match wrapper {
421            Wrapper::Wikilink => Self::Wikilink,
422            Wrapper::Markdown => style.axes().0,
423        }
424    }
425}
426
427/// The path-resolution a reference uses for a path target — the config-facing
428/// `references.path_style` axis, orthogonal to [`Notation`]. Applies to path
429/// targets only (id/alias ignore it).
430///
431/// Two resolutions, and deliberately not three. A `canonical` style once emitted
432/// a *workspace*-relative path with no leading slash (`path/file.md`), which
433/// [`resolve`] reads as *directory*-relative — so a canonical link resolved to
434/// what it named only from a document at the workspace root, and silently
435/// misresolved everywhere else. The ambiguity of a bare path is settled by
436/// committing to one meaning rather than by tagging it: **bare is
437/// directory-relative**, and a workspace-relative reference is spelled
438/// [`Root`](Self::Root), with the slash that says so. `prov convert <root>
439/// link_format markdown_root -r` restyles a workspace that used the old value.
440#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
441pub enum PathStyle {
442    /// Workspace-absolute: `/path/file.md`.
443    #[default]
444    Root,
445    /// Relative to the referring document: `../file.md`.
446    Relative,
447}
448
449impl PathStyle {
450    /// Parse the `references.path_style` config spelling; unknown → `None`.
451    pub fn from_config_str(value: &str) -> Option<Self> {
452        match value {
453            "root" => Some(Self::Root),
454            "relative" => Some(Self::Relative),
455            _ => None,
456        }
457    }
458
459    /// The `references.path_style` config spelling.
460    pub fn as_config_str(self) -> &'static str {
461        match self {
462            Self::Root => "root",
463            Self::Relative => "relative",
464        }
465    }
466}
467
468/// Format a link to `target` (a workspace-relative canonical path) as written in
469/// the document at `from`, in `style`, with `title` (used only by the Markdown
470/// styles). This is what keeps an authored link native to the workspace.
471pub fn format_link(style: LinkStyle, from: &Path, target: &Path, title: &str) -> String {
472    let canonical = target.to_string_lossy();
473    match style {
474        LinkStyle::MarkdownRoot => {
475            format!("[{title}]({})", emit_target(&format!("/{canonical}")))
476        }
477        LinkStyle::MarkdownRelative => {
478            let rel = relative(from.parent().unwrap_or(Path::new("")), target);
479            format!("[{title}]({})", emit_target(&rel))
480        }
481        LinkStyle::PlainRoot => format!("/{canonical}"),
482        LinkStyle::PlainRelative => relative(from.parent().unwrap_or(Path::new("")), target),
483    }
484}
485
486/// A human title generated from a path's file stem: `_`/`-` become spaces and
487/// each word is capitalized (`utility_index.md` → `Utility Index`). The fallback
488/// when a target document declares no `title`.
489pub fn path_to_title(path: &Path) -> String {
490    let stem = path
491        .file_stem()
492        .and_then(|s| s.to_str())
493        .unwrap_or_default();
494    stem.split(['_', '-', ' '])
495        .filter(|w| !w.is_empty())
496        .map(|word| {
497            let mut chars = word.chars();
498            match chars.next() {
499                Some(first) => first.to_ascii_uppercase().to_string() + chars.as_str(),
500                None => String::new(),
501            }
502        })
503        .collect::<Vec<_>>()
504        .join(" ")
505}
506
507/// Turn a human title into a filesystem-friendly filename stem — the readable
508/// slug prov derives when a document is created by title (`prov new "My
509/// Great Note"` → `my-great-note`). The rough inverse of
510/// [`path_to_title`]: lowercase, runs of whitespace and separators
511/// (space / `-` / `_` / `/`) collapsed to a single `-`, and any other
512/// punctuation dropped. Unicode letters and digits are kept, so a non-ASCII
513/// title still yields a legible name. A title with no slug-able characters (pure
514/// punctuation) falls back to `"untitled"` so the result is always a valid stem.
515///
516/// The point is prov's legibility contract (DESIGN §1): a title-first
517/// authoring flow that still leaves *readable paths* in the tree and in
518/// path-addressed links — unlike an opaque `note-3.md`.
519pub fn slug(title: &str) -> String {
520    let mut out = String::with_capacity(title.len());
521    let mut pending_dash = false;
522    for ch in title.chars() {
523        if ch.is_alphanumeric() {
524            if pending_dash {
525                out.push('-');
526                pending_dash = false;
527            }
528            out.extend(ch.to_lowercase());
529        } else if ch.is_whitespace() || matches!(ch, '-' | '_' | '/') {
530            // Defer the separator: only emitted if a kept character follows, so
531            // leading, trailing, and repeated separators never reach `out`.
532            pending_dash = !out.is_empty();
533        }
534        // Any other character (punctuation, symbols) is dropped.
535    }
536    if out.is_empty() {
537        "untitled".to_string()
538    } else {
539        out
540    }
541}
542
543/// The character that begins a [sub-document locator](Link::locator) on a link
544/// target.
545pub const LOCATOR_SEPARATOR: char = '#';
546
547/// Split a target into the part naming a document and its locator.
548///
549/// Splits at the **first** separator, so a locator may itself contain one. Two
550/// targets deliberately have no locator:
551///
552/// - one with no `#` at all — the ordinary case;
553/// - one whose `#` is the *first* character (`#3`). That is a same-document
554///   reference, and reading it as a locator on an empty path would resolve the
555///   link to the containing *directory* — a silent retarget where doing nothing
556///   is correct.
557///
558/// Purely syntactic: callers that must not split an external URL's fragment
559/// guard with [`Link::is_external`] first, as [`Link::locator`] does.
560///
561/// The cost of the convention is a document whose *filename* contains `#`,
562/// which can no longer be linked by path. That is the same trade every URL and
563/// Markdown implementation makes, and the character is rare in filenames where
564/// a locator is not.
565pub fn split_locator(target: &str) -> (&str, Option<&str>) {
566    match target.split_once(LOCATOR_SEPARATOR) {
567        Some((doc, locator)) if !doc.is_empty() => (doc, Some(locator)),
568        _ => (target, None),
569    }
570}
571
572/// Re-attach a locator to a document target — the inverse of [`split_locator`].
573/// `None` returns the target unchanged, so this is safe on a target that never
574/// had one.
575pub fn join_locator(target: impl Into<String>, locator: Option<&str>) -> String {
576    let mut target = target.into();
577    if let Some(locator) = locator {
578        target.push(LOCATOR_SEPARATOR);
579        target.push_str(locator);
580    }
581    target
582}
583
584/// The target scheme marking a link-by-ID: `id:<id>`.
585pub const ID_SCHEME: &str = "id:";
586
587/// The legacy scheme (`colophon:<id>`), still recognized on read so existing
588/// workspaces keep resolving. New links are authored with [`ID_SCHEME`].
589pub const LEGACY_ID_SCHEME: &str = "colophon:";
590
591/// Strip the ID scheme from a target, accepting the current `id:` spelling or
592/// the legacy `colophon:` one. `None` when the target names no ID.
593pub fn strip_id_scheme(target: &str) -> Option<&str> {
594    target
595        .strip_prefix(ID_SCHEME)
596        .or_else(|| target.strip_prefix(LEGACY_ID_SCHEME))
597}
598
599/// Render an ID as a link target (`id:<id>`).
600pub fn id_target(id: &crate::identity::Id) -> String {
601    format!("{ID_SCHEME}{id}")
602}
603
604/// The character separating the workspace qualifier from the id in a
605/// cross-workspace reference (`id:<workspace>/<id>`).
606pub const WORKSPACE_SEPARATOR: char = '/';
607
608/// Render a cross-workspace reference as a link target (`id:<workspace>/<id>`).
609pub fn foreign_id_target(workspace: &str, id: &crate::identity::Id) -> String {
610    format!("{ID_SCHEME}{workspace}{WORKSPACE_SEPARATOR}{id}")
611}
612
613/// Whether `name` is a usable workspace self-name.
614///
615/// The constraint comes entirely from where the name is *written*: it is the
616/// qualifier in an `id:<workspace>/<id>` target, so it may not contain the
617/// [`WORKSPACE_SEPARATOR`] that divides it from the id, the `:` that ends the
618/// scheme, or whitespace (a target is a single scalar; a space would make it
619/// two). Anything else is the user's business — this is a name for humans to
620/// *choose*. [`identity::mint_workspace_id`](crate::identity::mint_workspace_id) (reached by `prov id
621/// --workspace`) can mint an opaque one for an owner with no naming authority
622/// to lean on, but only when asked: a minted name satisfies this predicate like
623/// any other, and nothing here can tell the two apart.
624///
625/// It lives beside the grammar it is a constraint on rather than in
626/// `prov-config` with the [`workspace_id`] key, because it is not a policy
627/// choice — every clause above is dictated by how [`IdRef`] parses, and a config
628/// layer that spelled its own version of this could drift from the parser that
629/// decides what a reference actually means.
630///
631/// Deliberately *not* checked: uniqueness across workspaces. Nothing here can
632/// see another workspace, so a collision is undetectable from inside; it is the
633/// resolving host's problem, and the host is the only thing with the evidence to
634/// notice ([`crate::peer`]). A minted name buys its uniqueness with width
635/// instead — the only currency available to something that cannot check.
636///
637/// [`workspace_id`]: crate::graph::ReadSettings::workspace_id
638pub fn is_valid_workspace_id(name: &str) -> bool {
639    !name.is_empty()
640        && !name
641            .chars()
642            .any(|c| c == WORKSPACE_SEPARATOR || c == ':' || c.is_whitespace())
643}
644
645/// What an `id:`-scheme target names.
646///
647/// The scheme carries three distinguishable things, and keeping them apart is
648/// the whole point: a reference prov can resolve, one it deliberately cannot,
649/// and one that is broken. See `docs/reference-styles.md`.
650#[derive(Debug, Clone, PartialEq, Eq)]
651pub enum IdRef {
652    /// `id:<id>` — a document in *this* workspace, resolved through the registry.
653    Local(crate::identity::Id),
654    /// `id:<workspace>/<id>` — a document in the workspace named `workspace`.
655    ///
656    /// prov resolves this only when `workspace` is the reading workspace's own
657    /// [`workspace_id`](crate::graph::ReadSettings::workspace_id), in which case it
658    /// *is* local and is treated as such. Any other name is somewhere prov
659    /// cannot see: the library holds no map from a workspace name to a location
660    /// (that is a fact about a device, not about an archive), so the reference is
661    /// carried, never rewritten, and never reported broken.
662    Foreign {
663        /// The workspace qualifier, exactly as written.
664        workspace: String,
665        /// The id, exactly as written. **Not** check-verified: the foreign
666        /// workspace owns its id space and need not be a prov workspace at all
667        /// (a diaryx ARK blade is a different length and alphabet), so applying
668        /// prov's check character here would reject valid references.
669        id: crate::identity::Id,
670    },
671    /// The `id:` scheme with a body that is no reference at all — an empty half
672    /// (`id:`, `id:/x`, `id:ws/`) or more than one separator (`id:a/b/c`).
673    ///
674    /// Deliberately its own case rather than falling through to a path: the
675    /// author wrote `id:`, so silently resolving the text as a filename would
676    /// turn a typo into a dangling path and hide what actually went wrong.
677    Malformed,
678}
679
680/// Parse the body of an `id:`-scheme target (the text after the scheme).
681fn parse_id_body(body: &str) -> IdRef {
682    let Some((workspace, id)) = body.split_once(WORKSPACE_SEPARATOR) else {
683        return if body.is_empty() {
684            IdRef::Malformed
685        } else {
686            IdRef::Local(crate::identity::Id(body.to_string()))
687        };
688    };
689    if workspace.is_empty() || id.is_empty() || id.contains(WORKSPACE_SEPARATOR) {
690        return IdRef::Malformed;
691    }
692    IdRef::Foreign {
693        workspace: workspace.to_string(),
694        id: crate::identity::Id(id.to_string()),
695    }
696}
697
698/// The syntactic wrapper a reference is written in — the first of the two style
699/// axes (see `docs/reference-styles.md`).
700#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
701pub enum Wrapper {
702    /// The diaryx/markdown family: `[Title](target)` or a bare target, with the
703    /// exact path rendering governed by [`ReferenceStyle::path_style`].
704    #[default]
705    Markdown,
706    /// The Obsidian wikilink: `[[target]]` / `[[target|label]]`.
707    Wikilink,
708}
709
710impl Wrapper {
711    /// Parse the `reference_wrapper` config spelling; unknown → `None`.
712    pub fn from_config_str(value: &str) -> Option<Self> {
713        match value {
714            "markdown" => Some(Self::Markdown),
715            "wikilink" => Some(Self::Wikilink),
716            _ => None,
717        }
718    }
719
720    /// The `reference_wrapper` config spelling.
721    pub fn as_config_str(self) -> &'static str {
722        match self {
723            Self::Markdown => "markdown",
724            Self::Wikilink => "wikilink",
725        }
726    }
727}
728
729/// What a reference addresses its target *by* — the second style axis.
730#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
731pub enum Addressing {
732    /// By path — rewritten on every move; rendering follows
733    /// [`ReferenceStyle::path_style`].
734    #[default]
735    Path,
736    /// By durable `id:<id>` handle — move-stable; authoring one registers the
737    /// target (the link-by-id trigger).
738    Id,
739    /// By the target's title/name, resolved nominally through the title index —
740    /// readable but not move/rename-safe, and never registers. Implies
741    /// [`Wrapper::Wikilink`].
742    Alias,
743}
744
745impl Addressing {
746    /// Parse the `reference_target` config spelling; unknown → `None`.
747    pub fn from_config_str(value: &str) -> Option<Self> {
748        match value {
749            "path" => Some(Self::Path),
750            "id" => Some(Self::Id),
751            "alias" => Some(Self::Alias),
752            _ => None,
753        }
754    }
755
756    /// The `reference_target` config spelling.
757    pub fn as_config_str(self) -> &'static str {
758        match self {
759            Self::Path => "path",
760            Self::Id => "id",
761            Self::Alias => "alias",
762        }
763    }
764}
765
766/// How a durable reference is spelled: a [`Wrapper`], an [`Addressing`], whether
767/// an `id` link carries a title label, and the path rendering used when
768/// addressing by path. This is the per-workspace default *and* the per-relation
769/// override (see [`crate::relation::Relation::style`]).
770#[derive(Debug, Clone, Copy, PartialEq, Eq)]
771pub struct ReferenceStyle {
772    /// The syntactic wrapper.
773    pub wrapper: Wrapper,
774    /// What the reference addresses its target by.
775    pub addressing: Addressing,
776    /// Whether an `id` wikilink carries a `|Title` label (a maintained cache of
777    /// the target's title). Ignored for markdown (its `[Title]` is intrinsic)
778    /// and for `alias` (the target string *is* the title).
779    pub label: bool,
780    /// Path rendering for [`Addressing::Path`] — ignored otherwise.
781    pub path_style: LinkStyle,
782}
783
784impl Default for ReferenceStyle {
785    /// Markdown path links in the default [`LinkStyle`] — the pre-existing
786    /// behavior, so an unconfigured workspace is unchanged.
787    fn default() -> Self {
788        Self {
789            wrapper: Wrapper::Markdown,
790            addressing: Addressing::Path,
791            label: false,
792            path_style: LinkStyle::default(),
793        }
794    }
795}
796
797impl ReferenceStyle {
798    /// Normalize impossible combinations: `alias` has no markdown spelling (there
799    /// is no locator to put in `[Title](…)`), so a markdown+alias request becomes
800    /// wikilink+alias.
801    pub fn normalized(mut self) -> Self {
802        if self.addressing == Addressing::Alias {
803            self.wrapper = Wrapper::Wikilink;
804        }
805        self
806    }
807
808    /// Whether authoring in this style is a *link-by-id* — the trigger that
809    /// registers the target. Only `id` addressing registers.
810    pub fn registers(self) -> bool {
811        self.addressing == Addressing::Id
812    }
813}
814
815/// Render a durable reference from the document at `from` to `to` (titled
816/// `title`) in `style`. `id` must be `Some` when the style addresses by id
817/// (the caller registers the target first); it is ignored otherwise. Returns the
818/// exact scalar to store in metadata (a wikilink scalar keeps its `[[…]]` — the
819/// metadata writer is responsible for any format-level quoting).
820pub fn format_reference(
821    style: ReferenceStyle,
822    from: &Path,
823    to: &Path,
824    id: Option<&crate::identity::Id>,
825    title: &str,
826) -> String {
827    let style = style.normalized();
828    match style.addressing {
829        // No id available (identity off / does not register on link) degrades to
830        // a path link, mirroring the pre-existing `authored_target` fallback.
831        Addressing::Id => match id {
832            Some(id) => wrap(style.wrapper, &id_target(id), title, style.label),
833            None => format_path(
834                Wrapper::Markdown,
835                style.path_style,
836                from,
837                to,
838                title,
839                style.label,
840            ),
841        },
842        Addressing::Alias => wrap_alias(title),
843        Addressing::Path => format_path(
844            style.wrapper,
845            style.path_style,
846            from,
847            to,
848            title,
849            style.label,
850        ),
851    }
852}
853
854/// Render a path reference in `wrapper` at `path_style`.
855fn format_path(
856    wrapper: Wrapper,
857    path_style: LinkStyle,
858    from: &Path,
859    to: &Path,
860    title: &str,
861    label: bool,
862) -> String {
863    match wrapper {
864        // Preserve the exact markdown/plain behavior (labeled vs bare) that
865        // `LinkStyle` already encodes — the `label` axis does not apply here.
866        Wrapper::Markdown => format_link(path_style, from, to, title),
867        Wrapper::Wikilink => wrap(
868            Wrapper::Wikilink,
869            &path_text(path_style, from, to),
870            title,
871            label,
872        ),
873    }
874}
875
876/// The bare path *text* a path reference points at, in the shape `path_style`
877/// selects: workspace-absolute (`/canonical`), relative, or canonical.
878pub fn path_text(path_style: LinkStyle, from: &Path, to: &Path) -> String {
879    match path_style {
880        LinkStyle::MarkdownRoot | LinkStyle::PlainRoot => format!("/{}", to.to_string_lossy()),
881        LinkStyle::MarkdownRelative | LinkStyle::PlainRelative => {
882            relative(from.parent().unwrap_or(Path::new("")), to)
883        }
884    }
885}
886
887/// Wrap a resolved `target` (already scheme-/path-formatted) in `wrapper`,
888/// attaching `title` as a label when `with_label`. A markdown reference without
889/// a label is emitted bare (`id:xxx`) — the diaryx-shaped id link.
890fn wrap(wrapper: Wrapper, target: &str, title: &str, with_label: bool) -> String {
891    match (wrapper, with_label) {
892        (Wrapper::Wikilink, true) => format!("[[{target}|{title}]]"),
893        (Wrapper::Wikilink, false) => format!("[[{target}]]"),
894        (Wrapper::Markdown, true) => format!("[{title}]({})", emit_target(target)),
895        (Wrapper::Markdown, false) => emit_target(target),
896    }
897}
898
899/// An alias reference: the title itself, as a bare-name wikilink.
900fn wrap_alias(title: &str) -> String {
901    format!("[[{title}]]")
902}
903
904/// A wikilink embedded in a document's body: `[[target]]` or, with an Obsidian
905/// pipe label, `[[target|label]]`.
906///
907/// This is the body-text sibling of a metadata [`Link`]. The `target` is either
908/// a path (`[[notes/a.md]]`, the identity-free Diaryx-style link that moves
909/// rewrite) or a `colophon:<id>` reference (`[[colophon:ajp7eq]]`, the
910/// location-independent Obsidian-style link that moves leave alone — the
911/// registry update is its maintenance). Which one a workspace mints is a policy
912/// choice; discovering the span is not, so the scanner is neutral between them.
913///
914/// [`span`](Wikilink::span) is the byte range of the whole `[[…]]` construct in
915/// the source body — exactly what a rewrite replaces, so a path retarget can
916/// splice a new target back in without re-parsing the prose around it.
917#[derive(Debug, Clone, PartialEq, Eq)]
918pub struct Wikilink {
919    /// The target as written between `[[` and the `|` (or the closing `]]`),
920    /// trimmed of surrounding whitespace.
921    pub target: String,
922    /// The display label after `|`, when written `[[target|label]]`.
923    pub label: Option<String>,
924    /// Byte range of the entire `[[…]]` span within the scanned body.
925    pub span: Range<usize>,
926}
927
928impl Wikilink {
929    /// Build a wikilink from the raw inner text (between `[[` and `]]`) and its
930    /// span. Splits an Obsidian `target|label` on the first `|`; an empty target
931    /// (e.g. `[[]]` or `[[ | x ]]`) is not a link and yields `None`.
932    fn from_inner(inner: &str, span: Range<usize>) -> Option<Self> {
933        let (target, label) = match inner.split_once('|') {
934            Some((target, label)) => (target.trim(), Some(label.trim().to_string())),
935            None => (inner.trim(), None),
936        };
937        if target.is_empty() {
938            return None;
939        }
940        Some(Self {
941            target: target.to_string(),
942            label,
943            span,
944        })
945    }
946
947    /// Render back to `[[target]]` / `[[target|label]]`. Surrounding whitespace
948    /// inside the brackets is not preserved — the rendered form is canonical.
949    pub fn render(&self) -> String {
950        match &self.label {
951            Some(label) => format!("[[{}|{label}]]", self.target),
952            None => format!("[[{}]]", self.target),
953        }
954    }
955
956    /// This wikilink with a different target, keeping the label — the move path
957    /// uses it to rewrite a *path* target while leaving the display text intact.
958    /// (ID targets are never rewritten; that is the whole point of using one.)
959    pub fn with_target(&self, target: impl Into<String>) -> Self {
960        Self {
961            target: target.into(),
962            label: self.label.clone(),
963            span: self.span.clone(),
964        }
965    }
966
967    /// The stable ID this wikilink names, when its target uses the `id:<id>`
968    /// scheme (or the legacy `colophon:<id>` spelling) — `None` for a plain path
969    /// target. Mirrors [`Link::id_target`], including its locator handling:
970    /// `[[id:abc1234#2]]` names the document `abc1234`.
971    pub fn id_target(&self) -> Option<crate::identity::Id> {
972        strip_id_scheme(split_locator(&self.target).0).map(|id| crate::identity::Id(id.to_string()))
973    }
974}
975
976/// Scan body prose for every `[[…]]` wikilink, in source order, each carrying
977/// its byte span. Purely lexical: unclosed `[[` is ignored, the first following
978/// `]]` closes the span, and no markdown structure (code spans, escapes) is
979/// interpreted — a higher layer decides whether a match in a code fence counts.
980pub fn parse_wikilinks(body: &str) -> Vec<Wikilink> {
981    let mut out = Vec::new();
982    let mut base = 0; // byte offset of `rest` within `body`
983    let mut rest = body;
984    while let Some(open_rel) = rest.find("[[") {
985        let open = base + open_rel;
986        let after_open = open + 2;
987        let Some(close_rel) = body[after_open..].find("]]") else {
988            break; // no closing delimiter anywhere ahead — nothing more to find
989        };
990        let close = after_open + close_rel;
991        if let Some(link) = Wikilink::from_inner(&body[after_open..close], open..close + 2) {
992            out.push(link);
993        }
994        base = close + 2;
995        rest = &body[base..];
996    }
997    out
998}
999
1000/// Keep only the wikilinks in `links` whose span does not overlap any of
1001/// `code_spans` — the code-awareness DESIGN §8 asks for: a `[[…]]` that is
1002/// really code (inside a fenced/inline code span) must never be treated as a
1003/// link.
1004///
1005/// **Caveat:** this only helps for a `links` list in which every real
1006/// wikilink was already found as its own match. It cannot rescue a real
1007/// `[[…]]` that [`parse_wikilinks`]' greedy "next `]]` wins" scan has already
1008/// merged into one bogus match together with an unrelated `[[` earlier in
1009/// the same code span — by the time that happens, the real link was never
1010/// emitted as a separate [`Wikilink`] to keep. [`scan_wikilinks`] avoids the
1011/// problem at the source (it never lets a lexical scan cross a code span in
1012/// the first place) and is what `census`/`check`/rename actually use; reach
1013/// for this function only when you already have a trustworthy `Vec<Wikilink>`
1014/// (e.g. from a segment [`scan_wikilinks`] itself produced) and just need the
1015/// range check.
1016pub fn exclude_code_spans(links: Vec<Wikilink>, code_spans: &[Range<usize>]) -> Vec<Wikilink> {
1017    links
1018        .into_iter()
1019        .filter(|link| {
1020            !code_spans
1021                .iter()
1022                .any(|cs| cs.start < link.span.end && link.span.start < cs.end)
1023        })
1024        .collect()
1025}
1026
1027/// Scan `body` for wikilinks the way `census`/`check`/the rename machinery
1028/// actually should — never [`parse_wikilinks`] directly. When `path`'s
1029/// extension names a format `twig` understands, every code span (fenced/inline
1030/// code, raw escapes) is treated as opaque *before* the lexical `[[`…`]]` scan
1031/// ever sees it: each prose run between code spans is scanned on its own and the
1032/// results stitched back into `body`-relative spans. For an unrecognized
1033/// extension — or if the parse fails — this is exactly [`parse_wikilinks`] over
1034/// the whole body, the same behavior as before code-awareness existed.
1035///
1036/// The span is **lexical either way**: twig has no wikilink concept, so it
1037/// supplies the mask and nothing more. A caller about to *rewrite* what a span
1038/// covers wants [`parsed_link_spans`] instead, which reports only spans twig
1039/// itself identified as links.
1040///
1041/// Scanning prose runs *separately*, rather than scanning the whole body and
1042/// filtering the results (what [`exclude_code_spans`] alone can do), matters:
1043/// [`parse_wikilinks`]' greedy scan finds each `[[` a *later* `]]`, code or
1044/// not, closes — so one stray `[[` in a code block (a Python
1045/// `[[float('inf')] * width ...]`, DESIGN §8's motivating example, life-sized)
1046/// can eat every `]]` after it, including a real `[[gone.md]]` further down
1047/// the body, merging them into one bogus match that swallows the real link
1048/// whole. No post-hoc filter can get that link back — it was never emitted
1049/// as its own match. Keeping code spans out of the scan in the first place
1050/// is the only fix; this function is that fix.
1051pub fn scan_wikilinks(path: &Path, body: &str) -> Vec<Wikilink> {
1052    match code_spans_for(path, body) {
1053        Some(spans) if !spans.is_empty() => scan_outside_spans(body, &merge_spans(spans)),
1054        _ => parse_wikilinks(body),
1055    }
1056}
1057
1058/// One link found in body prose: the parsed [`Link`] (target, label, and whether
1059/// it was an Obsidian `[[…]]` wikilink or a markdown/djot `[label](target)`
1060/// link) together with the byte [`span`](BodyLink::span) of the whole construct
1061/// — exactly what a rewrite replaces. The unifying body-link currency: census,
1062/// `check`, and the rename machinery all consume this, blind to which syntax the
1063/// link was written in.
1064#[derive(Debug, Clone, PartialEq, Eq)]
1065pub struct BodyLink {
1066    /// The parsed link — [`render`](Link::render) reproduces its original
1067    /// wrapper, so a retargeted `[[a]]` stays a wikilink and a `[t](a)` stays a
1068    /// markdown link.
1069    pub link: Link,
1070    /// Byte range of the whole link construct within the scanned body.
1071    pub span: Range<usize>,
1072}
1073
1074impl BodyLink {
1075    /// The stable ID this link names, if any — [`Link::id_target`] on the inner
1076    /// link. ID targets are never rewritten by a move.
1077    pub fn id_target(&self) -> Option<crate::identity::Id> {
1078        self.link.id_target()
1079    }
1080
1081    /// Whether this link's target is a path — [`Link::is_path_target`] on the
1082    /// inner link. The predicate the body-rewrite passes filter on.
1083    pub fn is_path_target(&self) -> bool {
1084        self.link.is_path_target()
1085    }
1086}
1087
1088/// Scan `body` for **every** link a move or a check must account for — Obsidian
1089/// `[[…]]` wikilinks *and* markdown/djot `[label](target)` links — each as a
1090/// [`BodyLink`] in source order. This is the single body-scan seam
1091/// `census`/`check`/rename use; it supersedes the wikilink-only
1092/// [`scan_wikilinks`] for callers that must also see markdown/djot links.
1093///
1094/// Two syntaxes, two finders, both code-aware:
1095/// - **Wikilinks** come from the lexical [`scan_wikilinks`] scan (code spans
1096///   excluded at the source, so a `[[` inside a fence can never eat a later real
1097///   link).
1098/// - **Markdown/djot links** come from `twig`'s parser
1099///   ([`crate::content::link_spans`]): it reports the span of each real `link`
1100///   node, so a `[x](y)` in a code fence, an autolink, or bracket text that is
1101///   not a link is never returned. Each span holds exactly one link, so parsing
1102///   it with [`Link::parse`] cannot over-reach across a stray `)` — the
1103///   balanced-paren hazard the lexical parser has is structurally absent here.
1104///   Only inline `[label](target)` links are kept (a successful markdown parse);
1105///   reference-style and autolink forms are left for a later pass.
1106///
1107/// Falls back to wikilinks only when the extension names no `twig` grammar or the
1108/// parse fails — the same graceful degradation [`scan_wikilinks`] already has.
1109pub fn scan_body_links(path: &Path, body: &str) -> Vec<BodyLink> {
1110    let mut out: Vec<BodyLink> = scan_wikilinks(path, body)
1111        .into_iter()
1112        .map(|wl| BodyLink {
1113            link: Link {
1114                label: wl.label,
1115                target: wl.target,
1116                wikilink: true,
1117            },
1118            span: wl.span,
1119        })
1120        .collect();
1121    for span in parsed_link_spans(path, body) {
1122        let link = Link::parse(&body[span.clone()]);
1123        // Keep only inline `[label](target)` links (a labeled markdown parse):
1124        // reference/autolink spans parse to a bare or external target and are
1125        // skipped. Defensively drop a span overlapping a wikilink we already have.
1126        if link.label.is_none() || link.wikilink {
1127            continue;
1128        }
1129        if out
1130            .iter()
1131            .any(|b| b.span.start < span.end && span.start < b.span.end)
1132        {
1133            continue;
1134        }
1135        out.push(BodyLink { link, span });
1136    }
1137    out.sort_by_key(|b| b.span.start);
1138    out
1139}
1140
1141/// The spans of markdown/djot inline links in `body`, via `twig` — empty when
1142/// `path`'s extension names no grammar `twig` understands or the parse fails
1143/// (the same degrade-to-lexical rule as `code_spans_for`).
1144///
1145/// **The "twig says it is a link" predicate.** These spans come from twig's own
1146/// `link` nodes, so a span here is a link an actual parser recognized, and each
1147/// holds exactly one link. That is a materially stronger claim than
1148/// [`scan_wikilinks`] can make about a `[[…]]`: twig has no wikilink concept, so
1149/// it only supplies the *code mask* there and the span itself is still lexical.
1150/// The gap matters wherever a repair is going to *write* — a lexical span may sit
1151/// in prose that merely looks like a link, and DESIGN §8's whole objection to
1152/// editing body prose is that `[[float('inf')] * width]` must never be
1153/// "repaired". So `validate`'s body-link remedies are offered for a span in this
1154/// set and no other.
1155pub fn parsed_link_spans(path: &Path, body: &str) -> Vec<Range<usize>> {
1156    let Some(format) = crate::content::ContentFormat::from_extension(path) else {
1157        return Vec::new();
1158    };
1159    crate::content::link_spans(body, format).unwrap_or_default()
1160}
1161
1162/// Sort-then-merge overlapping/adjacent ranges. `code_spans_for`'s sources
1163/// don't currently nest or overlap (code-block/verbatim/raw nodes are AST
1164/// leaves), but merging first keeps [`scan_outside_spans`] correct even if
1165/// that ever changes, and collapses touching spans into one gap-free skip.
1166fn merge_spans(mut spans: Vec<Range<usize>>) -> Vec<Range<usize>> {
1167    spans.sort_by_key(|s| s.start);
1168    let mut out: Vec<Range<usize>> = Vec::with_capacity(spans.len());
1169    for span in spans {
1170        match out.last_mut() {
1171            Some(prev) if span.start <= prev.end => prev.end = prev.end.max(span.end),
1172            _ => out.push(span),
1173        }
1174    }
1175    out
1176}
1177
1178/// Run [`parse_wikilinks`] independently on each run of `body` outside
1179/// `code_spans` (sorted, non-overlapping), then shift each match's span back
1180/// to `body`-relative coordinates before stitching the runs' results
1181/// together in source order. This is what keeps a `[[` inside a code span
1182/// from ever being in the same scan as prose that follows it.
1183fn scan_outside_spans(body: &str, code_spans: &[Range<usize>]) -> Vec<Wikilink> {
1184    let mut out = Vec::new();
1185    let mut cursor = 0;
1186    for span in code_spans {
1187        if cursor < span.start {
1188            out.extend(shift_spans(
1189                parse_wikilinks(&body[cursor..span.start]),
1190                cursor,
1191            ));
1192        }
1193        cursor = cursor.max(span.end);
1194    }
1195    if cursor < body.len() {
1196        out.extend(shift_spans(parse_wikilinks(&body[cursor..]), cursor));
1197    }
1198    out
1199}
1200
1201fn shift_spans(links: Vec<Wikilink>, offset: usize) -> Vec<Wikilink> {
1202    links
1203        .into_iter()
1204        .map(|link| Wikilink {
1205            span: link.span.start + offset..link.span.end + offset,
1206            ..link
1207        })
1208        .collect()
1209}
1210
1211fn code_spans_for(path: &Path, body: &str) -> Option<Vec<Range<usize>>> {
1212    let format = crate::content::ContentFormat::from_extension(path)?;
1213    // A twig failure degrades to "no spans" rather than aborting the scan —
1214    // code-awareness is a refinement, the purely lexical scan above is
1215    // always a safe fallback. An unrecognized extension is `None` (via
1216    // `from_extension`), scanning the whole body as before.
1217    crate::content::code_spans(body, format).ok()
1218}
1219
1220// Lexical path handling lives in `fs-transaction`, which needs the same
1221// normalization to clamp a staged op to its root. Re-exported here so prov's
1222// read guards and its write guards demonstrably share one implementation
1223// rather than two that agree by inspection.
1224pub use fs_transaction::path::{escapes_root, normalize};
1225
1226/// Resolve a link target written in `doc` to a normalized path in the same
1227/// coordinate system as `doc` (workspace-relative when `doc` is). A target with
1228/// a leading `/` is **workspace-absolute** — resolved from the root, not `doc`'s
1229/// directory, and never against the filesystem root; any other target is
1230/// relative to `doc`'s directory.
1231///
1232/// Any [sub-document locator](Link::locator) is dropped first: it names a place
1233/// inside the document, so it has no bearing on which file this is.
1234pub fn resolve(doc: &Path, target: &str) -> PathBuf {
1235    let (target, _) = split_locator(target);
1236    if let Some(from_root) = target.strip_prefix('/') {
1237        return normalize(from_root);
1238    }
1239    let dir = doc.parent().unwrap_or(Path::new(""));
1240    normalize(dir.join(target))
1241}
1242
1243/// The relative path string that reaches `to` from `from_dir` (both normalized,
1244/// same coordinate system). Rendered with forward slashes — link targets are
1245/// text, not platform paths.
1246pub fn relative(from_dir: &Path, to: &Path) -> String {
1247    let from: Vec<&std::ffi::OsStr> = from_dir.iter().collect();
1248    let to_parts: Vec<&std::ffi::OsStr> = to.iter().collect();
1249    let common = from
1250        .iter()
1251        .zip(to_parts.iter())
1252        .take_while(|(a, b)| a == b)
1253        .count();
1254    let mut parts: Vec<String> = Vec::new();
1255    for _ in common..from.len() {
1256        parts.push("..".to_string());
1257    }
1258    for part in &to_parts[common..] {
1259        parts.push(part.to_string_lossy().into_owned());
1260    }
1261    if parts.is_empty() {
1262        ".".to_string()
1263    } else {
1264        parts.join("/")
1265    }
1266}
1267
1268#[cfg(test)]
1269mod tests {
1270    use super::*;
1271    use std::path::Component;
1272
1273    #[test]
1274    fn locator_splits_at_the_first_separator_and_round_trips() {
1275        assert_eq!(split_locator("chapter.md"), ("chapter.md", None));
1276        assert_eq!(split_locator("chapter.md#3"), ("chapter.md", Some("3")));
1277        assert_eq!(split_locator("id:abc1234#2-3"), ("id:abc1234", Some("2-3")));
1278        // First separator wins, so a locator may itself contain one.
1279        assert_eq!(split_locator("a.md#b#c"), ("a.md", Some("b#c")));
1280        // An empty locator is still a locator — the author wrote the `#`.
1281        assert_eq!(split_locator("a.md#"), ("a.md", Some("")));
1282        // A leading `#` is a same-document reference, not a locator on "".
1283        assert_eq!(split_locator("#3"), ("#3", None));
1284
1285        for target in ["chapter.md", "chapter.md#3", "a.md#b#c", "a.md#", "#3"] {
1286            let (doc, locator) = split_locator(target);
1287            assert_eq!(join_locator(doc, locator), target, "round-trip `{target}`");
1288        }
1289    }
1290
1291    #[test]
1292    fn a_locator_does_not_change_which_document_is_named() {
1293        // Path targets: the locator is dropped before resolution.
1294        assert_eq!(
1295            resolve(Path::new("bofm/1-ne-1.md"), "1-ne-2.md#5"),
1296            PathBuf::from("bofm/1-ne-2.md")
1297        );
1298        assert_eq!(
1299            resolve(Path::new("a/b.md"), "/vol/c.md#2-3"),
1300            PathBuf::from("vol/c.md")
1301        );
1302
1303        // Id targets: same id with or without a locator.
1304        let plain = Link::parse("[1 Nephi 1](id:abc1234)");
1305        let located = Link::parse("[1 Nephi 1:1](id:abc1234#1)");
1306        assert_eq!(located.id_target(), plain.id_target());
1307        assert_eq!(located.id_ref(), plain.id_ref());
1308        assert_eq!(located.locator(), Some("1"));
1309        assert_eq!(located.addressed_target(), "id:abc1234");
1310        assert_eq!(plain.locator(), None);
1311
1312        // …and a located id target is still not a path, so no move rewrites it.
1313        assert!(!located.is_path_target());
1314    }
1315
1316    #[test]
1317    fn an_external_url_keeps_its_own_fragment() {
1318        let url = Link::parse("[talk](https://example.com/a#p3)");
1319        assert!(url.is_external());
1320        assert_eq!(url.locator(), None);
1321        assert_eq!(url.addressed_target(), "https://example.com/a#p3");
1322        // Untouched by a rewrite, fragment and all.
1323        assert_eq!(url.with_path("x.md").render(), "[talk](x.md)");
1324    }
1325
1326    #[test]
1327    fn with_path_preserves_the_locator_but_with_target_does_not() {
1328        let link = Link::parse("[1 Nephi 1:1](../1-ne-1.md#1)");
1329        // The rewrite passes use `with_path`: a move changes where the document
1330        // lives, never which part of it was pointed at.
1331        assert_eq!(
1332            link.with_path("bofm/1-ne-1.md").render(),
1333            "[1 Nephi 1:1](bofm/1-ne-1.md#1)"
1334        );
1335        // `with_target` still sets the target verbatim.
1336        assert_eq!(
1337            link.with_target("bofm/1-ne-1.md").render(),
1338            "[1 Nephi 1:1](bofm/1-ne-1.md)"
1339        );
1340        // A wikilink keeps its wrapper and its locator alike.
1341        let wl = Link::parse("[[1-ne-1.md#1|1 Nephi 1:1]]");
1342        assert_eq!(wl.locator(), Some("1"));
1343        assert_eq!(wl.with_path("x.md").render(), "[[x.md#1|1 Nephi 1:1]]");
1344    }
1345
1346    #[test]
1347    fn slug_makes_readable_stems_and_round_trips_the_common_case() {
1348        assert_eq!(slug("My Great Note"), "my-great-note");
1349        // Collapses/strips separators and punctuation; keeps it legible.
1350        assert_eq!(slug("  Hello,  World!  "), "hello-world");
1351        assert_eq!(slug("already-a-slug"), "already-a-slug");
1352        assert_eq!(slug("under_scored/and slashed"), "under-scored-and-slashed");
1353        assert_eq!(slug("v1.0 Release"), "v10-release");
1354        // Unicode letters/digits survive.
1355        assert_eq!(slug("Café Notes"), "café-notes");
1356        // No leading/trailing/double dashes ever reach the output.
1357        assert_eq!(slug("--x--y--"), "x-y");
1358        // A title with nothing slug-able still yields a valid stem.
1359        assert_eq!(slug("!!!"), "untitled");
1360        assert_eq!(slug(""), "untitled");
1361        // The everyday case is the inverse of path_to_title.
1362        assert_eq!(
1363            path_to_title(std::path::Path::new("my-great-note.md")),
1364            "My Great Note"
1365        );
1366    }
1367
1368    #[test]
1369    fn parses_labeled_and_bare_links() {
1370        let l = Link::parse("[Design](docs/design.md)");
1371        assert_eq!(l.label.as_deref(), Some("Design"));
1372        assert_eq!(l.target, "docs/design.md");
1373        assert_eq!(l.render(), "[Design](docs/design.md)");
1374
1375        let bare = Link::parse("notes/a.md");
1376        assert_eq!(bare.label, None);
1377        assert_eq!(bare.render(), "notes/a.md");
1378    }
1379
1380    #[test]
1381    fn odd_shapes_fall_back_to_bare() {
1382        // A target with brackets but not the [label](target) shape.
1383        for raw in ["[unclosed](x", "no[mid](x)", "[]"] {
1384            assert_eq!(Link::parse(raw).render(), raw);
1385        }
1386    }
1387
1388    #[test]
1389    fn with_target_keeps_the_label() {
1390        let l = Link::parse("[Design](old.md)").with_target("new.md");
1391        assert_eq!(l.render(), "[Design](new.md)");
1392    }
1393
1394    #[test]
1395    fn external_links_are_flagged() {
1396        assert!(Link::parse("https://example.com/x").is_external());
1397        assert!(Link::parse("[me](mailto:a@b.c)").is_external());
1398        assert!(!Link::parse("docs/design.md").is_external());
1399    }
1400
1401    #[test]
1402    fn id_refs_split_into_local_foreign_and_malformed() {
1403        use crate::identity::Id;
1404        let r = |t: &str| Link::parse(t).id_ref();
1405        assert_eq!(r("id:ajp7eq"), Some(IdRef::Local(Id("ajp7eq".into()))));
1406        assert_eq!(
1407            r("id:notes/ajp7eq"),
1408            Some(IdRef::Foreign {
1409                workspace: "notes".into(),
1410                id: Id("ajp7eq".into()),
1411            })
1412        );
1413        // The legacy scheme carries a qualifier just as well — an old workspace
1414        // gains cross-workspace references without a rewrite.
1415        assert_eq!(
1416            r("colophon:notes/ajp7eq"),
1417            Some(IdRef::Foreign {
1418                workspace: "notes".into(),
1419                id: Id("ajp7eq".into()),
1420            })
1421        );
1422        // Every way the scheme can be present but the reference absent.
1423        for bad in ["id:", "id:/x", "id:ws/", "id:a/b/c"] {
1424            assert_eq!(r(bad), Some(IdRef::Malformed), "{bad}");
1425        }
1426        // No scheme at all is not an id ref — it is a path or an alias.
1427        assert_eq!(r("docs/design.md"), None);
1428        assert_eq!(r("https://example.com/x"), None);
1429    }
1430
1431    #[test]
1432    fn a_foreign_id_is_not_a_local_id() {
1433        // The distinction that keeps a foreign reference from being looked up in
1434        // the wrong registry: `id_target` is *local ids only*.
1435        let foreign = Link::parse("id:notes/ajp7eq");
1436        assert_eq!(foreign.id_target(), None);
1437        assert_eq!(
1438            foreign.foreign_target(),
1439            Some(("notes".into(), crate::identity::Id("ajp7eq".into())))
1440        );
1441        let local = Link::parse("id:ajp7eq");
1442        assert_eq!(
1443            local.id_target(),
1444            Some(crate::identity::Id("ajp7eq".into()))
1445        );
1446        assert_eq!(local.foreign_target(), None);
1447    }
1448
1449    #[test]
1450    fn only_paths_are_path_targets() {
1451        // The predicate every rewrite pass filters on. A move may rewrite the
1452        // first group and must leave the second alone — including the malformed
1453        // id, which is a broken reference, not a filename to re-relativize.
1454        for path in ["docs/design.md", "/a.md", "../b.md", "My Note"] {
1455            assert!(Link::parse(path).is_path_target(), "{path}");
1456        }
1457        for stable in [
1458            "id:ajp7eq",
1459            "id:notes/ajp7eq",
1460            "colophon:notes/ajp7eq",
1461            "id:a/b/c",
1462            "https://example.com/x",
1463            "mailto:a@b.c",
1464        ] {
1465            assert!(!Link::parse(stable).is_path_target(), "{stable}");
1466        }
1467    }
1468
1469    #[test]
1470    fn foreign_targets_round_trip_through_rendering() {
1471        let target = foreign_id_target("notes", &crate::identity::Id("ajp7eq".into()));
1472        assert_eq!(target, "id:notes/ajp7eq");
1473        assert_eq!(
1474            Link::parse(&target).foreign_target(),
1475            Some(("notes".into(), crate::identity::Id("ajp7eq".into())))
1476        );
1477        // Through a labeled wikilink too, since that is how diaryx would author
1478        // one — the wrapper is orthogonal to the addressing.
1479        let wl = Link::parse("[[id:notes/ajp7eq|My Note]]");
1480        assert_eq!(wl.label.as_deref(), Some("My Note"));
1481        assert_eq!(
1482            wl.foreign_target(),
1483            Some(("notes".into(), crate::identity::Id("ajp7eq".into())))
1484        );
1485        assert_eq!(wl.render(), "[[id:notes/ajp7eq|My Note]]");
1486    }
1487
1488    #[test]
1489    fn parses_angle_bracketed_and_absolute_targets() {
1490        // Diaryx-style: a labeled link to an angle-bracketed, workspace-absolute
1491        // path containing a space.
1492        let l = Link::parse("[Archived Documents](</Archive/Archived documents.md>)");
1493        assert_eq!(l.label.as_deref(), Some("Archived Documents"));
1494        assert_eq!(l.target, "/Archive/Archived documents.md");
1495        // Round-trips: the space forces the angle brackets back on render.
1496        assert_eq!(
1497            l.render(),
1498            "[Archived Documents](</Archive/Archived documents.md>)"
1499        );
1500
1501        // A bare angle-bracketed target — no `[label](…)` around it — is
1502        // *not* unwrapped: angle brackets are only URL delimiters inside a
1503        // parsed markdown link, so a bare `<…>` value stays byte-literal (C2;
1504        // diaryx reads a bare `<…>` as a literal path, brackets and all). This
1505        // used to unwrap unconditionally; see `bare_angle_bracket_value_stays_literal`
1506        // for the dedicated regression coverage.
1507        let bare = Link::parse("</Creative Writing/Creative Writing.md>");
1508        assert_eq!(bare.target, "</Creative Writing/Creative Writing.md>");
1509        assert_eq!(bare.render(), "</Creative Writing/Creative Writing.md>");
1510
1511        // An absolute path without spaces needs no brackets, and stays bare.
1512        let plain = Link::parse("[Blog](/Blog/Blog.md)");
1513        assert_eq!(plain.target, "/Blog/Blog.md");
1514        assert_eq!(plain.render(), "[Blog](/Blog/Blog.md)");
1515    }
1516
1517    /// C2 regression: before the fix, the bare-value fallback ran `unbracket`
1518    /// on the *whole* raw value, so any `<...>`-shaped bare string (not just
1519    /// the diaryx example above) was silently unwrapped. Now the bare branch
1520    /// never touches angle brackets — only a successfully parsed
1521    /// `[label](<target>)` URL gets unwrapped.
1522    #[test]
1523    fn bare_angle_bracket_value_stays_literal() {
1524        for raw in ["<notes/a.md>", "<https://example.com>", "<a (b) c>", "<>"] {
1525            let l = Link::parse(raw);
1526            assert_eq!(l.label, None);
1527            assert_eq!(l.target, raw, "bare angle-bracket value must be literal");
1528            assert_eq!(l.render(), raw);
1529        }
1530        // Contrast: the *same* angle-bracketed text, once it's the URL of an
1531        // actual markdown link, is unwrapped — that part of the old behavior
1532        // was correct and stays.
1533        assert_eq!(Link::parse("[x](<notes/a.md>)").target, "notes/a.md");
1534    }
1535
1536    /// C3 regression: before the fix, the markdown-link branch demanded the
1537    /// *entire* trimmed input end in `)` (`raw.strip_suffix(')')`), so any
1538    /// trailing text after a well-formed link's closing paren made the whole
1539    /// value fall through to the bare branch — the complete string, including
1540    /// the `[label](target)` syntax, became one literal target. Balanced-paren
1541    /// scanning fixes both halves of that: trailing text is tolerated, and
1542    /// parens *inside* the target (nested, even) don't confuse the scan.
1543    #[test]
1544    fn markdown_link_split_tolerates_trailing_text_and_balanced_parens() {
1545        // Trailing prose after a legitimate link is ignored, not swallowed.
1546        let l = Link::parse("[Title](/path.md) trailing junk");
1547        assert_eq!(l.label.as_deref(), Some("Title"));
1548        assert_eq!(l.target, "/path.md");
1549
1550        // A target containing its own parens still closes at the matching `)`.
1551        let l = Link::parse("[Explanation (1.1)](/Archive/Explanation (1.1).md)");
1552        assert_eq!(l.label.as_deref(), Some("Explanation (1.1)"));
1553        assert_eq!(l.target, "/Archive/Explanation (1.1).md");
1554
1555        // Nested parens in the target keep working.
1556        let l = Link::parse("[File (a (b))](/path/file (a (b)).md)");
1557        assert_eq!(l.label.as_deref(), Some("File (a (b))"));
1558        assert_eq!(l.target, "/path/file (a (b)).md");
1559
1560        // Trailing text *and* parens in the target, together.
1561        let l = Link::parse("[T](/a (1).md) and then some more words");
1562        assert_eq!(l.target, "/a (1).md");
1563
1564        // An angle-bracketed URL still requires the `>` immediately followed by
1565        // `)` — trailing text after *that* `)` is likewise tolerated.
1566        let l = Link::parse("[Notes](</My Notes/x.md>) ignored tail");
1567        assert_eq!(l.target, "/My Notes/x.md");
1568
1569        // An unterminated target (no closing paren at all) still falls back to
1570        // bare, unchanged from before.
1571        let unterminated = "[Title](/path.md";
1572        assert_eq!(Link::parse(unterminated).render(), unterminated);
1573    }
1574
1575    /// C1: `parse_path_only` opts out of the `[[…]]` wikilink convention so a
1576    /// frontmatter path field can hold a literal bracket-shaped string without
1577    /// `Link::parse` reinterpreting it — the convention diaryx's own path-value
1578    /// parser never had. `parse` is unchanged (still treats it as a wikilink).
1579    #[test]
1580    fn wikilink_opt_out_keeps_bracket_literal_string() {
1581        let ordinary = Link::parse("[[notes/a.md]]");
1582        assert!(ordinary.wikilink);
1583        assert_eq!(ordinary.target, "notes/a.md");
1584
1585        let opted_out = Link::parse_path_only("[[notes/a.md]]");
1586        assert!(!opted_out.wikilink);
1587        assert_eq!(opted_out.label, None);
1588        assert_eq!(opted_out.target, "[[notes/a.md]]");
1589        assert_eq!(opted_out.render(), "[[notes/a.md]]");
1590
1591        // A pipe-labeled wikilink scalar is likewise kept as one literal bare
1592        // string, not split into label/target.
1593        let piped = Link::parse_path_only("[[notes/a.md|My Note]]");
1594        assert_eq!(piped.label, None);
1595        assert_eq!(piped.target, "[[notes/a.md|My Note]]");
1596
1597        // Every other `parse` rule is unaffected: markdown links, bare paths,
1598        // and angle-bracket handling (both C2's literal-bare and C3's
1599        // balanced-paren splitting) all behave identically under the opt-out.
1600        assert_eq!(
1601            Link::parse_path_only("[Design](docs/design.md)"),
1602            Link::parse("[Design](docs/design.md)")
1603        );
1604        assert_eq!(
1605            Link::parse_path_only("notes/a.md"),
1606            Link::parse("notes/a.md")
1607        );
1608        assert_eq!(
1609            Link::parse_path_only("<notes/a.md>"),
1610            Link::parse("<notes/a.md>")
1611        );
1612    }
1613
1614    #[test]
1615    fn formats_links_in_each_workspace_style() {
1616        let from = Path::new("School/MATH 213/hw.md");
1617        let target = Path::new("School/Archive/MATH 213 files.md");
1618        // MarkdownRoot: absolute, titled, angle-bracketed for the space.
1619        assert_eq!(
1620            format_link(LinkStyle::MarkdownRoot, from, target, "MATH 213 Files"),
1621            "[MATH 213 Files](</School/Archive/MATH 213 files.md>)"
1622        );
1623        // MarkdownRelative: relative, titled.
1624        assert_eq!(
1625            format_link(LinkStyle::MarkdownRelative, from, target, "MATH 213 Files"),
1626            "[MATH 213 Files](<../Archive/MATH 213 files.md>)"
1627        );
1628        // Plain styles: bare, no title.
1629        assert_eq!(
1630            format_link(LinkStyle::PlainRelative, from, target, "ignored"),
1631            "../Archive/MATH 213 files.md"
1632        );
1633        assert_eq!(
1634            format_link(LinkStyle::PlainRoot, from, target, "ignored"),
1635            "/School/Archive/MATH 213 files.md"
1636        );
1637    }
1638
1639    #[test]
1640    fn link_style_axes_round_trip_and_cover_every_combination() {
1641        use Notation::*;
1642        use PathStyle::*;
1643        // Every notation×path_style combination has a fused LinkStyle, and axes()
1644        // is its inverse — so the orthogonal config surface is lossless.
1645        for notation in [Markdown, Bare] {
1646            for path_style in [Root, Relative] {
1647                let style = LinkStyle::from_axes(notation, path_style);
1648                assert_eq!(style.axes(), (notation, path_style));
1649            }
1650        }
1651        // Wikilink has no bare/bracketed split, so it maps through the Markdown
1652        // family and its path text follows the path style.
1653        assert_eq!(
1654            LinkStyle::from_axes(Wikilink, Relative),
1655            LinkStyle::MarkdownRelative
1656        );
1657        assert_eq!(
1658            Notation::from_wrapper(Wrapper::Wikilink, LinkStyle::MarkdownRoot),
1659            Wikilink
1660        );
1661        assert_eq!(Notation::from_config_str("bare"), Some(Bare));
1662        assert_eq!(PathStyle::from_config_str("relative"), Some(Relative));
1663        // Retired: a bare workspace-relative path is unspellable, because a bare
1664        // path is directory-relative and the two cannot both be true.
1665        assert_eq!(PathStyle::from_config_str("canonical"), None);
1666        assert_eq!(LinkStyle::default(), LinkStyle::MarkdownRoot);
1667        assert_eq!(
1668            path_to_title(Path::new("Folder/utility_index.md")),
1669            "Utility Index"
1670        );
1671    }
1672
1673    #[test]
1674    fn the_bare_workspace_absolute_style_renders_a_leading_slash() {
1675        let from = Path::new("a/b.md");
1676        let to = Path::new("c/d.md");
1677        assert_eq!(format_link(LinkStyle::PlainRoot, from, to, "D"), "/c/d.md");
1678    }
1679
1680    #[test]
1681    fn resolves_workspace_absolute_paths_from_the_root() {
1682        // A leading slash means "from the workspace root", regardless of where
1683        // the linking document sits — and never the filesystem root.
1684        assert_eq!(
1685            resolve(Path::new("Meta/Meta files.md"), "/Blog/Blog.md"),
1686            PathBuf::from("Blog/Blog.md")
1687        );
1688        assert_eq!(
1689            resolve(Path::new("deep/nested/doc.md"), "/Resume.md"),
1690            PathBuf::from("Resume.md")
1691        );
1692        // Relative targets still resolve against the document's own directory.
1693        assert_eq!(
1694            resolve(Path::new("Meta/Meta files.md"), "../Blog/Blog.md"),
1695            PathBuf::from("Blog/Blog.md")
1696        );
1697    }
1698
1699    #[test]
1700    fn normalizes_dot_and_dotdot() {
1701        assert_eq!(normalize("a/./b/../c.md"), PathBuf::from("a/c.md"));
1702        assert_eq!(normalize("../up.md"), PathBuf::from("../up.md"));
1703        assert_eq!(normalize("a/b/../../x.md"), PathBuf::from("x.md"));
1704    }
1705
1706    #[test]
1707    fn resolves_against_the_documents_directory() {
1708        assert_eq!(
1709            resolve(Path::new("docs/index.md"), "../README.md"),
1710            PathBuf::from("README.md")
1711        );
1712        assert_eq!(
1713            resolve(Path::new("README.md"), "docs/design.md"),
1714            PathBuf::from("docs/design.md")
1715        );
1716    }
1717
1718    #[test]
1719    fn scans_bare_and_labeled_wikilinks_with_spans() {
1720        let body = "see [[notes/a.md]] and [[colophon:ajp7eq|My file]] here";
1721        let links = parse_wikilinks(body);
1722        assert_eq!(links.len(), 2);
1723
1724        assert_eq!(links[0].target, "notes/a.md");
1725        assert_eq!(links[0].label, None);
1726        assert_eq!(&body[links[0].span.clone()], "[[notes/a.md]]");
1727        assert_eq!(links[0].id_target(), None);
1728
1729        assert_eq!(links[1].target, "colophon:ajp7eq");
1730        assert_eq!(links[1].label.as_deref(), Some("My file"));
1731        assert_eq!(&body[links[1].span.clone()], "[[colophon:ajp7eq|My file]]");
1732        assert_eq!(
1733            links[1].id_target(),
1734            Some(crate::identity::Id("ajp7eq".into()))
1735        );
1736    }
1737
1738    #[test]
1739    fn wikilink_scan_trims_and_skips_degenerate_shapes() {
1740        // Whitespace inside the brackets is trimmed on both sides of the pipe.
1741        let trimmed = parse_wikilinks("x [[  notes/a.md  |  Label  ]] y");
1742        assert_eq!(trimmed[0].target, "notes/a.md");
1743        assert_eq!(trimmed[0].label.as_deref(), Some("Label"));
1744
1745        // Empty target and unclosed openers are not links.
1746        assert!(parse_wikilinks("nothing [[]] here").is_empty());
1747        assert!(parse_wikilinks("[[ | orphan label ]]").is_empty());
1748        assert!(parse_wikilinks("dangling [[notes/a.md without close").is_empty());
1749    }
1750
1751    #[test]
1752    fn wikilink_render_round_trips_and_retargets() {
1753        let link = &parse_wikilinks("[[old.md|Design]]")[0];
1754        assert_eq!(link.render(), "[[old.md|Design]]");
1755        // Retarget keeps the label — the rename path relies on this.
1756        assert_eq!(link.with_target("new.md").render(), "[[new.md|Design]]");
1757
1758        let bare = &parse_wikilinks("[[old.md]]")[0];
1759        assert_eq!(bare.render(), "[[old.md]]");
1760    }
1761
1762    #[test]
1763    fn exclude_code_spans_drops_only_overlapping_wikilinks() {
1764        let body = "see [[notes/a.md]] and `[[not/a/link]]` too";
1765        let links = parse_wikilinks(body);
1766        assert_eq!(links.len(), 2, "the lexical scanner has no code awareness");
1767
1768        let code_start = body.find('`').unwrap();
1769        let code_end = body.rfind('`').unwrap() + 1;
1770        let code_span = code_start..code_end;
1771        let kept = exclude_code_spans(links, std::slice::from_ref(&code_span));
1772
1773        assert_eq!(kept.len(), 1);
1774        assert_eq!(kept[0].target, "notes/a.md");
1775    }
1776
1777    #[test]
1778    fn relative_walks_up_and_down() {
1779        assert_eq!(
1780            relative(Path::new("docs"), Path::new("README.md")),
1781            "../README.md"
1782        );
1783        assert_eq!(
1784            relative(Path::new(""), Path::new("docs/design.md")),
1785            "docs/design.md"
1786        );
1787        assert_eq!(relative(Path::new("a/b"), Path::new("a/b/c.md")), "c.md");
1788        assert_eq!(relative(Path::new("a/b"), Path::new("a/b")), ".");
1789    }
1790
1791    #[test]
1792    fn parses_and_round_trips_wikilink_scalars_in_metadata() {
1793        // A metadata scalar written as a wikilink resolves through the same
1794        // Link path as a markdown one, and round-trips its wrapper.
1795        let l = Link::parse("[[id:ajp7eqb|My File]]");
1796        assert!(l.wikilink);
1797        assert_eq!(l.label.as_deref(), Some("My File"));
1798        assert_eq!(l.target, "id:ajp7eqb");
1799        assert_eq!(l.id_target(), Some(crate::identity::Id("ajp7eqb".into())));
1800        assert_eq!(l.render(), "[[id:ajp7eqb|My File]]");
1801
1802        let bare = Link::parse("[[notes/a.md]]");
1803        assert!(bare.wikilink);
1804        assert_eq!(bare.label, None);
1805        assert_eq!(bare.render(), "[[notes/a.md]]");
1806        // Retarget keeps the wikilink wrapper and label.
1807        assert_eq!(
1808            l.with_target("id:zzzzzz9").render(),
1809            "[[id:zzzzzz9|My File]]"
1810        );
1811    }
1812
1813    #[test]
1814    fn id_scheme_reads_current_and_legacy_spellings() {
1815        assert_eq!(strip_id_scheme("id:ajp7eqb"), Some("ajp7eqb"));
1816        assert_eq!(strip_id_scheme("colophon:ajp7eqb"), Some("ajp7eqb"));
1817        assert_eq!(strip_id_scheme("notes/a.md"), None);
1818        // New links are authored in the `id:` spelling.
1819        assert_eq!(
1820            id_target(&crate::identity::Id("ajp7eqb".into())),
1821            "id:ajp7eqb"
1822        );
1823        assert_eq!(
1824            Link::parse("colophon:ajp7eqb").id_target().unwrap().0,
1825            "ajp7eqb"
1826        );
1827    }
1828
1829    #[test]
1830    fn format_reference_renders_each_style() {
1831        let from = Path::new("notes/hw.md");
1832        let to = Path::new("Archive/a.md");
1833        let id = crate::identity::Id("ajp7eqb".into());
1834        let s = |wrapper, addressing, label| ReferenceStyle {
1835            wrapper,
1836            addressing,
1837            label,
1838            path_style: LinkStyle::MarkdownRoot,
1839        };
1840
1841        // Markdown + path → the classic LinkStyle rendering.
1842        assert_eq!(
1843            format_reference(
1844                s(Wrapper::Markdown, Addressing::Path, false),
1845                from,
1846                to,
1847                None,
1848                "A"
1849            ),
1850            "[A](/Archive/a.md)"
1851        );
1852        // Wikilink + path, label off vs on.
1853        assert_eq!(
1854            format_reference(
1855                s(Wrapper::Wikilink, Addressing::Path, false),
1856                from,
1857                to,
1858                None,
1859                "A"
1860            ),
1861            "[[/Archive/a.md]]"
1862        );
1863        assert_eq!(
1864            format_reference(
1865                s(Wrapper::Wikilink, Addressing::Path, true),
1866                from,
1867                to,
1868                None,
1869                "A"
1870            ),
1871            "[[/Archive/a.md|A]]"
1872        );
1873        // Markdown + id: bare when unlabeled (the diaryx-shaped id link), a
1874        // titled markdown link when labeled.
1875        assert_eq!(
1876            format_reference(
1877                s(Wrapper::Markdown, Addressing::Id, false),
1878                from,
1879                to,
1880                Some(&id),
1881                "A"
1882            ),
1883            "id:ajp7eqb"
1884        );
1885        assert_eq!(
1886            format_reference(
1887                s(Wrapper::Markdown, Addressing::Id, true),
1888                from,
1889                to,
1890                Some(&id),
1891                "A"
1892            ),
1893            "[A](id:ajp7eqb)"
1894        );
1895        // Wikilink + id, no label / with label.
1896        assert_eq!(
1897            format_reference(
1898                s(Wrapper::Wikilink, Addressing::Id, false),
1899                from,
1900                to,
1901                Some(&id),
1902                "A"
1903            ),
1904            "[[id:ajp7eqb]]"
1905        );
1906        assert_eq!(
1907            format_reference(
1908                s(Wrapper::Wikilink, Addressing::Id, true),
1909                from,
1910                to,
1911                Some(&id),
1912                "A"
1913            ),
1914            "[[id:ajp7eqb|A]]"
1915        );
1916        // Alias is a bare-name wikilink, even if markdown was requested.
1917        assert_eq!(
1918            format_reference(
1919                s(Wrapper::Markdown, Addressing::Alias, false),
1920                from,
1921                to,
1922                None,
1923                "My File"
1924            ),
1925            "[[My File]]"
1926        );
1927        // Id addressing with no id available degrades to a path link.
1928        assert_eq!(
1929            format_reference(
1930                s(Wrapper::Wikilink, Addressing::Id, true),
1931                from,
1932                to,
1933                None,
1934                "A"
1935            ),
1936            "[A](/Archive/a.md)"
1937        );
1938    }
1939
1940    #[test]
1941    fn reference_style_config_round_trips_and_normalizes() {
1942        assert_eq!(
1943            Wrapper::from_config_str("wikilink"),
1944            Some(Wrapper::Wikilink)
1945        );
1946        assert_eq!(
1947            Addressing::from_config_str("alias"),
1948            Some(Addressing::Alias)
1949        );
1950        assert_eq!(Wrapper::Wikilink.as_config_str(), "wikilink");
1951        assert_eq!(Addressing::Id.as_config_str(), "id");
1952        // markdown + alias is impossible; normalization forces wikilink.
1953        let n = ReferenceStyle {
1954            addressing: Addressing::Alias,
1955            ..ReferenceStyle::default()
1956        }
1957        .normalized();
1958        assert_eq!(n.wrapper, Wrapper::Wikilink);
1959        assert!(
1960            ReferenceStyle {
1961                addressing: Addressing::Id,
1962                ..ReferenceStyle::default()
1963            }
1964            .registers()
1965        );
1966        assert!(!ReferenceStyle::default().registers());
1967    }
1968
1969    #[test]
1970    fn path_text_takes_the_path_style_shape() {
1971        let from = Path::new("a/b/hw.md");
1972        let to = Path::new("a/c/x.md");
1973        assert_eq!(path_text(LinkStyle::MarkdownRoot, from, to), "/a/c/x.md");
1974        assert_eq!(
1975            path_text(LinkStyle::MarkdownRelative, from, to),
1976            "../c/x.md"
1977        );
1978        assert_eq!(path_text(LinkStyle::PlainRoot, from, to), "/a/c/x.md");
1979    }
1980
1981    /// Laws, rather than examples.
1982    ///
1983    /// Every test above names one input and asserts the output prov produced
1984    /// for it. That is the right shape for a decision (`slug("v1.0 Release")`
1985    /// is `"v10-release"` because someone chose it), and the wrong shape for a
1986    /// *law* — a claim quantified over every input, of which an example
1987    /// witnesses exactly one. The round-trip below is a law: a link prov
1988    /// authors must name the document prov meant, from wherever it was
1989    /// written, in whichever style the workspace declared.
1990    mod properties {
1991        use super::*;
1992        use proptest::prelude::*;
1993
1994        /// A workspace-relative document path — up to two directories deep,
1995        /// short lowercase names. The alphabet is small on purpose: a
1996        /// counterexample is only useful if you can read it, and `a/b.md`
1997        /// makes the same point as `Xk9/qZ2.md` with none of the noise.
1998        fn doc_path() -> impl Strategy<Value = PathBuf> {
1999            (prop::collection::vec("[a-z]{1,3}", 0..3usize), "[a-z]{1,3}").prop_map(
2000                |(dirs, stem)| {
2001                    let mut path = PathBuf::new();
2002                    for dir in dirs {
2003                        path.push(dir);
2004                    }
2005                    path.push(format!("{stem}.md"));
2006                    path
2007                },
2008            )
2009        }
2010
2011        /// A path as a *human* might write one into frontmatter: real names
2012        /// mixed with the `.` and `..` a relative reference is made of. Unlike
2013        /// [`doc_path`] this may climb above the root (`../../x`), which is
2014        /// legal to *write* and refused later by [`escapes_root`] — the
2015        /// normalizer has to survive it either way.
2016        fn messy_path() -> impl Strategy<Value = PathBuf> {
2017            prop::collection::vec(
2018                prop_oneof![Just(".".to_string()), Just("..".to_string()), "[a-z]{1,3}"],
2019                1..5usize,
2020            )
2021            .prop_map(|parts| parts.iter().collect())
2022        }
2023
2024        /// Every style there is.
2025        ///
2026        /// This list was once four of six. The two `Canonical` styles emitted a
2027        /// bare *workspace*-relative path that [`resolve`] reads as
2028        /// *directory*-relative, so they failed the law below — and they are
2029        /// gone now rather than excused, which is why the strategy can name the
2030        /// whole enum again. A style that cannot be read back is not a style.
2031        fn round_tripping_style() -> impl Strategy<Value = LinkStyle> {
2032            prop::sample::select(vec![
2033                LinkStyle::MarkdownRoot,
2034                LinkStyle::MarkdownRelative,
2035                LinkStyle::PlainRoot,
2036                LinkStyle::PlainRelative,
2037            ])
2038        }
2039
2040        proptest! {
2041            /// `resolve ∘ format = id`: authoring a link to `to` from the
2042            /// document at `from`, then reading it back the way every consumer
2043            /// does (`Link::parse` for the scalar, `resolve` for the path),
2044            /// must land on `to` again. The whole point of a style axis is
2045            /// that it changes how a reference is *spelled* and not what it
2046            /// *means* — so this holds for every style, which is a claim the
2047            /// enum only earned once the canonical pair was retired.
2048            #[test]
2049            fn a_formatted_link_resolves_back_to_the_document_it_names(
2050                from in doc_path(),
2051                to in doc_path(),
2052                style in round_tripping_style(),
2053            ) {
2054                let written = format_link(style, &from, &to, "Label");
2055                let got = resolve(&from, &Link::parse(&written).target);
2056                prop_assert_eq!(
2057                    &got,
2058                    &to,
2059                    "{:?} wrote `{}` in `{}`",
2060                    style,
2061                    written,
2062                    from.display()
2063                );
2064            }
2065
2066            /// The law a move rests on: **re-relativizing a link preserves its
2067            /// referent.** `mutate::rename` rewrites every outbound path link
2068            /// of a document it moves by resolving the old text against the old
2069            /// location and re-rendering it against the new one
2070            /// (`rename.rs:236`). That is only safe if the two resolve to the
2071            /// same document — which is this, quantified over every pair of
2072            /// locations a document could move between.
2073            #[test]
2074            fn re_relativizing_a_link_preserves_the_document_it_names(
2075                from in doc_path(),
2076                to in doc_path(),
2077                referent in doc_path(),
2078            ) {
2079                let dir = |p: &Path| p.parent().unwrap_or(Path::new("")).to_path_buf();
2080                // As authored in the document at its old location.
2081                let written = relative(&dir(&from), &referent);
2082                // What `rename` writes when the document lands at `to`.
2083                let rewritten = relative(&dir(&to), &resolve(&from, &written));
2084                prop_assert_eq!(
2085                    resolve(&to, &rewritten),
2086                    referent,
2087                    "`{}` moving {} → {} became `{}`",
2088                    written,
2089                    from.display(),
2090                    to.display(),
2091                    rewritten
2092                );
2093            }
2094
2095            /// [`normalize`] is idempotent — folding `.`/`..` a second time
2096            /// changes nothing. Load-bearing because resolved paths are
2097            /// compared for equality all over the crate (`move_conflict`, the
2098            /// registry's id↔path bijection, every `check` finding that says
2099            /// two links name the same document); a normal form that still
2100            /// moves under re-application would make those comparisons depend
2101            /// on how many times a path had been through the resolver.
2102            #[test]
2103            fn normalizing_a_path_twice_is_normalizing_it_once(path in messy_path()) {
2104                let once = normalize(&path);
2105                prop_assert_eq!(normalize(&once), once.clone(), "on `{}`", path.display());
2106            }
2107
2108            /// A normalized path has no `.` left, and any `..` that survives is
2109            /// a *leading* one — the climb above the root that could not be
2110            /// cancelled, which is precisely what [`escapes_root`] then looks
2111            /// for. A `..` appearing after a normal component would mean the
2112            /// fold missed a cancellation, and every path comparison downstream
2113            /// would be reading a path that still had reducing left to do.
2114            #[test]
2115            fn a_normalized_path_keeps_only_the_climb_it_could_not_cancel(
2116                path in messy_path(),
2117            ) {
2118                let normalized = normalize(&path);
2119                let parts: Vec<_> = normalized.components().collect();
2120                let climb = parts
2121                    .iter()
2122                    .take_while(|c| matches!(c, Component::ParentDir))
2123                    .count();
2124                prop_assert!(
2125                    parts[climb..]
2126                        .iter()
2127                        .all(|c| matches!(c, Component::Normal(_) | Component::RootDir)),
2128                    "`{}` normalized to `{}`",
2129                    path.display(),
2130                    normalized.display()
2131                );
2132            }
2133        }
2134    }
2135}