rs_web/markdown/
pipeline.rs

1use std::path::Path;
2
3use super::parser::{events_to_html, parse_markdown};
4use super::transforms::AstTransform;
5use crate::config::Config;
6
7/// Context passed to transforms (fields available for custom transforms)
8#[allow(dead_code)]
9pub struct TransformContext<'a> {
10    pub config: &'a Config,
11    pub current_path: &'a Path,
12    pub base_url: &'a str,
13}
14
15/// Markdown processing pipeline
16pub struct Pipeline {
17    transforms: Vec<Box<dyn AstTransform>>,
18}
19
20impl Pipeline {
21    pub fn new() -> Self {
22        Self {
23            transforms: Vec::new(),
24        }
25    }
26
27    /// Add a transform to the pipeline
28    pub fn with<T: AstTransform + 'static>(mut self, transform: T) -> Self {
29        self.transforms.push(Box::new(transform));
30        self
31    }
32
33    /// Build the pipeline (sorts by priority)
34    pub fn build(mut self) -> Self {
35        self.transforms.sort_by_key(|t| t.priority());
36        self
37    }
38
39    /// Process markdown content through the pipeline
40    pub fn process(&self, content: &str, ctx: &TransformContext<'_>) -> String {
41        let mut events = parse_markdown(content);
42
43        // Apply each transform
44        for transform in &self.transforms {
45            events = transform.transform(events, ctx);
46        }
47
48        // Convert to HTML
49        events_to_html(events.into_iter())
50    }
51}
52
53impl Pipeline {
54    /// Create pipeline with default transforms and configuration
55    pub fn from_config(config: &Config) -> Self {
56        use super::transforms::*;
57
58        Self::new()
59            .with(LazyImagesTransform)
60            .with(HeadingAnchorsTransform::new())
61            .with(NameHighlightTransform::new(config.highlight.clone()))
62            .with(ExternalLinksTransform)
63            .build()
64    }
65}
66
67impl Default for Pipeline {
68    fn default() -> Self {
69        use super::transforms::*;
70
71        Self::new()
72            .with(LazyImagesTransform)
73            .with(HeadingAnchorsTransform::new())
74            .with(ExternalLinksTransform)
75            .build()
76    }
77}