From 1503db95094f3171dafaae79cb1a965cb48b6d65 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 17 May 2025 21:04:56 -0400 Subject: [PATCH] working import task wip: not complete, got to fix some of markdown options --- backend/task/src/tasks/import_posts.rs | 23 ++++----- backend/task/src/utils/front_matter.rs | 70 ++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/backend/task/src/tasks/import_posts.rs b/backend/task/src/tasks/import_posts.rs index 971bd57..dbbfcfb 100644 --- a/backend/task/src/tasks/import_posts.rs +++ b/backend/task/src/tasks/import_posts.rs @@ -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::( + let document = crate::utils::front_matter::YamlFrontMatter::parse::( &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 { +fn process_read_file(file_path: &std::path::Path) -> Result { let mut file = std::fs::read_to_string(file_path)?; Ok(file) diff --git a/backend/task/src/utils/front_matter.rs b/backend/task/src/utils/front_matter.rs index 2f8bf66..42491a7 100644 --- a/backend/task/src/utils/front_matter.rs +++ b/backend/task/src/utils/front_matter.rs @@ -2,19 +2,36 @@ use serde::de::DeserializeOwned; #[derive(Debug)] -pub struct Document { - 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( markdown: &str, - ) -> Result, Box> { + ) -> Result> { let yaml = YamlFrontMatter::extract(markdown)?; - println!("{:?}", yaml); - let metadata = serde_yaml::from_str::(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 { + 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, + }) + } }