typub-passes 0.1.1

Semantic IR passes for typub
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Internal link pass for v2 semantic document IR.

use anyhow::Result;
use std::collections::BTreeSet;
use typub_core::LinkResolution;
use typub_html::inlines_text;
use typub_ir::{Block, Document, Inline, ListKind, UnknownChild, Url};
use typub_storage::StatusTracker;

use super::walk::{NodePath, VisitorMut, walk_document_mut};
use super::{Diagnostic, DiagnosticLevel, Pass, PassCtx};

#[derive(Debug, Clone, PartialEq, Eq)]
struct ParsedInternalHref {
    slug: String,
    suffix: String,
}

/// Behavior for unresolved internal links.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UnresolvedInternalLinkBehavior {
    /// Keep the original link href unchanged.
    KeepLink,
    /// Replace link node with plain text extracted from link content.
    #[default]
    ReplaceWithText,
}

/// Resolve internal slugs to published URLs.
pub trait LinkResolver {
    /// Resolve `slug` for a preferred platform, or any platform when `preferred_platform` is `None`.
    fn resolve_slug(&self, slug: &str, preferred_platform: Option<&str>) -> Result<Option<String>>;
}

/// `LinkResolver` backed by `StatusTracker`.
pub struct StatusTrackerResolver<'a> {
    tracker: &'a StatusTracker,
}

impl<'a> StatusTrackerResolver<'a> {
    pub fn new(tracker: &'a StatusTracker) -> Self {
        Self { tracker }
    }
}

impl LinkResolver for StatusTrackerResolver<'_> {
    fn resolve_slug(&self, slug: &str, preferred_platform: Option<&str>) -> Result<Option<String>> {
        if let Some(platform) = preferred_platform {
            return self.tracker.get_published_url(slug, platform);
        }
        self.tracker
            .get_first_published_url(slug)
            .map(|v| v.map(|(_, url)| url))
    }
}

/// Link rewrite pass over `Inline::Link`.
pub struct ResolveInternalLinksPass<'a> {
    resolver: &'a dyn LinkResolver,
    preferred_platform: Option<&'a str>,
    unresolved_behavior: UnresolvedInternalLinkBehavior,
}

impl<'a> ResolveInternalLinksPass<'a> {
    pub fn new(resolver: &'a dyn LinkResolver, preferred_platform: Option<&'a str>) -> Self {
        Self {
            resolver,
            preferred_platform,
            unresolved_behavior: UnresolvedInternalLinkBehavior::default(),
        }
    }

    pub fn with_unresolved_behavior(mut self, behavior: UnresolvedInternalLinkBehavior) -> Self {
        self.unresolved_behavior = behavior;
        self
    }
}

impl Pass for ResolveInternalLinksPass<'_> {
    fn name(&self) -> &'static str {
        "resolve_internal_links"
    }

    fn run(&mut self, doc: &mut Document, ctx: &mut PassCtx) -> Result<()> {
        let mut rewriter = LinkRewriter {
            resolver: self.resolver,
            preferred_platform: self.preferred_platform,
            unresolved_behavior: self.unresolved_behavior,
            diagnostics: Vec::new(),
        };
        walk_document_mut(doc, &mut rewriter)?;
        ctx.diagnostics.append(&mut rewriter.diagnostics);
        Ok(())
    }
}

struct LinkRewriter<'a> {
    resolver: &'a dyn LinkResolver,
    preferred_platform: Option<&'a str>,
    unresolved_behavior: UnresolvedInternalLinkBehavior,
    diagnostics: Vec<Diagnostic>,
}

impl LinkRewriter<'_> {
    fn warning(&mut self, message: String, path: &NodePath) {
        self.diagnostics.push(Diagnostic {
            pass: "resolve_internal_links",
            level: DiagnosticLevel::Warning,
            message,
            location: Some(path.render()),
        });
    }
}

impl VisitorMut for LinkRewriter<'_> {
    fn visit_inline(&mut self, inline: &mut Inline, path: &NodePath) -> Result<()> {
        let (href, text_fallback) = match inline {
            Inline::Link { content, href, .. } => (href.0.clone(), inlines_text(content)),
            _ => return Ok(()),
        };

        match resolve_href_with(self.resolver, &href, self.preferred_platform) {
            Ok(LinkResolution::NonInternal) => {}
            Ok(LinkResolution::InternalResolved { url, .. }) => {
                if let Inline::Link { href, .. } = inline {
                    *href = Url(url);
                }
            }
            Ok(LinkResolution::InternalUnresolved { slug }) => {
                self.warning(
                    format!("internal link target '{}' not found; link degraded", slug),
                    path,
                );
                if self.unresolved_behavior == UnresolvedInternalLinkBehavior::ReplaceWithText {
                    *inline = Inline::Text(text_fallback);
                }
            }
            Err(err) => {
                self.warning(
                    format!(
                        "failed to resolve internal link '{}': {}; link degraded",
                        href, err
                    ),
                    path,
                );
                if self.unresolved_behavior == UnresolvedInternalLinkBehavior::ReplaceWithText {
                    *inline = Inline::Text(text_fallback);
                }
            }
        }

        Ok(())
    }
}

fn looks_external(href: &str) -> bool {
    let h = href.trim().to_ascii_lowercase();
    let looks_like_scheme = h
        .find(':')
        .is_some_and(|idx| !h[..idx].contains('/') && !h[..idx].is_empty());
    h.starts_with("http://")
        || h.starts_with("https://")
        || h.starts_with("mailto:")
        || h.starts_with("tel:")
        || h.starts_with("file:")
        || h.starts_with("data:")
        || h.starts_with('#')
        || h.starts_with("//")
        || looks_like_scheme
}

fn split_path_and_suffix(href: &str) -> Option<(&str, &str)> {
    let trimmed = href.trim();
    if trimmed.is_empty() {
        return None;
    }
    if let Some(i) = trimmed.find(['?', '#']) {
        Some((&trimmed[..i], &trimmed[i..]))
    } else {
        Some((trimmed, ""))
    }
}

fn parse_internal_href(href: &str) -> Option<ParsedInternalHref> {
    if looks_external(href) {
        return None;
    }
    let (base, suffix) = split_path_and_suffix(href)?;
    if base.is_empty() {
        return None;
    }
    let trimmed = base.trim_start_matches('/').trim_end_matches('/');
    let parts: Vec<&str> = trimmed.split('/').filter(|s| !s.is_empty()).collect();
    let mut segment = parts.last().copied()?.trim();
    if segment.is_empty() {
        return None;
    }
    if let Some(no_ext) = segment.strip_suffix(".html") {
        segment = no_ext;
    }
    if segment == "index"
        && let Some(prev) = parts.iter().rev().nth(1)
        && !prev.trim().is_empty()
    {
        segment = prev.trim();
    }
    if segment.is_empty() {
        return None;
    }
    Some(ParsedInternalHref {
        slug: segment.to_string(),
        suffix: suffix.to_string(),
    })
}

fn apply_url_suffix(base_url: &str, suffix: &str) -> String {
    if suffix.is_empty() {
        return base_url.to_string();
    }
    format!("{base_url}{suffix}")
}

/// Resolve href using a custom resolver.
pub fn resolve_href_with(
    resolver: &dyn LinkResolver,
    href: &str,
    preferred_platform: Option<&str>,
) -> Result<LinkResolution> {
    let Some(parsed) = parse_internal_href(href) else {
        return Ok(LinkResolution::NonInternal);
    };

    if let Some(preferred) = preferred_platform
        && let Some(url) = resolver.resolve_slug(&parsed.slug, Some(preferred))?
    {
        return Ok(LinkResolution::InternalResolved {
            slug: parsed.slug,
            url: apply_url_suffix(&url, &parsed.suffix),
        });
    }

    if let Some(url) = resolver.resolve_slug(&parsed.slug, None)? {
        return Ok(LinkResolution::InternalResolved {
            slug: parsed.slug,
            url: apply_url_suffix(&url, &parsed.suffix),
        });
    }

    Ok(LinkResolution::InternalUnresolved { slug: parsed.slug })
}

/// Resolve href with a `StatusTracker`.
pub fn resolve_href(
    href: &str,
    preferred_platform: Option<&str>,
    tracker: &StatusTracker,
) -> Result<LinkResolution> {
    let resolver = StatusTrackerResolver::new(tracker);
    resolve_href_with(&resolver, href, preferred_platform)
}

/// Extract the target slug from a relative/internal href.
pub fn extract_slug_from_relative_href(href: &str) -> Option<String> {
    parse_internal_href(href).map(|v| v.slug)
}

/// Collect unique internal link targets from a semantic document.
pub fn collect_internal_link_targets(doc: &Document) -> Vec<String> {
    let mut slugs = BTreeSet::new();
    collect_from_blocks(&doc.blocks, &mut slugs);
    for def in doc.footnotes.values() {
        collect_from_blocks(&def.blocks, &mut slugs);
    }
    slugs.into_iter().collect()
}

fn collect_from_blocks(blocks: &[Block], out: &mut BTreeSet<String>) {
    for block in blocks {
        collect_from_block(block, out);
    }
}

fn collect_from_block(block: &Block, out: &mut BTreeSet<String>) {
    match block {
        Block::Heading { content, .. } | Block::Paragraph { content, .. } => {
            collect_from_inlines(content, out);
        }
        Block::Quote { blocks, .. }
        | Block::Figure {
            content: blocks, ..
        }
        | Block::Admonition { blocks, .. }
        | Block::Details { blocks, .. } => collect_from_blocks(blocks, out),
        Block::CodeBlock { .. }
        | Block::Divider { .. }
        | Block::MathBlock { .. }
        | Block::SvgBlock { .. } => {}
        Block::List { list, .. } => collect_from_list_kind(&list.kind, out),
        Block::DefinitionList { items, .. } => {
            for item in items {
                for term in &item.terms {
                    collect_from_blocks(term, out);
                }
                for definition in &item.definitions {
                    collect_from_blocks(definition, out);
                }
            }
        }
        Block::Table {
            caption, sections, ..
        } => {
            if let Some(caption) = caption {
                collect_from_blocks(caption, out);
            }
            for section in sections {
                for row in &section.rows {
                    for cell in &row.cells {
                        collect_from_blocks(&cell.blocks, out);
                    }
                }
            }
        }
        Block::UnknownBlock { children, .. } => {
            for child in children {
                match child {
                    UnknownChild::Block(block) => collect_from_block(block, out),
                    UnknownChild::Inline(inline) => collect_from_inline(inline, out),
                }
            }
        }
        Block::RawBlock { .. } => {}
    }
}

fn collect_from_list_kind(kind: &ListKind, out: &mut BTreeSet<String>) {
    match kind {
        ListKind::Bullet { items } | ListKind::Numbered { items, .. } => {
            for item in items {
                collect_from_blocks(&item.blocks, out);
            }
        }
        ListKind::Task { items } => {
            for item in items {
                collect_from_blocks(&item.blocks, out);
            }
        }
        ListKind::Custom { items, .. } => {
            for item in items {
                collect_from_blocks(&item.blocks, out);
            }
        }
    }
}

fn collect_from_inlines(inlines: &[Inline], out: &mut BTreeSet<String>) {
    for inline in inlines {
        collect_from_inline(inline, out);
    }
}

fn collect_from_inline(inline: &Inline, out: &mut BTreeSet<String>) {
    match inline {
        Inline::Link { content, href, .. } => {
            if let Some(parsed) = parse_internal_href(&href.0) {
                out.insert(parsed.slug);
            }
            collect_from_inlines(content, out);
        }
        Inline::Styled { content, .. } | Inline::UnknownInline { content, .. } => {
            collect_from_inlines(content, out)
        }
        Inline::Text(_)
        | Inline::Code(_)
        | Inline::SoftBreak
        | Inline::HardBreak
        | Inline::Image { .. }
        | Inline::FootnoteRef(_)
        | Inline::MathInline { .. }
        | Inline::SvgInline { .. }
        | Inline::RawInline { .. } => {}
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used)]

    use super::*;
    use std::collections::BTreeMap;
    use typub_ir::{BlockAttrs, DocMeta, Document, InlineAttrs};

    #[derive(Default)]
    struct MockResolver {
        by_platform: BTreeMap<(String, String), String>,
        any_platform: BTreeMap<String, String>,
        fail_slug: Option<String>,
    }

    impl LinkResolver for MockResolver {
        fn resolve_slug(
            &self,
            slug: &str,
            preferred_platform: Option<&str>,
        ) -> Result<Option<String>> {
            if self.fail_slug.as_deref() == Some(slug) {
                anyhow::bail!("resolver failure for slug '{}'", slug);
            }
            if let Some(platform) = preferred_platform
                && let Some(url) = self
                    .by_platform
                    .get(&(slug.to_string(), platform.to_string()))
            {
                return Ok(Some(url.clone()));
            }
            Ok(self.any_platform.get(slug).cloned())
        }
    }

    fn simple_doc_with_link(href: &str, text: &str) -> Document {
        Document {
            blocks: vec![Block::Paragraph {
                content: vec![Inline::Link {
                    content: vec![Inline::Text(text.to_string())],
                    href: Url(href.to_string()),
                    title: None,
                    attrs: InlineAttrs::default(),
                }],
                attrs: BlockAttrs::default(),
            }],
            footnotes: BTreeMap::new(),
            assets: BTreeMap::new(),
            meta: DocMeta::default(),
        }
    }

    #[test]
    fn extract_slug_from_relative_href_handles_common_paths() {
        assert_eq!(
            extract_slug_from_relative_href("../2026-02-09-hello-world/"),
            Some("2026-02-09-hello-world".to_string())
        );
        assert_eq!(
            extract_slug_from_relative_href("./2026-02-09-hello-world/index.html"),
            Some("2026-02-09-hello-world".to_string())
        );
        assert_eq!(
            extract_slug_from_relative_href("https://example.com/post"),
            None
        );
        assert_eq!(extract_slug_from_relative_href("#section"), None);
        assert_eq!(
            extract_slug_from_relative_href("/absolute/path"),
            Some("path".to_string())
        );
    }

    #[test]
    fn collect_internal_link_targets_is_deduplicated_and_sorted() {
        let doc = Document {
            blocks: vec![
                Block::Paragraph {
                    content: vec![
                        Inline::Link {
                            content: vec![Inline::Text("a".to_string())],
                            href: Url("/b-post".to_string()),
                            title: None,
                            attrs: InlineAttrs::default(),
                        },
                        Inline::Link {
                            content: vec![Inline::Text("a2".to_string())],
                            href: Url("/a-post".to_string()),
                            title: None,
                            attrs: InlineAttrs::default(),
                        },
                        Inline::Link {
                            content: vec![Inline::Text("external".to_string())],
                            href: Url("https://example.com".to_string()),
                            title: None,
                            attrs: InlineAttrs::default(),
                        },
                    ],
                    attrs: BlockAttrs::default(),
                },
                Block::UnknownBlock {
                    tag: "x".to_string(),
                    attrs: BlockAttrs::default(),
                    children: vec![UnknownChild::Inline(Inline::Link {
                        content: vec![Inline::Text("dup".to_string())],
                        href: Url("/a-post".to_string()),
                        title: None,
                        attrs: InlineAttrs::default(),
                    })],
                    data: BTreeMap::new(),
                    note: None,
                    source: None,
                },
            ],
            footnotes: BTreeMap::new(),
            assets: BTreeMap::new(),
            meta: DocMeta::default(),
        };

        let targets = collect_internal_link_targets(&doc);
        assert_eq!(targets, vec!["a-post".to_string(), "b-post".to_string()]);
    }

    #[test]
    fn link_pass_rewrites_internal_links_when_resolved() {
        let mut doc = simple_doc_with_link("/hello/index.html?x=1#y", "Hello");
        let mut resolver = MockResolver::default();
        resolver.any_platform.insert(
            "hello".to_string(),
            "https://example.com/posts/hello".to_string(),
        );

        let mut pass = ResolveInternalLinksPass::new(&resolver, Some("ghost"));
        let mut ctx = PassCtx::default();
        pass.run(&mut doc, &mut ctx).expect("run link pass");

        match &doc.blocks[0] {
            Block::Paragraph { content, .. } => {
                assert!(
                    matches!(
                        &content[0],
                        Inline::Link { href, .. } if href.0 == "https://example.com/posts/hello?x=1#y"
                    ),
                    "link should be rewritten with suffix preserved"
                );
            }
            other => panic!("unexpected block: {other:?}"),
        }
        assert!(ctx.diagnostics.is_empty());
    }

    #[test]
    fn link_pass_degrades_unresolved_links_to_text_by_default() {
        let mut doc = simple_doc_with_link("/missing", "Missing target");
        let resolver = MockResolver::default();
        let mut pass = ResolveInternalLinksPass::new(&resolver, Some("ghost"));
        let mut ctx = PassCtx::default();
        pass.run(&mut doc, &mut ctx).expect("run link pass");

        match &doc.blocks[0] {
            Block::Paragraph { content, .. } => {
                assert!(matches!(&content[0], Inline::Text(t) if t == "Missing target"));
            }
            other => panic!("unexpected block: {other:?}"),
        }
        assert_eq!(ctx.diagnostics.len(), 1);
        assert_eq!(ctx.diagnostics[0].level, DiagnosticLevel::Warning);
    }

    #[test]
    fn link_pass_can_keep_unresolved_link() {
        let mut doc = simple_doc_with_link("/missing", "Missing target");
        let resolver = MockResolver::default();
        let mut pass = ResolveInternalLinksPass::new(&resolver, None)
            .with_unresolved_behavior(UnresolvedInternalLinkBehavior::KeepLink);
        let mut ctx = PassCtx::default();
        pass.run(&mut doc, &mut ctx).expect("run link pass");

        match &doc.blocks[0] {
            Block::Paragraph { content, .. } => {
                assert!(matches!(&content[0], Inline::Link { href, .. } if href.0 == "/missing"));
            }
            other => panic!("unexpected block: {other:?}"),
        }
        assert_eq!(ctx.diagnostics.len(), 1);
    }

    #[test]
    fn link_pass_leaves_non_internal_links_unchanged() {
        let mut doc = simple_doc_with_link("https://example.com/path", "External");
        let resolver = MockResolver::default();
        let mut pass = ResolveInternalLinksPass::new(&resolver, Some("ghost"));
        let mut ctx = PassCtx::default();
        pass.run(&mut doc, &mut ctx).expect("run link pass");

        match &doc.blocks[0] {
            Block::Paragraph { content, .. } => {
                assert!(matches!(
                    &content[0],
                    Inline::Link { href, .. } if href.0 == "https://example.com/path"
                ));
            }
            other => panic!("unexpected block: {other:?}"),
        }
        assert!(ctx.diagnostics.is_empty());
    }
}