Skip to content

Commit

Permalink
fix the second part of the issue #12
Browse files Browse the repository at this point in the history
which I forgot to fix in the last commit:
starting a bracket at the end of a link:
`https://delta.chat/page(this is the link to our site)`
(when it is not closed in the link its not taken)
  • Loading branch information
Simon-Laux committed Mar 4, 2022
1 parent 593059a commit 8d3fa05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::parser::link_url::LinkDestination;

use super::base_parsers::*;
use super::Element;
use crate::nom::{Offset, Slice};
use nom::bytes::complete::take_while;
use nom::{
bytes::{
Expand Down Expand Up @@ -79,15 +80,35 @@ fn link_intern(input: &str) -> IResult<&str, (), CustomError<&str>> {
match char {
'(' => {
parentheses_count += 1;
// if there is no closing bracket in the link, then don't take the bracket as a part of the link
if (<&str>::clone(&consumed)).slice(i..).find(')').is_none() {
alternative_offset = Some(i);
break;
}
}
'{' => {
curly_brackets_count += 1;
// if there is no closing bracket in the link, then don't take the bracket as a part of the link
if (<&str>::clone(&consumed)).slice(i..).find('}').is_none() {
alternative_offset = Some(i);
break;
}
}
'[' => {
brackets_count += 1;
// if there is no closing bracket in the link, then don't take the bracket as a part of the link
if (<&str>::clone(&consumed)).slice(i..).find(']').is_none() {
alternative_offset = Some(i);
break;
}
}
'<' => {
angle_backets += 1;
// if there is no closing bracket in the link, then don't take the bracket as a part of the link
if (<&str>::clone(&consumed)).slice(i..).find('>').is_none() {
alternative_offset = Some(i);
break;
}
}
')' => {
if parentheses_count == 0 {
Expand Down Expand Up @@ -126,7 +147,6 @@ fn link_intern(input: &str) -> IResult<&str, (), CustomError<&str>> {
}

if let Some(offset) = alternative_offset {
use crate::nom::Slice;
let remaining = input.slice(offset..);
Ok((remaining, ()))
} else {
Expand All @@ -138,7 +158,6 @@ pub(crate) fn link(input: &str) -> IResult<&str, Element, CustomError<&str>> {
// basically
//let (input, content) = recognize(link_intern)(input)?;
// but don't eat the last char if it is one of these: `.,;:`
use crate::nom::{Offset, Slice};
let i = <&str>::clone(&input);
let i2 = <&str>::clone(&input);
let i3 = <&str>::clone(&input);
Expand Down
27 changes: 27 additions & 0 deletions tests/text_to_ast/text_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,30 @@ fn link_with_parenthesis_in_parenthesis_curly() {
]
);
}

#[test]
fn link_with_descriptive_parenthesis() {
assert_eq!(
parse_only_text("https://delta.chat/page(this is the link to our site)"),
vec![
Link {
destination: link_destination_for_testing("https://delta.chat/page")
},
Text("(this is the link to our site)")
]
);
}

#[test]
fn link_in_parenthesis2() {
assert_eq!(
parse_only_text("A great chat app (see https://delta.chat/en/)"),
vec![
Text("A great chat app (see "),
Link {
destination: link_destination_for_testing("https://delta.chat/en/")
},
Text(")")
]
);
}

0 comments on commit 8d3fa05

Please sign in to comment.