Skip to content

Commit e8ded8d

Browse files
committed
feat: Add support for links by replacing inner value
1 parent 242ffd5 commit e8ded8d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

tui-markdown/src/lib.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ struct TextWriter<'a, I> {
9595
/// Current list index as a stack of indices.
9696
list_indices: Vec<Option<u64>>,
9797

98+
/// Whether a link should be appended
99+
will_append_link: Option<Span<'a>>,
100+
98101
needs_newline: bool,
99102
}
100103

@@ -118,6 +121,7 @@ where
118121
needs_newline: false,
119122
#[cfg(feature = "highlight-code")]
120123
code_highlighter: None,
124+
will_append_link: None,
121125
}
122126
}
123127

@@ -164,7 +168,7 @@ where
164168
Tag::Emphasis => self.push_inline_style(Style::new().italic()),
165169
Tag::Strong => self.push_inline_style(Style::new().bold()),
166170
Tag::Strikethrough => self.push_inline_style(Style::new().crossed_out()),
167-
Tag::Link { .. } => warn!("Link not yet supported"),
171+
Tag::Link { dest_url, .. } => self.will_append_link(dest_url),
168172
Tag::Image { .. } => warn!("Image not yet supported"),
169173
Tag::MetadataBlock(_) => warn!("Metadata block not yet supported"),
170174
Tag::DefinitionList => warn!("Definition list not yet supported"),
@@ -190,7 +194,12 @@ where
190194
TagEnd::Emphasis => self.pop_inline_style(),
191195
TagEnd::Strong => self.pop_inline_style(),
192196
TagEnd::Strikethrough => self.pop_inline_style(),
193-
TagEnd::Link => {}
197+
TagEnd::Link => {
198+
// Append the link to the previous span (the title)
199+
if let Some(span) = self.will_append_link.take() {
200+
self.push_span(span);
201+
}
202+
}
194203
TagEnd::Image => {}
195204
TagEnd::MetadataBlock(_) => {}
196205
TagEnd::DefinitionList => {}
@@ -413,6 +422,12 @@ where
413422
self.push_line(Line::from(vec![span]));
414423
}
415424
}
425+
426+
#[instrument(level = "trace", skip(self))]
427+
fn will_append_link(&mut self, dest_url: CowStr<'a>) {
428+
let span = Span::from(format!(" ({})", dest_url));
429+
self.will_append_link = Some(span);
430+
}
416431
}
417432

418433
mod styles {
@@ -763,4 +778,15 @@ mod tests {
763778
]))
764779
);
765780
}
781+
782+
#[rstest]
783+
fn link(with_tracing: DefaultGuard) {
784+
assert_eq!(
785+
from_str("[Link](https://example.com)"),
786+
Text::from(Line::from_iter([
787+
Span::from("Link"),
788+
Span::from(" (https://example.com)")
789+
]))
790+
);
791+
}
766792
}

0 commit comments

Comments
 (0)