Skip to main content

verso/export/
writer.rs

1use anyhow::Result;
2use std::path::Path;
3
4pub fn write_export(dir: &Path, slug: &str, contents: &str) -> Result<std::path::PathBuf> {
5    std::fs::create_dir_all(dir)?;
6    let path = dir.join(format!("{slug}.md"));
7    std::fs::write(&path, contents)?;
8    Ok(path)
9}
10
11pub fn slug_from_title(title: &str) -> String {
12    title
13        .chars()
14        .filter_map(|c| {
15            if c.is_alphanumeric() {
16                Some(c.to_ascii_lowercase())
17            } else if c.is_whitespace() || c == '-' || c == '_' {
18                Some('-')
19            } else {
20                None
21            }
22        })
23        .collect::<String>()
24        .split('-')
25        .filter(|s| !s.is_empty())
26        .collect::<Vec<_>>()
27        .join("-")
28}