working import task
wip: not complete, got to fix some of markdown options
This commit is contained in:
		@@ -63,18 +63,20 @@ async fn import_posts(
 | 
			
		||||
            println!("Importing new file: {}", file_name_str);
 | 
			
		||||
 | 
			
		||||
            // Process file contents
 | 
			
		||||
            let file_md_contents = process_read_file(&file_path, &options)?;
 | 
			
		||||
            let file_md_contents = process_read_file(&file_path)?;
 | 
			
		||||
            // println!("{:?}", file_md_contents);
 | 
			
		||||
            let content = markdown::to_html(&file_md_contents);
 | 
			
		||||
            // println!("{:?}", content);
 | 
			
		||||
 | 
			
		||||
            // Extract metadata
 | 
			
		||||
            let metadata = crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>(
 | 
			
		||||
            let document = crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>(
 | 
			
		||||
                &file_md_contents,
 | 
			
		||||
            )?;
 | 
			
		||||
            // println!("{:?}", metadata);
 | 
			
		||||
            let title = metadata.metadata.title;
 | 
			
		||||
            let content_final = metadata.content;
 | 
			
		||||
 | 
			
		||||
            let content =
 | 
			
		||||
                markdown::to_html_with_options(&document.content, &markdown::Options::default());
 | 
			
		||||
            println!("{:?}", content);
 | 
			
		||||
 | 
			
		||||
            // println!("{:?}", document);
 | 
			
		||||
            let title = document.metadata.title;
 | 
			
		||||
            let content_final = content.unwrap();
 | 
			
		||||
            // println!("{:?}", title);
 | 
			
		||||
 | 
			
		||||
            // Insert into database
 | 
			
		||||
@@ -101,10 +103,7 @@ async fn import_posts(
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn process_read_file(
 | 
			
		||||
    file_path: &std::path::Path,
 | 
			
		||||
    options: &MarkdownOptions,
 | 
			
		||||
) -> Result<String, std::io::Error> {
 | 
			
		||||
fn process_read_file(file_path: &std::path::Path) -> Result<String, std::io::Error> {
 | 
			
		||||
    let mut file = std::fs::read_to_string(file_path)?;
 | 
			
		||||
 | 
			
		||||
    Ok(file)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,19 +2,36 @@
 | 
			
		||||
use serde::de::DeserializeOwned;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Document<T: DeserializeOwned> {
 | 
			
		||||
    pub metadata: T,
 | 
			
		||||
pub struct Document {
 | 
			
		||||
    pub metadata: FrontMatter,
 | 
			
		||||
    pub content: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct FrontMatter {
 | 
			
		||||
    pub layout: String,
 | 
			
		||||
    pub title: String,
 | 
			
		||||
    pub date: String,
 | 
			
		||||
    pub published: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct YamlFrontMatter;
 | 
			
		||||
impl YamlFrontMatter {
 | 
			
		||||
    pub fn parse<T: DeserializeOwned>(
 | 
			
		||||
        markdown: &str,
 | 
			
		||||
    ) -> Result<Document<T>, Box<dyn std::error::Error>> {
 | 
			
		||||
    ) -> Result<Document, Box<dyn std::error::Error>> {
 | 
			
		||||
        let yaml = YamlFrontMatter::extract(markdown)?;
 | 
			
		||||
        println!("{:?}", yaml);
 | 
			
		||||
        let metadata = serde_yaml::from_str::<T>(yaml.0.as_str())?;
 | 
			
		||||
        println!("File front matter metadata (raw): {:?}", yaml.0);
 | 
			
		||||
        // println!("File content: {:?}", yaml.1);
 | 
			
		||||
        let clean_yaml = YamlFrontMatter::unescape_str(&yaml.0);
 | 
			
		||||
        println!("File front matter metadata (clean): {:?}", clean_yaml);
 | 
			
		||||
        let metadata = match YamlFrontMatter::from_yaml_str(clean_yaml.as_str()) {
 | 
			
		||||
            Ok(m) => m,
 | 
			
		||||
            Err(e) => {
 | 
			
		||||
                println!("{e}");
 | 
			
		||||
                panic!();
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Ok(Document {
 | 
			
		||||
            metadata,
 | 
			
		||||
@@ -54,4 +71,47 @@ impl YamlFrontMatter {
 | 
			
		||||
                .join("\n"),
 | 
			
		||||
        ))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn unescape_str(s: &str) -> String {
 | 
			
		||||
        s.replace("\\n", "\n")
 | 
			
		||||
            .replace("\\\"", "\"")
 | 
			
		||||
            .replace("\\\\", "\\")
 | 
			
		||||
        // .replace("\\t", "\t")
 | 
			
		||||
        // .replace("\\r", "\r")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn from_yaml_str(yaml: &str) -> Result<FrontMatter, String> {
 | 
			
		||||
        let mut layout = String::new();
 | 
			
		||||
        let mut title = String::new();
 | 
			
		||||
        let mut date = String::new();
 | 
			
		||||
        let mut published = false;
 | 
			
		||||
 | 
			
		||||
        for line in yaml.lines() {
 | 
			
		||||
            let line = line.trim();
 | 
			
		||||
            if let Some((key, value)) = line.split_once(':') {
 | 
			
		||||
                let key = key.trim();
 | 
			
		||||
                let value = value.trim();
 | 
			
		||||
 | 
			
		||||
                match key {
 | 
			
		||||
                    "layout" => layout = value.to_string(),
 | 
			
		||||
                    "title" => {
 | 
			
		||||
                        // Remove quotes if present
 | 
			
		||||
                        title = value.trim_matches('\'').trim_matches('"').to_string();
 | 
			
		||||
                    }
 | 
			
		||||
                    "date" => date = value.to_string(),
 | 
			
		||||
                    "published" => {
 | 
			
		||||
                        published = value.parse().map_err(|_| "Invalid boolean for published")?;
 | 
			
		||||
                    }
 | 
			
		||||
                    _ => {} // Ignore unknown fields
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(FrontMatter {
 | 
			
		||||
            layout,
 | 
			
		||||
            title,
 | 
			
		||||
            date,
 | 
			
		||||
            published,
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user