From 63628d861b3063563467b99c25d650185862fc7c Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Tue, 7 May 2024 12:45:32 +0200 Subject: [PATCH] fix parenthesis in target of labeled link --- message_parser_wasm/src/lib.rs | 2 +- src/parser/parse_from_text/markdown_elements.rs | 15 ++++----------- tests/text_to_ast/markdown.rs | 11 +++++++++++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/message_parser_wasm/src/lib.rs b/message_parser_wasm/src/lib.rs index cb6d75f..e6882ee 100644 --- a/message_parser_wasm/src/lib.rs +++ b/message_parser_wasm/src/lib.rs @@ -23,7 +23,7 @@ pub fn parse_text(s: &str, enable_markdown: bool) -> JsValue { serde_wasm_bindgen::to_value(&ast).expect("Element converts to JsValue") } -/// parses text to json AST (text elements and labled links, to replicate current desktop implementation) +/// parses text to json AST (text elements and labeled links, to replicate current desktop implementation) #[wasm_bindgen] pub fn parse_desktop_set(s: &str) -> JsValue { serde_wasm_bindgen::to_value(&deltachat_message_parser::parser::parse_desktop_set(s)) diff --git a/src/parser/parse_from_text/markdown_elements.rs b/src/parser/parse_from_text/markdown_elements.rs index dbc5ec8..a239839 100644 --- a/src/parser/parse_from_text/markdown_elements.rs +++ b/src/parser/parse_from_text/markdown_elements.rs @@ -107,17 +107,10 @@ pub(crate) fn labeled_link(input: &str) -> IResult<&str, Element, CustomError<&s } let label = parse_all(raw_label); - let (input, raw_link): (&str, &str) = delimited(tag("("), is_not(")"), tag(")"))(input)?; - if raw_link.is_empty() { - return Err(nom::Err::Error(CustomError::NoContent)); - } - // check if result is valid link - let (remainder, destination) = LinkDestination::parse_labelled(raw_link)?; - if remainder.is_empty() { - Ok((input, Element::LabeledLink { label, destination })) - } else { - Err(nom::Err::Error(CustomError::InvalidLink)) - } + let (input, (_, destination, _)) = + tuple((tag("("), LinkDestination::parse_labelled, tag(")")))(input)?; + + Ok((input, Element::LabeledLink { label, destination })) } pub(crate) fn parse_element( diff --git a/tests/text_to_ast/markdown.rs b/tests/text_to_ast/markdown.rs index c0e8e84..fe19090 100644 --- a/tests/text_to_ast/markdown.rs +++ b/tests/text_to_ast/markdown.rs @@ -689,6 +689,17 @@ fn labeled_link() { ); } +#[test] +fn labeled_link_parenthesis_in_target() { + assert_eq!( + parse_markdown_text("[a link](https://delta.chat/en/help(help)hi)"), + vec![LabeledLink { + label: vec![Text("a link")], + destination: https_link_no_puny("https://delta.chat/en/help(help)hi", "delta.chat"), + }] + ); +} + #[test] fn labeled_link_example() { assert_eq!(