working import task

wip: not complete, got to fix some of markdown options
This commit is contained in:
Wyatt J. Miller 2025-05-17 21:04:56 -04:00
parent c8273016ee
commit 1503db9509
2 changed files with 76 additions and 17 deletions

View File

@ -63,18 +63,20 @@ async fn import_posts(
println!("Importing new file: {}", file_name_str); println!("Importing new file: {}", file_name_str);
// Process file contents // 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); // println!("{:?}", file_md_contents);
let content = markdown::to_html(&file_md_contents);
// println!("{:?}", content);
// Extract metadata // Extract metadata
let metadata = crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>( let document = crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>(
&file_md_contents, &file_md_contents,
)?; )?;
// println!("{:?}", metadata);
let title = metadata.metadata.title; let content =
let content_final = metadata.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); // println!("{:?}", title);
// Insert into database // Insert into database
@ -101,10 +103,7 @@ async fn import_posts(
Ok(()) Ok(())
} }
fn process_read_file( fn process_read_file(file_path: &std::path::Path) -> Result<String, std::io::Error> {
file_path: &std::path::Path,
options: &MarkdownOptions,
) -> Result<String, std::io::Error> {
let mut file = std::fs::read_to_string(file_path)?; let mut file = std::fs::read_to_string(file_path)?;
Ok(file) Ok(file)

View File

@ -2,19 +2,36 @@
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
#[derive(Debug)] #[derive(Debug)]
pub struct Document<T: DeserializeOwned> { pub struct Document {
pub metadata: T, pub metadata: FrontMatter,
pub content: String, pub content: String,
} }
#[derive(Debug)]
pub struct FrontMatter {
pub layout: String,
pub title: String,
pub date: String,
pub published: bool,
}
pub struct YamlFrontMatter; pub struct YamlFrontMatter;
impl YamlFrontMatter { impl YamlFrontMatter {
pub fn parse<T: DeserializeOwned>( pub fn parse<T: DeserializeOwned>(
markdown: &str, markdown: &str,
) -> Result<Document<T>, Box<dyn std::error::Error>> { ) -> Result<Document, Box<dyn std::error::Error>> {
let yaml = YamlFrontMatter::extract(markdown)?; let yaml = YamlFrontMatter::extract(markdown)?;
println!("{:?}", yaml); println!("File front matter metadata (raw): {:?}", yaml.0);
let metadata = serde_yaml::from_str::<T>(yaml.0.as_str())?; // 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 { Ok(Document {
metadata, metadata,
@ -54,4 +71,47 @@ impl YamlFrontMatter {
.join("\n"), .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,
})
}
} }