From b1ed042dadef08647ed4e55961394e9bd6772a3f Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Mon, 29 Apr 2024 14:58:03 -0300 Subject: [PATCH] fix(npf): remove unecessary parent check --- src/convert/npf.rs | 74 +++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/src/convert/npf.rs b/src/convert/npf.rs index b0bc34f..41a91db 100644 --- a/src/convert/npf.rs +++ b/src/convert/npf.rs @@ -164,51 +164,43 @@ impl<'a> TryFrom<&'a Node<'a, RefCell>> for objects::Post { Ok(post) } NodeValue::Image(i) => { - let alt_text = Self::try_from(node.children())?.fold_content(); - if let Some(p) = node.parent() { - if let NodeValue::Paragraph = p.data.borrow().value { - let alt_text = alt_text - .content - .iter() - .find(|b| { - if let BlockValue::Text(_) = b { - true - } else { - false - } - }) - .unwrap_or(BlockValue::Text(BlockText::new("")).borrow()) - .to_owned(); - let alt_text = if let BlockValue::Text(t) = alt_text { - Some(t.text.clone()) + let alt_text = Self::try_from(node.children())? + .fold_content() + .content + .iter() + .find(|b| { + if let BlockValue::Text(_) = b { + true } else { - None - }; + false + } + }) + .unwrap_or(BlockValue::Text(BlockText::new("")).borrow()) + .to_owned(); + let alt_text = if let BlockValue::Text(t) = alt_text { + Some(t.text.clone()) + } else { + None + }; - let media = if let Ok(url) = url::Url::from_str(&i.url) { - Media::from(url) - } else if let Some(name) = path::Path::new(&i.url).file_name() { - if let Some(name) = name.to_str() { - Media::from(name) - } else { - Media::from(i.url.as_str()) - } - } else { - Media::from(i.url.as_str()) - }; - - let mut block = BlockImage::from(media); - block.alt_text = alt_text; - - let mut post = Self::new(0); - post.content.push(BlockValue::Image(block)); - Ok(post) + let media = if let Ok(url) = url::Url::from_str(&i.url) { + Media::from(url) + } else if let Some(name) = path::Path::new(&i.url).file_name() { + if let Some(name) = name.to_str() { + Media::from(name) } else { - Ok(alt_text) + Media::from(i.url.as_str()) } } else { - Ok(alt_text) - } + Media::from(i.url.as_str()) + }; + + let mut block = BlockImage::from(media); + block.alt_text = alt_text; + + let mut post = Self::new(0); + post.content.push(BlockValue::Image(block)); + Ok(post) } _ => Ok(Self::new(0)), } @@ -457,7 +449,7 @@ mod tests { let markdown = "If **you** are reading this, thanks for giving a look\n\ and checking the ~~ugly~~ source code of this *little\n\ **personal** project*. It is heart warming to know that *at least*\n\ - [someone](t:_YENQUPzd_oPpmVDqZQ-yw) found this interesting and maybe useful, ![even knowing](image.png)\n\ + [someone](t:_YENQUPzd_oPpmVDqZQ-yw) found this interesting and maybe useful, even knowing\n\ how niched this whole project is.\\ - [Gustavo \"Guz\" L. de Mello](https://guz.one), Apr 16, 12.2024";