Skip to main content

synaptic_loaders/
markdown_loader.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use crate::Document;
5use async_trait::async_trait;
6use serde_json::Value;
7use synaptic_core::SynapticError;
8
9use crate::Loader;
10
11/// Loads a markdown file, preserving original content.
12///
13/// Reads the markdown file and returns it as a single Document.
14/// Metadata includes `source` (the file path) and `format: "markdown"`.
15pub struct MarkdownLoader {
16    path: PathBuf,
17}
18
19impl MarkdownLoader {
20    pub fn new(path: impl Into<PathBuf>) -> Self {
21        Self { path: path.into() }
22    }
23}
24
25#[async_trait]
26impl Loader for MarkdownLoader {
27    async fn load(&self) -> Result<Vec<Document>, SynapticError> {
28        let content = tokio::fs::read_to_string(&self.path).await.map_err(|e| {
29            SynapticError::Loader(format!("cannot read {}: {e}", self.path.display()))
30        })?;
31
32        let id = self.path.to_string_lossy().to_string();
33
34        let mut metadata = HashMap::new();
35        metadata.insert(
36            "source".to_string(),
37            Value::String(self.path.to_string_lossy().to_string()),
38        );
39        metadata.insert("format".to_string(), Value::String("markdown".to_string()));
40
41        Ok(vec![Document::with_metadata(id, content, metadata)])
42    }
43}