Skip to main content

systemprompt_sync/export/
content.rs

1use super::escape_yaml;
2use anyhow::Result;
3use std::fs;
4use std::path::Path;
5use systemprompt_content::models::Content;
6
7pub fn generate_content_markdown(content: &Content) -> String {
8    let image_str = content.image.as_deref().unwrap_or("");
9
10    format!(
11        r#"---
12title: "{}"
13description: "{}"
14author: "{}"
15slug: "{}"
16keywords: "{}"
17image: "{}"
18kind: "{}"
19public: {}
20tags: []
21published_at: "{}"
22updated_at: "{}"
23---
24
25{}"#,
26        escape_yaml(&content.title),
27        escape_yaml(&content.description),
28        escape_yaml(&content.author),
29        &content.slug,
30        escape_yaml(&content.keywords),
31        image_str,
32        &content.kind,
33        content.public,
34        content.published_at.format("%Y-%m-%d"),
35        content.updated_at.format("%Y-%m-%d"),
36        &content.body
37    )
38}
39
40pub fn export_content_to_file(
41    content: &Content,
42    base_path: &Path,
43    source_type: &str,
44) -> Result<()> {
45    let markdown = generate_content_markdown(content);
46
47    let content_dir = if source_type == "blog" {
48        let dir = base_path.join(&content.slug);
49        fs::create_dir_all(&dir)?;
50        dir.join("index.md")
51    } else {
52        fs::create_dir_all(base_path)?;
53        base_path.join(format!("{}.md", content.slug))
54    };
55
56    fs::write(&content_dir, markdown)?;
57    Ok(())
58}