feat(npf): link conversion and error handling

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-04-25 16:35:48 -03:00
parent 4bdf0fcdb4
commit 498a9ed57f
2 changed files with 26 additions and 3 deletions

View File

@@ -20,12 +20,12 @@ mod objects_post;
use content_blocks::{BlockText, BlockValue};
use text_formatting::{FormatTypeBold, FormatTypeItalic, FormatValue};
use self::content_blocks::BlockImage;
use text_formatting::{FormatTypeLink, FormatTypeStrikeThrough};
#[derive(Debug)]
pub enum NPFConvertError {
TODO,
InvalidURL { url: String, err: url::ParseError },
}
fn extract_text(contents: &Vec<BlockValue>) -> String {
@@ -105,13 +105,33 @@ impl<'a> TryFrom<&'a Node<'a, RefCell<Ast>>> for objects::Post {
post.content.append(&mut content);
Ok(())
}
NodeValue::Link(link) => match url::Url::parse(&link.url) {
Ok(url) => {
let mut content = Self::try_from(n)?
.fold_content()
.for_each_content(|c| {
if let BlockValue::Text(ref mut t) = c {
let mut format = FormatTypeLink::from(&t.text);
format.url = url.clone();
t.push_formatting(FormatValue::Link(format));
t.text = String::from(t.text.trim());
}
})
.content;
post.content.append(&mut content);
Ok(())
}
Err(err) => Err(NPFConvertError::InvalidURL {
url: link.url.clone(),
err,
}),
},
_ => Ok(()),
})
.collect();
if let Err(e) = r {
Err(e)
} else {
println!("{:#?}", post);
Ok(post)
}
}

View File

@@ -73,7 +73,10 @@ fn main() {
let arena = comrak::Arena::new();
let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options());
let _ = convert::npf::from(ast);
match convert::npf::from(ast) {
Ok(test) => println!("{}", serde_json::to_string_pretty(&test).unwrap()),
Err(err) => println!("{err:#?}"),
};
// println!("{:#?}", ast);
let result = match cli.command {