Skip to content

Commit

Permalink
Merge pull request #87 from notriddle/notriddle/link-tweaks
Browse files Browse the repository at this point in the history
feat: angle brackets if link paren is unbalanced
  • Loading branch information
Byron authored Oct 16, 2024
2 parents d5323a8 + b3d7df2 commit 81ac29b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,12 +821,30 @@ fn close_link<F>(uri: &str, title: &str, f: &mut F, link_type: LinkType) -> fmt:
where
F: fmt::Write,
{
let needs_brackets = {
let mut depth = 0;
for b in uri.bytes() {
match b {
b'(' => depth += 1,
b')' => depth -= 1,
b' ' => {
depth += 1;
break;
}
_ => {}
}
if depth > 3 {
break;
}
}
depth != 0
};
let separator = match link_type {
LinkType::Shortcut => ": ",
_ => "(",
};

if uri.contains(' ') {
if needs_brackets {
write!(f, "]{separator}<{uri}>")?;
} else {
write!(f, "]{separator}{uri}")?;
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/common-mark.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusam et [justo duo dolores][1] et ea rebum. Stet clita kasd gubergren, no
sea [takimata sanctus](./showcase.md) est Lorem ipsum dolor sit amet.

- [bacon](<(>)
- [bacon](<)>)
- [bacon](test())

Ask to <user@example.com>.

[1]: http://www.example.com/reference
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/snapshots/stupicat-event-by-event-output
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusam et [justo duo dolores][1] et ea rebum. Stet clita kasd gubergren, no
sea [takimata sanctus](./showcase.md) est Lorem ipsum dolor sit amet.

* [bacon](<(>)
* [bacon](<)>)
* [bacon](test())

Ask to <user@example.com>.

## [Images]
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/snapshots/stupicat-output
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusam et [justo duo dolores][1] et ea rebum. Stet clita kasd gubergren, no
sea [takimata sanctus](./showcase.md) est Lorem ipsum dolor sit amet.

* [bacon](<(>)
* [bacon](<)>)
* [bacon](test())

Ask to <user@example.com>.

## [Images]
Expand Down
5 changes: 3 additions & 2 deletions tests/spec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use pulldown_cmark::utils::TextMergeStream;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
use pulldown_cmark_to_cmark::cmark;

Expand Down Expand Up @@ -37,10 +38,10 @@ fn collect_test_case<'a>(events: &mut impl Iterator<Item = Event<'a>>) -> Option

fn test_roundtrip(original: &str, expected: &str) -> bool {
let opts = Options::empty();
let event_list = Parser::new_ext(original, opts).collect::<Vec<_>>();
let event_list = TextMergeStream::new(Parser::new_ext(original, opts)).collect::<Vec<_>>();
let mut regen_str = String::new();
cmark(event_list.iter().cloned(), &mut regen_str).expect("Regeneration failure");
let event_list_2 = Parser::new_ext(&regen_str, opts).collect::<Vec<_>>();
let event_list_2 = TextMergeStream::new(Parser::new_ext(&regen_str, opts)).collect::<Vec<_>>();
let event_count = event_list.len();
let event_count_2 = event_list_2.len();
let same_event_count = event_list
Expand Down

0 comments on commit 81ac29b

Please sign in to comment.