Skip to content

Commit

Permalink
feat: show link label and title
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Aug 12, 2024
1 parent a31f6e4 commit f1c30ee
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
74 changes: 71 additions & 3 deletions src/markdown/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,23 @@ impl<'a> InlinesParser<'a> {
NodeValue::Emph => self.process_children(node, style.italics())?,
NodeValue::Strikethrough => self.process_children(node, style.strikethrough())?,
NodeValue::SoftBreak => self.pending_text.push(Text::from(" ")),
NodeValue::Link(link) => self.pending_text.push(Text::new(link.url.clone(), TextStyle::default().link())),
NodeValue::Link(link) => {
let has_label = node.first_child().is_some();
let has_title = !link.title.is_empty();
if has_label {
self.process_children(node, TextStyle::default().link_label())?;
self.pending_text.push(Text::from(" ("));
}
self.pending_text.push(Text::new(link.url.clone(), TextStyle::default().link_url()));
if has_title {
self.pending_text.push(Text::from(" \""));
self.pending_text.push(Text::new(link.title.clone(), TextStyle::default().link_title()));
self.pending_text.push(Text::from("\""));
}
if has_label {
self.pending_text.push(Text::from(")"));
}
}
NodeValue::LineBreak => {
self.store_pending_text();
self.inlines.push(Inline::LineBreak);
Expand Down Expand Up @@ -565,10 +581,62 @@ boop
}

#[test]
fn link() {
fn link_wo_label_wo_title() {
let parsed = parse_single("my [](https://example.com)");
let MarkdownElement::Paragraph(elements) = parsed else { panic!("not a paragraph: {parsed:?}") };
let expected_chunks =
vec![Text::from("my "), Text::new("https://example.com", TextStyle::default().link_url())];

let expected_elements = &[ParagraphElement::Text(TextBlock(expected_chunks))];
assert_eq!(elements, expected_elements);
}

#[test]
fn link_w_label_wo_title() {
let parsed = parse_single("my [website](https://example.com)");
let MarkdownElement::Paragraph(elements) = parsed else { panic!("not a paragraph: {parsed:?}") };
let expected_chunks = vec![Text::from("my "), Text::new("https://example.com", TextStyle::default().link())];
let expected_chunks = vec![
Text::from("my "),
Text::new("website", TextStyle::default().link_label()),
Text::from(" ("),
Text::new("https://example.com", TextStyle::default().link_url()),
Text::from(")"),
];

let expected_elements = &[ParagraphElement::Text(TextBlock(expected_chunks))];
assert_eq!(elements, expected_elements);
}

#[test]
fn link_wo_label_w_title() {
let parsed = parse_single("my [](https://example.com \"Example\")");
let MarkdownElement::Paragraph(elements) = parsed else { panic!("not a paragraph: {parsed:?}") };
let expected_chunks = vec![
Text::from("my "),
Text::new("https://example.com", TextStyle::default().link_url()),
Text::from(" \""),
Text::new("Example", TextStyle::default().link_title()),
Text::from("\""),
];

let expected_elements = &[ParagraphElement::Text(TextBlock(expected_chunks))];
assert_eq!(elements, expected_elements);
}

#[test]
fn link_w_label_w_title() {
let parsed = parse_single("my [website](https://example.com \"Example\")");
let MarkdownElement::Paragraph(elements) = parsed else { panic!("not a paragraph: {parsed:?}") };
let expected_chunks = vec![
Text::from("my "),
Text::new("website", TextStyle::default().link_label()),
Text::from(" ("),
Text::new("https://example.com", TextStyle::default().link_url()),
Text::from(" \""),
Text::new("Example", TextStyle::default().link_title()),
Text::from("\""),
Text::from(")"),
];

let expected_elements = &[ParagraphElement::Text(TextBlock(expected_chunks))];
assert_eq!(elements, expected_elements);
Expand Down
14 changes: 12 additions & 2 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ impl TextStyle {
self.add_flag(TextFormatFlags::Underlined)
}

/// Indicate this is a link.
pub(crate) fn link(self) -> Self {
/// Indicate this is a link label.
pub(crate) fn link_label(self) -> Self {
self.bold()
}

/// Indicate this is a link title.
pub(crate) fn link_title(self) -> Self {
self.italics()
}

/// Indicate this is a link url.
pub(crate) fn link_url(self) -> Self {
self.italics().underlined()
}

Expand Down

0 comments on commit f1c30ee

Please sign in to comment.