crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! Markdown 后处理器
//!
//! 从 [`crate::workers::scrape_worker::ScrapeWorker`] 抽取的 markdown 后处理逻辑,
//! 遵循 SRP:ScrapeWorker 专注任务调度,markdown 转换由本类型独立负责。
//!
//! # 职责
//!
//! 根据任务请求的 `formats` 字段判断是否需要生成 Markdown,
//! 命中 `"markdown"` 则通过注入的 `MarkdownConverter` 转换 HTML → Markdown。
//!
//! # 依赖倒置(DIP)
//!
//! 本类型通过 `Arc<dyn MarkdownConverter>` 注入转换器,而非硬编码
//! `HtmdMarkdownService`,便于测试时 mock 替换,未来扩展其他实现时无需修改源码。
//!
//! # 特性门控
//!
//! 本模块整体 gated `content`(与 [`crate::domain::services::markdown_service`] 一致)。
//! 关闭 `content` 特性时本模块不编译,ScrapeWorker 也不持有本类型的字段。

#![cfg(feature = "content")]

use crate::application::dto::scrape_request::ScrapeRequestDto;
use crate::domain::services::content_extractor::ContentExtractionFacade;
use crate::domain::services::markdown_service::{MarkdownConverter, MarkdownError};
use log::{debug, warn};
use std::sync::Arc;

/// Markdown 后处理错误(错误显性化)
///
/// 原实现 `generate()` 返回 `Option<String>`,将三类不同语义的 `None` 混淆:
/// 1. 未请求 markdown(非错误,应返回 `Ok(None)`)
/// 2. 转换失败(应返回 `Err(ConversionFailed)`)
/// 3. 转换结果为空(应返回 `Err(EmptyResult)`)
///
/// 现通过显式错误类型让调用方区分语义,符合“失败必须显性化”原则。
/// 调用方可根据业务策略决定是否阻断主流程(markdown 为增强字段,
/// 失败不阻断基础抓取结果,调用方应记录错误并继续)。
///
/// # 类型化错误传递(#7)
///
/// `ConversionFailed` 使用 `#[from] MarkdownError` 保留下层错误类型,
/// 调用方可通过 `downcast_ref::<MarkdownError>` 拿到原始错误做精细处理,
/// 而非仅拿到字符串化后的错误消息。
#[derive(Debug, thiserror::Error)]
pub enum MarkdownPostProcessorError {
    /// HTML → Markdown 转换失败(底层 `MarkdownConverter::convert` 返回错误)
    ///
    /// 通过 `#[from]` 自动实现 `From<MarkdownError>`,保留下层错误类型信息。
    #[error("markdown conversion failed: {0}")]
    ConversionFailed(#[from] MarkdownError),
    /// 转换结果为空(HTML 解析后无可见文本,可能是空 HTML 或纯空白内容)
    #[error("markdown conversion returned empty result")]
    EmptyResult,
}

/// Markdown 后处理器
///
/// 持有注入的 `MarkdownConverter` 实例(通过 `Arc` 共享),可安全在多处共享单例。
///
/// # 用法
///
/// ```no_run
/// # use crawlrs::application::dto::scrape_request::ScrapeRequestDto;
/// # use crawlrs::workers::markdown_post_processor::MarkdownPostProcessor;
/// # use crawlrs::domain::services::markdown_service::HtmdMarkdownService;
/// # use crawlrs::domain::services::content_extractor::ContentExtractionFacade;
/// # use serde_json::json;
/// # use std::sync::Arc;
/// let processor = MarkdownPostProcessor::new(
///     Arc::new(HtmdMarkdownService::new()),
///     Some(Arc::new(ContentExtractionFacade::new(None))),
///);
/// let req: ScrapeRequestDto = serde_json::from_value(json!({
///     "url": "https://example.com",
///     "formats": ["markdown"]
/// })).unwrap();
/// // generate is async — use .await in async context
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let md = processor.generate(crawlrs::domain::id::Id::generate(), &req, "<html><body><h1>Hi</h1></body></html>").await;
/// assert!(md.unwrap().is_some());
/// # });
/// ```
#[derive(Clone)]
pub struct MarkdownPostProcessor {
    /// 注入的 Markdown 转换服务(DIP:依赖抽象而非具体实现)
    markdown_converter: Arc<dyn MarkdownConverter>,
    /// 可选的正文提取门面
    ///
    /// `only_main_content=true` 时先经此提取正文,再转 Markdown。
    /// `None` 时跳过正文提取,整页转 Markdown。
    content_extractor: Option<Arc<ContentExtractionFacade>>,
}

/// only_main_content 时的正文来源(自 [`MarkdownPostProcessor::generate`] 提取)。
enum MainContentSource {
    /// 结构化正文 HTML
    StructuredHtml(String),
    /// 纯文本(抽取器未产出正文 HTML,显性降级)
    PlainText(String),
    /// 整页 HTML
    FullHtml,
}

impl MarkdownPostProcessor {
    /// 创建新的 Markdown 后处理器
    ///
    /// # 参数
    ///
    /// - `markdown_converter`:Markdown 转换器(实现 `MarkdownConverter` trait),
    ///   默认注入 alchemize html 引擎(`markdown::build_converter` 默认装配)。
    ///   通过 `Arc` 共享,多个 worker 可共用同一实例。
    /// - `content_extractor`:可选的正文提取门面。
    ///   `only_main_content=true` 时先提取正文再转 Markdown;`None` 时跳过。
    #[must_use]
    pub fn new(
        markdown_converter: Arc<dyn MarkdownConverter>,
        content_extractor: Option<Arc<ContentExtractionFacade>>,
    ) -> Self {
        Self {
            markdown_converter,
            content_extractor,
        }
    }

    /// 根据任务请求生成 Markdown(如 `formats` 含 `"markdown"`)
    ///
    /// # 参数
    ///
    /// - `task_id`:任务 ID(仅用于日志)
    /// - `req`:已解析的 [`ScrapeRequestDto`],读取 `formats` 字段
    /// - `content`:待转换的 HTML 内容
    ///
    /// # 返回值(错误显性化)
    ///
    /// - `Ok(Some(md))`:成功生成非空 Markdown
    /// - `Ok(None)`:任务未请求 markdown(非错误,调用方应跳过)
    /// - `Err(ConversionFailed(e))`:底层转换服务返回错误
    /// - `Err(EmptyResult)`:转换结果为空(HTML 无可见文本)
    ///
    /// # 调用方策略
    ///
    /// markdown 为增强字段,失败不阻断基础抓取结果。调用方应:
    /// ```ignore
    /// match processor.generate(task_id, req, content) {
    ///     Ok(Some(md)) => { /* 存储 markdown */ }
    ///     Ok(None) => { /* 未请求,跳过 */ }
    ///     Err(e) => {
    ///         warn!("markdown post-processing failed: {e}");
    ///         /* 继续不阻断主流程 */
    ///     }
    /// }
    /// ```
    pub async fn generate(
        &self,
        task_id: crate::domain::id::Id,
        req: &ScrapeRequestDto,
        content: &str,
    ) -> Result<Option<String>, MarkdownPostProcessorError> {
        let want_markdown = req
            .formats
            .as_ref()
            .map(|fs| fs.iter().any(|f| f == "markdown"))
            .unwrap_or(false);

        if !want_markdown {
            return Ok(None);
        }

        let content_source = self
            .resolve_main_content_source(task_id, req, content)
            .await;

        // 结构化正文与整页 HTML 走 Markdown 转换;纯文本没有可转换的结构,
        // 直接作为 Markdown 正文(按值消费避免整文深拷贝;行首元字符做最小
        // 转义防下游误渲染为标题/列表——见 escape_leading_markdown_chars)。
        let markdown_result = self.convert_main_content_to_markdown(content, req, content_source);

        match markdown_result {
            Ok(md) if !md.trim().is_empty() => {
                debug!(
                    "task_id: {}, Markdown generated ({} bytes)",
                    task_id,
                    md.len()
                );
                Ok(Some(md))
            }
            Ok(_md) => {
                debug!(
                    "task_id: {}, Markdown conversion returned empty result",
                    task_id
                );
                Err(MarkdownPostProcessorError::EmptyResult)
            }
            Err(e) => {
                // #7:保留下层 MarkdownError 类型(通过 #[from] 自动转换),
                // 调用方可 downcast_ref 拿到原始错误做精细处理。
                warn!("task_id: {}, Markdown conversion failed: {}", task_id, e);
                Err(MarkdownPostProcessorError::ConversionFailed(e))
            }
        }
    }

    /// 解析正文来源(自 [`Self::generate`] 提取):`only_main_content=true`
    /// 时优先用保留结构的正文 HTML,失败/缺失显性降级。
    async fn resolve_main_content_source(
        &self,
        task_id: crate::domain::id::Id,
        req: &ScrapeRequestDto,
        content: &str,
    ) -> MainContentSource {
        // only_main_content=true 时先提取正文
        let only_main = req
            .options
            .as_ref()
            .and_then(|o| o.only_main_content)
            .unwrap_or(false);

        // P16:优先用保留结构的正文 HTML(表格/链接/代码块不丢)。
        // `ExtractedContent.text` 是纯文本——纯文本喂 Markdown 转换器
        // 会把结构信息全部抹平。
        if only_main {
            if let Some(extractor) = &self.content_extractor {
                match extractor.extract(content, &req.url).await {
                    Ok(extracted) if !extracted.text.trim().is_empty() => {
                        debug!(
                            "task_id: {}, main content extracted (confidence={:.2}, {} bytes)",
                            task_id,
                            extracted.confidence,
                            extracted.text.len()
                        );
                        match extracted.html.filter(|h| !h.trim().is_empty()) {
                            Some(html) => MainContentSource::StructuredHtml(html),
                            None => {
                                // 降级必须显性:纯文本转 Markdown 无结构
                                warn!(
                                    "task_id: {}, extractor produced no main-content HTML; \
                                     Markdown will be structureless (plain text)",
                                    task_id
                                );
                                MainContentSource::PlainText(extracted.text)
                            }
                        }
                    }
                    Ok(_) => {
                        warn!(
                                "task_id: {}, content extraction returned empty, falling back to full HTML",
                                task_id
                            );
                        MainContentSource::FullHtml
                    }
                    Err(e) => {
                        warn!(
                            "task_id: {}, content extraction failed: {}, falling back to full HTML",
                            task_id, e
                        );
                        MainContentSource::FullHtml
                    }
                }
            } else {
                MainContentSource::FullHtml
            }
        } else {
            MainContentSource::FullHtml
        }
    }

    /// 按正文来源执行 Markdown 转换(自 [`Self::generate`] 提取)。
    fn convert_main_content_to_markdown(
        &self,
        content: &str,
        req: &ScrapeRequestDto,
        content_source: MainContentSource,
    ) -> Result<String, MarkdownError> {
        match content_source {
            MainContentSource::PlainText(text) => Ok(escape_leading_markdown_chars(&text)),
            MainContentSource::StructuredHtml(html) => {
                // 注意:rs-trafilatura 0.2.2 的 content_html 不做相对→绝对
                // URL 转换(上游 convert_tags 为死代码),先以页面 URL 为基重写
                let absolute = absoluteize_links(&html, &req.url);
                self.markdown_converter.convert(&absolute)
            }
            MainContentSource::FullHtml => self.markdown_converter.convert(content),
        }
    }
}

/// 把 HTML 中相对的 href/src 重写为基于 `base_url` 的绝对 URL
///
/// rs-trafilatura 0.2.2 的 `content_html` 不做相对→绝对转换(上游
/// `convert_tags` 为死代码),相对链接直接进 Markdown 会产出下游无法
/// 解析的 `[text](/relative/path)`。锚点/协议相对/数据/邮件等非相对目标
/// 原样保留;base 解析失败时原样返回。
fn absoluteize_links(html: &str, base_url: &str) -> String {
    use regex::Regex;

    let base = match url::Url::parse(base_url) {
        Ok(b) => b,
        Err(_) => return html.to_string(),
    };
    let attr_re = match Regex::new(r#"(?i)(\s(?:href|src)=")([^"]*)(")"#) {
        Ok(r) => r,
        Err(_) => return html.to_string(),
    };
    attr_re
        .replace_all(html, |caps: &regex::Captures| {
            let value = &caps[2];
            let is_special = value.is_empty()
                || value.starts_with('#')
                || value.starts_with("//")
                || value.starts_with("mailto:")
                || value.starts_with("tel:")
                || value.starts_with("javascript:")
                || value.starts_with("data:");
            if is_special {
                return caps[0].to_string();
            }
            match base.join(value) {
                Ok(absolute) => format!("{}{}{}", &caps[1], absolute, &caps[3]),
                // join 失败(如 base 与 value 组合非法)保留原值
                Err(_) => caps[0].to_string(),
            }
        })
        .into_owned()
}

/// 纯文本降级分支的最小 Markdown 转义——仅转义行首会误渲染为
/// 结构的字符(ATX 标题 #、引用 >、无序列表 - + *、有序列表 "1.")
fn escape_leading_markdown_chars(text: &str) -> String {
    fn needs_escape(trimmed: &str) -> bool {
        if trimmed.starts_with('#') || trimmed.starts_with('>') {
            return true;
        }
        for marker in ["- ", "+ ", "* "] {
            if trimmed.starts_with(marker) {
                return true;
            }
        }
        // 有序列表:行首 "数字."(如 "1. " / "42. ")
        match trimmed.split_once('.') {
            Some((num, rest)) if !num.is_empty() && num.chars().all(|c| c.is_ascii_digit()) => {
                rest.starts_with(' ') || rest.is_empty()
            }
            _ => false,
        }
    }

    let mut out = String::with_capacity(text.len());
    for line in text.split_inclusive('\n') {
        let leading = line.len() - line.trim_start().len();
        if needs_escape(line.trim_start()) {
            out.push_str(&line[..leading]);
            out.push('\\');
            out.push_str(&line[leading..]);
        } else {
            out.push_str(line);
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::services::markdown_service::HtmdMarkdownService;
    use serde_json::json;

    fn make_req(payload: serde_json::Value) -> ScrapeRequestDto {
        serde_json::from_value(payload).expect("valid ScrapeRequestDto")
    }

    fn make_processor() -> MarkdownPostProcessor {
        MarkdownPostProcessor::new(Arc::new(HtmdMarkdownService::new()), None)
    }

    /// formats 含 "markdown" 时应生成 Markdown(返回 Ok(Some))
    #[tokio::test]
    async fn generates_markdown_when_formats_contains_markdown() {
        let p = make_processor();
        let req = make_req(json!({
            "url": "https://example.com",
            "formats": ["markdown"]
        }));
        let result = p
            .generate(
                crate::domain::id::Id::generate(),
                &req,
                "<html><body><h1>Title</h1></body></html>",
            )
            .await;
        let md = result.expect("expected Ok, got Err");
        assert!(md.is_some(), "expected Some(markdown)");
        let md = md.expect("some markdown");
        assert!(
            md.contains("Title"),
            "expected Title in markdown, got: {md}"
        );
    }

    /// formats 不含 "markdown" 时返回 Ok(None)(非错误)
    #[tokio::test]
    async fn returns_ok_none_when_formats_does_not_contain_markdown() {
        let p = make_processor();
        let req = make_req(json!({
            "url": "https://example.com",
            "formats": ["html"]
        }));
        let result = p
            .generate(
                crate::domain::id::Id::generate(),
                &req,
                "<html><body>Hi</body></html>",
            )
            .await;
        assert!(result.is_ok(), "expected Ok, got Err: {:?}", result.err());
        assert!(
            result.unwrap().is_none(),
            "expected Ok(None) when markdown not requested"
        );
    }

    /// formats 为 None 时返回 Ok(None)(非错误)
    #[tokio::test]
    async fn returns_ok_none_when_formats_is_none() {
        let p = make_processor();
        let req = make_req(json!({"url": "https://example.com"}));
        let result = p
            .generate(
                crate::domain::id::Id::generate(),
                &req,
                "<html><body>Hi</body></html>",
            )
            .await;
        assert!(result.is_ok(), "expected Ok, got Err: {:?}", result.err());
        assert!(
            result.unwrap().is_none(),
            "expected Ok(None) when formats absent"
        );
    }

    /// 空 HTML 在请求 markdown 时应返回 Err(EmptyResult)(错误显性化)
    ///
    /// 原实现:返回 `None`(与"未请求 markdown"混淆)
    /// 现实现:返回 `Err(EmptyResult)`(让调用方区分语义)
    #[tokio::test]
    async fn returns_err_empty_result_for_empty_html_when_markdown_requested() {
        let p = make_processor();
        let req = make_req(json!({
            "url": "https://example.com",
            "formats": ["markdown"]
        }));
        let result = p
            .generate(crate::domain::id::Id::generate(), &req, "")
            .await;
        match result {
            Err(MarkdownPostProcessorError::EmptyResult) => {}
            other => panic!("expected Err(EmptyResult), got {other:?}"),
        }
    }

    /// 纯空白内容(仅空格/换行)在请求 markdown 时也应返回 Err(EmptyResult)
    #[tokio::test]
    async fn returns_err_empty_result_for_whitespace_only_html() {
        let p = make_processor();
        let req = make_req(json!({
            "url": "https://example.com",
            "formats": ["markdown"]
        }));
        let result = p
            .generate(crate::domain::id::Id::generate(), &req, "   \n\t  \n  ")
            .await;
        match result {
            Err(MarkdownPostProcessorError::EmptyResult) => {}
            other => panic!("expected Err(EmptyResult), got {other:?}"),
        }
    }

    /// 确认 Ok(None) 与 Err(EmptyResult) 在调用方正确处理(模拟 scrape_worker 行为)
    ///
    /// 调用方策略:markdown 为增强字段,失败不阻断主流程
    #[tokio::test]
    async fn caller_strategy_continues_on_markdown_error() {
        let p = make_processor();

        // 未请求 markdown → Ok(None) → 继续无 markdown
        let req = make_req(json!({"url": "https://example.com"}));
        let result = p
            .generate(crate::domain::id::Id::generate(), &req, "<html></html>")
            .await;
        let generated: Option<String> = match result {
            Ok(md) => md,
            Err(e) => {
                // 模拟 scrape_worker 调用方:记录告警并继续
                warn!("markdown post-processing failed: {}", e);
                None
            }
        };
        assert!(
            generated.is_none(),
            "expected no markdown when not requested"
        );

        // 请求 markdown 但空 HTML → Err(EmptyResult) → 继续无 markdown
        let req = make_req(json!({
            "url": "https://example.com",
            "formats": ["markdown"]
        }));
        let result = p
            .generate(crate::domain::id::Id::generate(), &req, "")
            .await;
        let generated: Option<String> = match result {
            Ok(md) => md,
            Err(e) => {
                warn!("markdown post-processing failed: {}", e);
                None
            }
        };
        assert!(
            generated.is_none(),
            "expected no markdown on empty html error"
        );
    }

    /// P16 回归钉:only_main_content=true 且抽取器产出正文 HTML 时,
    /// Markdown 必须保留结构(表格/链接),而非退化为纯文本。
    ///
    /// 修复前:直接把 `ExtractedContent.text`(纯文本)喂给 Markdown 转换器,
    /// 表格/链接/代码块结构全部丢失。
    #[cfg(feature = "trafilatura")]
    #[tokio::test]
    async fn only_main_content_preserves_markdown_structure_from_extracted_html() {
        use crate::domain::services::content_extractor::ContentExtractionFacade;

        let p = MarkdownPostProcessor::new(
            Arc::new(HtmdMarkdownService::new()),
            Some(Arc::new(ContentExtractionFacade::new(None))),
        );
        let req = make_req(json!({
            "url": "https://example.com/article",
            "formats": ["markdown"],
            "options": { "only_main_content": true }
        }));
        let html = "<html><head><title>Engine Benchmarks Report</title></head><body>\
                    <nav>home | about | contact</nav>\
                    <article>\
                    <h1>Engine Benchmarks Report</h1>\
                    <p>This report covers the latest round of performance testing across \
                    all supported crawling engines. The methodology focuses on sustained \
                    request throughput under realistic network latency conditions.\
                    Read the <a href=\"https://example.com/docs\">full documentation</a> for setup details.</p>\
                    <h2>Methodology</h2>\
                    <p>Each engine ran for ten minutes against a controlled test cluster. \
                    We measured requests per second, error rates, and memory overhead. \
                    The test harness pinned CPU affinity to eliminate scheduler noise.</p>\
                    <h2>Results</h2>\
                    <ul><li>wreq reached 9000 rps</li><li>reqwest reached 6500 rps</li></ul>\
                    <p>For deeper analysis see the <a href=\"https://example.com/analysis\">analysis</a> page.</p>\
                    </article>\
                    </body></html>";
        let result = p
            .generate(crate::domain::id::Id::generate(), &req, html)
            .await;
        let md = result
            .expect("expected Ok")
            .expect("expected Some(markdown)");
        assert!(
            md.contains("documentation") && md.contains("]("),
            "links must survive as Markdown links, got: {md}"
        );
        assert!(
            md.contains('#'),
            "heading structure must survive as Markdown heading, got: {md}"
        );
        // 注:rs-trafilatura 0.2.2 的 content_html 会把 <table> 扁平化为文本,
        // 表格结构无法经此链路保留(外部依赖能力边界,见 design.md Consequences)
    }

    /// P16 降级路径:only_main_content=true 但无抽取器注入时,
    /// 维持整页 HTML 转换行为(与修复前一致)
    #[tokio::test]
    async fn only_main_content_without_extractor_falls_back_to_full_html() {
        let p = make_processor();
        let req = make_req(json!({
            "url": "https://example.com/article",
            "formats": ["markdown"],
            "options": { "only_main_content": true }
        }));
        let result = p
            .generate(
                crate::domain::id::Id::generate(),
                &req,
                "<html><body><h1>Title</h1><p>Body text.</p></body></html>",
            )
            .await;
        let md = result
            .expect("expected Ok")
            .expect("expected Some(markdown)");
        assert!(
            md.contains("Title"),
            "full HTML conversion should still work, got: {md}"
        );
    }
}