mbr-markdown-browser 0.4.5

A fast, featureful markdown viewer, browser, and (optional) static site generator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
use crate::media::MediaEmbed;
use regex::Regex;
use reqwest::Client;
use scraper::{ElementRef, Html, Selector};
use std::error::Error;
use std::sync::LazyLock;
use std::time::Duration;

/// Formats an error with its full source chain for detailed logging.
/// Walks the `source()` chain to reveal nested errors (e.g., reqwest -> hyper -> io).
fn format_error_chain(err: &dyn Error) -> String {
    let mut chain = vec![err.to_string()];
    let mut current = err.source();
    while let Some(source) = current {
        chain.push(source.to_string());
        current = source.source();
    }
    chain.join(" -> ")
}

// Precompiled selectors for efficient HTML parsing
// All metadata lives in <head>, so we scope our searches there
static HEAD_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("head").expect("Invalid head selector"));
static TITLE_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("title").expect("Invalid title selector"));

// OpenGraph meta tags (priority sources)
static OG_TITLE_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("meta[property='og:title']").expect("Invalid og:title selector")
});
static OG_IMAGE_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("meta[property='og:image']").expect("Invalid og:image selector")
});
static OG_DESCRIPTION_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("meta[property='og:description']").expect("Invalid og:description selector")
});

// Fallback selectors
static META_DESCRIPTION_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("meta[name='description']").expect("Invalid meta description selector")
});
static FAVICON_SELECTOR: LazyLock<Selector> =
    LazyLock::new(|| Selector::parse("link[rel='icon']").expect("Invalid favicon selector"));
static ALT_FAVICON_SELECTOR: LazyLock<Selector> = LazyLock::new(|| {
    Selector::parse("link[rel='alternate icon']").expect("Invalid alternate favicon selector")
});

/// Supported image types for favicons
const SUPPORTED_FAVICON_TYPES: &[&str] = &[
    "image/png",
    "image/jpeg",
    "image/jpg",
    "image/svg+xml",
    "image/x-icon",
    "image/vnd.microsoft.icon",
];

/// Check if a favicon type is supported (or if no type is specified, assume supported)
fn is_supported_favicon_type(type_attr: Option<&str>) -> bool {
    type_attr.is_none_or(|t| SUPPORTED_FAVICON_TYPES.contains(&t))
}

/// Extract content attribute from first matching element, HTML-escaped
fn extract_content(head: &ElementRef, selector: &Selector) -> Option<String> {
    head.select(selector)
        .next()
        .and_then(|el| el.value().attr("content"))
        .map(|s| html_escape::encode_text(s).to_string())
}

/// Extract favicon href from first matching element with a supported image type
fn extract_favicon(head: &ElementRef, selector: &Selector) -> Option<String> {
    head.select(selector)
        .find(|el| is_supported_favicon_type(el.value().attr("type")))
        .and_then(|el| el.value().attr("href"))
        .map(|s| s.to_string())
}

// Giphy regex patterns - compiled once for efficiency
static GIPHY_MEDIA_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"https?://(?:media\d*|i)\.giphy\.com/").unwrap());
static GIPHY_PAGE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"https?://(?:www\.)?giphy\.com/gifs/(?:[^/]+-)?([a-zA-Z0-9]+)(?:\?.*)?$").unwrap()
});

// GIST regex patterns
static GIST_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"https?://gist\.github\.com/").unwrap());

#[derive(Default, Clone)]
pub struct PageInfo {
    pub url: String,
    pub title: Option<String>,
    pub description: Option<String>,
    pub image: Option<String>,
    pub embed_html: Option<String>, // For YouTube and other embeddable content
}

impl PageInfo {
    /// Estimates the memory size of this PageInfo in bytes.
    ///
    /// Used by OembedCache for size-based eviction. This is an approximation
    /// that accounts for the struct overhead plus the heap-allocated strings.
    pub fn estimated_size(&self) -> usize {
        std::mem::size_of::<Self>()
            + self.url.len()
            + self.title.as_ref().map_or(0, |s| s.len())
            + self.description.as_ref().map_or(0, |s| s.len())
            + self.image.as_ref().map_or(0, |s| s.len())
            + self.embed_html.as_ref().map_or(0, |s| s.len())
    }

    /// Check if URL is a Giphy URL and create embed HTML
    /// Matches patterns like:
    /// - https://media.giphy.com/media/ID/giphy.gif
    /// - https://media1.giphy.com/media/.../giphy.gif (or .webp)
    /// - https://giphy.com/gifs/name-ID
    /// - https://i.giphy.com/ID.gif
    fn giphy_embed(url: &str) -> Option<String> {
        // Pattern for direct media URLs (media.giphy.com, media1.giphy.com, i.giphy.com)
        // These are direct image files - embed as img tags
        if GIPHY_MEDIA_RE.is_match(url) {
            return Some(format!(
                r#"<figure class="giphy-embed">
                    <img src="{}" alt="Giphy animation" loading="lazy" />
                </figure>"#,
                url
            ));
        }

        // Pattern for giphy.com/gifs/... URLs - extract the ID and convert to media URL
        // Format: https://giphy.com/gifs/description-ID or https://giphy.com/gifs/ID
        if let Some(caps) = GIPHY_PAGE_RE.captures(url)
            && let Some(id) = caps.get(1)
        {
            let gif_url = format!("https://media.giphy.com/media/{}/giphy.gif", id.as_str());
            return Some(format!(
                r#"<figure class="giphy-embed">
                    <img src="{}" alt="Giphy animation" loading="lazy" />
                </figure>"#,
                gif_url
            ));
        }

        None
    }

    /// Check if URL is a gist URL and create embed HTML
    /// Matches patterns like:
    /// - https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea
    /// - https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea#file-order-ts
    fn gist_embed(url: &str) -> Option<String> {
        if GIST_RE.is_match(url) {
            // Handle fragment - .js must come before #fragment
            let script_url = if let Some(hash_pos) = url.find('#') {
                let (path, fragment) = url.split_at(hash_pos);
                format!("{}.js{}", path, fragment)
            } else {
                format!("{}.js", url)
            };
            return Some(format!(r#"<script src="{}"></script>"#, script_url));
        }
        None
    }

    pub async fn new_from_url(
        url: &str,
        timeout_ms: u64,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        // If timeout is 0, oembed is disabled - return a plain link without network call or substitution
        if timeout_ms == 0 {
            // tracing::debug!("Oembed disabled, ignoring url {}", &url);
            return Ok(PageInfo {
                url: url.to_string(),
                ..Default::default()
            });
        }

        // Check for Giphy - embed directly without fetching
        if let Some(embed_html) = Self::giphy_embed(url) {
            return Ok(PageInfo {
                url: url.to_string(),
                embed_html: Some(embed_html),
                ..Default::default()
            });
        }

        // Check for GitHub gist - embed without fetching
        if let Some(embed_html) = Self::gist_embed(url) {
            return Ok(PageInfo {
                url: url.to_string(),
                embed_html: Some(embed_html),
                ..Default::default()
            });
        }

        // Check for media extension (video/audio/PDF) - embed without fetching
        // Parameters: server_mode=false (safe default for static builds),
        // transcode_enabled=true, hls_enabled=false
        if let Some(media) = MediaEmbed::from_bare_url(url) {
            return Ok(PageInfo {
                url: url.to_string(),
                embed_html: Some(media.to_html(false, true, false)),
                ..Default::default()
            });
        }

        let client = crate::http_client(Duration::from_millis(timeout_ms));

        // For other URLs, fetch and parse OpenGraph metadata
        match Self::fetch_page_info(&client, url).await {
            Ok(info) => Ok(info),
            Err(e) => {
                // Any error (timeout, network, etc.) - return a plain link
                tracing::warn!(
                    "Error fetching URL ({}) for enriched display: {}",
                    &url,
                    format_error_chain(e.as_ref())
                );
                Ok(PageInfo {
                    url: url.to_string(),
                    ..Default::default()
                })
            }
        }
    }

    async fn fetch_page_info(
        client: &Client,
        url: &str,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let response = client.get(url).send().await?;

        // Check for Cloudflare challenge block
        if response
            .headers()
            .get("cf-mitigated")
            .is_some_and(|v| v == "challenge")
        {
            return Err("Blocked by Cloudflare challenge".into());
        }

        let body = response.text().await?;
        let document = Html::parse_document(&body);

        // All metadata lives in <head> - extract with og:* priority, then fallbacks
        let (title, description, image) = document
            .select(&HEAD_SELECTOR)
            .next()
            .map(|head| {
                // Priority: og:* tags first
                let og_title = extract_content(&head, &OG_TITLE_SELECTOR);
                let og_desc = extract_content(&head, &OG_DESCRIPTION_SELECTOR);
                let og_image = extract_content(&head, &OG_IMAGE_SELECTOR);

                // Fallbacks only computed if og:* not found
                let title = og_title.or_else(|| {
                    head.select(&TITLE_SELECTOR)
                        .next()
                        .map(|el| el.text().collect::<String>())
                        .filter(|s| !s.is_empty())
                        .map(|s| html_escape::encode_text(&s).to_string())
                });

                let description =
                    og_desc.or_else(|| extract_content(&head, &META_DESCRIPTION_SELECTOR));

                let image = og_image
                    .or_else(|| extract_favicon(&head, &FAVICON_SELECTOR))
                    .or_else(|| extract_favicon(&head, &ALT_FAVICON_SELECTOR));

                (title, description, image)
            })
            .unwrap_or((None, None, None));

        Ok(PageInfo {
            url: url.to_string(),
            title,
            description,
            image,
            embed_html: None,
        })
    }

    pub fn text(&self) -> String {
        format!(
            "{}: {}",
            self.title.clone().unwrap_or("no title".to_string()),
            self.url
        )
    }

    pub fn html(&self) -> String {
        // If we have embed HTML (e.g., YouTube), use that
        if let Some(embed) = &self.embed_html {
            return embed.clone();
        }

        // Otherwise, create a link (TODO: make this a rich card with image/description)
        if let Some(title) = self.title.clone() {
            let img_tag = self
                .image
                .as_ref()
                .map(|src| format!("<img src='{}'/>", src))
                .unwrap_or_default();
            format!(
                "<article class='mbr-social-link-box'>
                    {}
                    <a href='{}' class='mbr-social-link'>
                        <header>{}</header>
                        <p>{}</p>
                    </a>    
                </article>
                ",
                img_tag,
                &self.url,
                title,
                self.description.as_deref().unwrap_or(""),
            )
        } else {
            format!("<a href='{}'>{}</a>", &self.url, &self.url,)
        }
    }
}

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

    #[test]
    fn test_giphy_media_url() {
        let url = "https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExaThmbzNhb3duNGM5NGVxemo4aHlnYTM1YXA4cGxmc2l2ejdjc2s4ZCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/CAxbo8KC2A0y4/giphy.gif";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("giphy-embed"));
        assert!(html.contains(url));
    }

    #[test]
    fn test_giphy_media_url_without_version() {
        let url = "https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.gif";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("giphy-embed"));
        assert!(html.contains(url));
    }

    #[test]
    fn test_giphy_i_url() {
        let url = "https://i.giphy.com/CAxbo8KC2A0y4.gif";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("giphy-embed"));
        assert!(html.contains(url));
    }

    #[test]
    fn test_giphy_page_url() {
        let url = "https://giphy.com/gifs/cat-funny-CAxbo8KC2A0y4";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("giphy-embed"));
        assert!(html.contains("https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.gif"));
    }

    #[test]
    fn test_giphy_page_url_simple() {
        let url = "https://giphy.com/gifs/CAxbo8KC2A0y4";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.gif"));
    }

    #[test]
    fn test_non_giphy_url() {
        let url = "https://example.com/image.gif";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_none());
    }

    #[test]
    fn test_youtube_not_giphy() {
        let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_none());
    }

    #[tokio::test]
    async fn test_zero_timeout_returns_plain_link() {
        // With timeout=0, should return plain link without network call
        let url = "https://example.com/some-page";
        let result = PageInfo::new_from_url(url, 0).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert_eq!(info.url, url);
        assert!(info.title.is_none()); // No network call, so no title fetched
        assert!(info.embed_html.is_none());
        // html() should return a plain link
        assert!(info.html().contains("<a href="));
        assert!(info.html().contains(url));
    }

    #[tokio::test]
    async fn test_zero_timeout_disables_youtube_embed() {
        // With timeout=0, ALL enrichment is disabled - even no-network embeds like YouTube
        let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
        let result = PageInfo::new_from_url(url, 0).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_none());
        // Should return a plain link instead
        assert!(info.html().contains("<a href="));
    }

    #[tokio::test]
    async fn test_zero_timeout_disables_giphy_embed() {
        // With timeout=0, ALL enrichment is disabled - even no-network embeds like Giphy
        let url = "https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.gif";
        let result = PageInfo::new_from_url(url, 0).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_none());
        // Should return a plain link instead
        assert!(info.html().contains("<a href="));
    }

    #[test]
    fn test_estimated_size_empty() {
        let info = PageInfo::default();
        // Should at least include the struct size
        assert!(info.estimated_size() >= std::mem::size_of::<PageInfo>());
    }

    #[test]
    fn test_estimated_size_with_fields() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            title: Some("Test Title".to_string()),
            description: Some("A longer description text".to_string()),
            image: Some("https://example.com/image.png".to_string()),
            embed_html: None,
        };
        let size = info.estimated_size();
        // Size should include all the string lengths
        let expected_min = std::mem::size_of::<PageInfo>()
            + info.url.len()
            + info.title.as_ref().unwrap().len()
            + info.description.as_ref().unwrap().len()
            + info.image.as_ref().unwrap().len();
        assert_eq!(size, expected_min);
    }

    #[test]
    fn test_clone() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            title: Some("Test".to_string()),
            description: None,
            image: None,
            embed_html: Some("<div></div>".to_string()),
        };
        let cloned = info.clone();
        assert_eq!(cloned.url, info.url);
        assert_eq!(cloned.title, info.title);
        assert_eq!(cloned.embed_html, info.embed_html);
    }

    #[test]
    fn test_gist_embed_basic() {
        let url = "https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea";
        let embed = PageInfo::gist_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains(
            r#"<script src="https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea.js"></script>"#
        ));
    }

    #[test]
    fn test_gist_embed_with_fragment() {
        let url = "https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea#file-order-ts";
        let embed = PageInfo::gist_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        // .js should come BEFORE the fragment
        assert!(html.contains(
            r#"<script src="https://gist.github.com/rpinna/b97f8505940f255e8ebbd9a17c76f3ea.js#file-order-ts"></script>"#
        ));
    }

    #[test]
    fn test_gist_embed_http() {
        let url = "http://gist.github.com/user/abc123";
        let embed = PageInfo::gist_embed(url);
        assert!(embed.is_some());
    }

    #[test]
    fn test_gist_non_gist_url() {
        let url = "https://github.com/user/repo";
        let embed = PageInfo::gist_embed(url);
        assert!(embed.is_none());
    }

    #[test]
    fn test_gist_not_youtube() {
        let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
        let embed = PageInfo::gist_embed(url);
        assert!(embed.is_none());
    }

    // Bare media URL tests
    #[tokio::test]
    async fn test_bare_mp4_url_returns_video_embed() {
        let url = "https://example.com/videos/demo.mp4";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_some());
        let html = info.embed_html.unwrap();
        assert!(html.contains("<video"));
        assert!(html.contains(url));
    }

    #[tokio::test]
    async fn test_bare_mp3_url_returns_audio_embed() {
        let url = "https://example.com/audio/podcast.mp3";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_some());
        let html = info.embed_html.unwrap();
        assert!(html.contains("<audio"));
        assert!(html.contains(url));
    }

    #[tokio::test]
    async fn test_bare_pdf_url_returns_pdf_embed() {
        let url = "https://example.com/docs/report.pdf";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_some());
        let html = info.embed_html.unwrap();
        assert!(html.contains("pdf-embed"));
        assert!(html.contains(r#"type="application/pdf""#));
        assert!(html.contains(url));
    }

    #[tokio::test]
    async fn test_mp4_in_path_but_not_extension_goes_to_opengraph() {
        // URL has .mp4 in the path but not as the file extension
        // This should NOT match media detection and should proceed to OpenGraph
        let url = "https://example.com/videos/mp4-format/info";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        // Should NOT have embed_html (or if it times out, it's a plain link)
        // The key is that it shouldn't detect as video
        if let Some(html) = &info.embed_html {
            assert!(!html.contains("<video"));
        }
    }

    #[tokio::test]
    async fn test_bare_m4v_url_returns_video_embed() {
        // .m4v is a supported video extension (unlike .mov which Vid doesn't detect)
        let url = "https://example.com/videos/clip.m4v";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_some());
        let html = info.embed_html.unwrap();
        assert!(html.contains("<video"));
    }

    #[tokio::test]
    async fn test_bare_wav_url_returns_audio_embed() {
        let url = "https://example.com/sounds/effect.wav";
        let result = PageInfo::new_from_url(url, 500).await;
        assert!(result.is_ok());
        let info = result.unwrap();
        assert!(info.embed_html.is_some());
        let html = info.embed_html.unwrap();
        assert!(html.contains("<audio"));
    }

    // Tests for format_error_chain
    #[test]
    fn test_format_error_chain_single() {
        let err = std::io::Error::other("simple error");
        let chain = format_error_chain(&err);
        assert_eq!(chain, "simple error");
    }

    #[test]
    fn test_format_error_chain_nested() {
        // Create a nested error chain using Box<dyn Error>
        #[derive(Debug)]
        struct OuterError {
            source: Box<dyn Error + Send + Sync>,
        }
        impl std::fmt::Display for OuterError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "outer error")
            }
        }
        impl Error for OuterError {
            fn source(&self) -> Option<&(dyn Error + 'static)> {
                Some(self.source.as_ref())
            }
        }

        let inner = std::io::Error::other("inner error");
        let outer = OuterError {
            source: Box::new(inner),
        };
        let chain = format_error_chain(&outer);
        assert!(chain.contains("outer error"));
        assert!(chain.contains("inner error"));
        assert!(chain.contains(" -> "));
    }

    // Tests for is_supported_favicon_type
    #[test]
    fn test_supported_favicon_type_none() {
        // No type attribute means assume supported
        assert!(is_supported_favicon_type(None));
    }

    #[test]
    fn test_supported_favicon_type_png() {
        assert!(is_supported_favicon_type(Some("image/png")));
    }

    #[test]
    fn test_supported_favicon_type_svg() {
        assert!(is_supported_favicon_type(Some("image/svg+xml")));
    }

    #[test]
    fn test_supported_favicon_type_ico() {
        assert!(is_supported_favicon_type(Some("image/x-icon")));
        assert!(is_supported_favicon_type(Some("image/vnd.microsoft.icon")));
    }

    #[test]
    fn test_unsupported_favicon_type() {
        assert!(!is_supported_favicon_type(Some("image/webp")));
        assert!(!is_supported_favicon_type(Some("text/html")));
    }

    // Tests for PageInfo::text()
    #[test]
    fn test_text_with_title() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            title: Some("My Title".to_string()),
            ..Default::default()
        };
        let text = info.text();
        assert_eq!(text, "My Title: https://example.com");
    }

    #[test]
    fn test_text_without_title() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            ..Default::default()
        };
        let text = info.text();
        assert_eq!(text, "no title: https://example.com");
    }

    // Tests for PageInfo::html()
    #[test]
    fn test_html_with_embed() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            embed_html: Some("<div>embedded</div>".to_string()),
            ..Default::default()
        };
        let html = info.html();
        assert_eq!(html, "<div>embedded</div>");
    }

    #[test]
    fn test_html_with_title_and_image() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            title: Some("My Title".to_string()),
            description: Some("My description".to_string()),
            image: Some("https://example.com/image.png".to_string()),
            embed_html: None,
        };
        let html = info.html();
        assert!(html.contains("mbr-social-link-box"));
        assert!(html.contains("My Title"));
        assert!(html.contains("My description"));
        assert!(html.contains("<img src='https://example.com/image.png'/>"));
    }

    #[test]
    fn test_html_with_title_no_image() {
        let info = PageInfo {
            url: "https://example.com".to_string(),
            title: Some("Just Title".to_string()),
            description: None,
            image: None,
            embed_html: None,
        };
        let html = info.html();
        assert!(html.contains("mbr-social-link-box"));
        assert!(html.contains("Just Title"));
        assert!(!html.contains("<img"));
    }

    #[test]
    fn test_html_no_title_plain_link() {
        let info = PageInfo {
            url: "https://example.com/page".to_string(),
            title: None,
            description: Some("ignored without title".to_string()),
            image: None,
            embed_html: None,
        };
        let html = info.html();
        // Should be a plain link
        assert!(html.contains("<a href='https://example.com/page'>"));
        assert!(html.contains("https://example.com/page</a>"));
        assert!(!html.contains("mbr-social-link-box"));
    }

    // Tests for Giphy URL with query params
    #[test]
    fn test_giphy_page_url_with_query() {
        let url = "https://giphy.com/gifs/cat-funny-CAxbo8KC2A0y4?utm_source=test";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains("https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.gif"));
    }

    #[test]
    fn test_giphy_webp_extension() {
        let url = "https://media.giphy.com/media/CAxbo8KC2A0y4/giphy.webp";
        let embed = PageInfo::giphy_embed(url);
        assert!(embed.is_some());
        let html = embed.unwrap();
        assert!(html.contains(url));
    }
}