From 5b4ab0302ca7d390f014826fff9d848721bb043b Mon Sep 17 00:00:00 2001 From: Bram Dingelstad Date: Mon, 6 Nov 2023 20:54:29 +0100 Subject: [PATCH] feat: updated to newest notion-client version --- src/lib.rs | 236 ++++++++++++++++++++++++++++------------------------- 1 file changed, 125 insertions(+), 111 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4751de9..00b92d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,109 +1,114 @@ - use async_recursion::async_recursion; #[derive(Debug)] -pub enum Error { -} +pub enum Error {} use notion; use notion::BlockType; pub fn convert_rich_text(text: ¬ion::RichText) -> String { match text { - notion::RichText::Text(text, _) => { + notion::RichText::Text { + text, annotations, .. + } => { let mut string = text.content.to_owned(); - - if text.annotations.bold { + + if annotations.bold { string = format!("**{string}**"); } - if text.annotations.italic { + if annotations.italic { string = format!("*{string}*"); } - if text.annotations.code { + if annotations.code { string = format!("`{string}`"); } string - }, - _ => "".to_string() + } + _ => "".to_string(), } } #[async_recursion] -pub async fn convert_blocks(notion: ¬ion::Client, blocks: &Vec) -> Result { +pub async fn convert_blocks( + notion: ¬ion::Client, + blocks: &Vec, +) -> Result { let mut output = vec![]; for block in blocks.iter() { - let string = match &block.value { - BlockType::Heading1(heading) | - BlockType::Heading2(heading) | - BlockType::Heading3(heading) => { - let content = heading.rich_text + let string = match &block.block { + BlockType::Heading1 { heading } + | BlockType::Heading2 { heading } + | BlockType::Heading3 { heading } => { + let content = heading + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); - let markdown_heading = match &block.value { - BlockType::Heading1(_) => "#", - BlockType::Heading2(_) => "##", - BlockType::Heading3(_) | _ => "###", + let markdown_heading = match &block.block { + BlockType::Heading1 { .. } => "#", + BlockType::Heading2 { .. } => "##", + BlockType::Heading3 { .. } | _ => "###", }; Some(format!("{markdown_heading} {content}")) - }, - BlockType::Paragraph(paragraph) => { - Some( - paragraph.rich_text - .iter() - .map(|text| convert_rich_text(text)) - .collect::() - ) - }, - BlockType::Code(code) => { + } + BlockType::Paragraph { paragraph, .. } => Some( + paragraph + .rich_text + .iter() + .map(|text| convert_rich_text(text)) + .collect::(), + ), + BlockType::Code { code, .. } => { let language = serde_variant::to_variant_name(&code.language).unwrap(); - let content = code.rich_text + let content = code + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); - Some( - format!("```{language}\n{content}\n```") - ) - }, - BlockType::BulletedListItem(list_item) => { - let content = list_item.rich_text + Some(format!("```{language}\n{content}\n```")) + } + BlockType::BulletedListItem { + bulleted_list_item, .. + } => { + let content = bulleted_list_item + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); // TODO: Recurse down to `children` - Some( - format!("* {content}") - ) - }, - BlockType::NumberedListItem(list_item) => { + Some(format!("* {content}")) + } + BlockType::NumberedListItem { + numbered_list_item, .. + } => { // TODO: Hold state for numbering - let content = list_item.rich_text + let content = numbered_list_item + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); // TODO: Recurse down to `children` - Some( - format!("1. {content}") - ) - }, - BlockType::ToDo(todo_item) => { - let content = todo_item.rich_text + Some(format!("1. {content}")) + } + BlockType::ToDo { to_do, .. } => { + let content = to_do + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); - let checked = if todo_item.checked.unwrap_or(false) { + let checked = if to_do.checked.unwrap_or(false) { "x" } else { " " @@ -111,32 +116,30 @@ pub async fn convert_blocks(notion: ¬ion::Client, blocks: &Vec // TODO: Recurse down to `children` - Some( - format!("[{checked}] {content}") - ) - }, - BlockType::Quote(quote) => { - let content = quote.rich_text + Some(format!("[{checked}] {content}")) + } + BlockType::Quote { quote, .. } => { + let content = quote + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); // TODO: Recurse down to `children` - Some( - format!("> {content}") - ) - }, - BlockType::Callout(callout) => { - let content = callout.rich_text + Some(format!("> {content}")) + } + BlockType::Callout { callout, .. } => { + let content = callout + .rich_text .iter() .map(|text| convert_rich_text(text)) .collect::(); let icon = if let Some(value) = &callout.icon { match value { - notion::Icon::Emoji(emoji) => emoji, - _ => "" + notion::Icon::Emoji { emoji, .. } => emoji, + _ => "", } } else { "" @@ -144,42 +147,53 @@ pub async fn convert_blocks(notion: ¬ion::Client, blocks: &Vec // TODO: Recurse down to `children` - Some( - format!("> {icon} {content}") - ) - }, - BlockType::Image(image) => { - match &image.image { - notion::File::External(url) => Some(format!(r#""#)), + Some(format!("> {icon} {content}")) + } + BlockType::Image { image, .. } => { + match &image { + notion::File::External { external, .. } => { + let url = &external.url; + Some(format!(r#""#)) + } // TODO: Implement reupload of Notion file type - _ => None + _ => None, } - }, - BlockType::Video(video) => { - match &video.video { - notion::File::External(url) => Some(format!(r#"