@@ -95,6 +95,9 @@ struct TextWriter<'a, I> {
95
95
/// Current list index as a stack of indices.
96
96
list_indices : Vec < Option < u64 > > ,
97
97
98
+ /// Whether a link should be appended
99
+ will_append_link : Option < Span < ' a > > ,
100
+
98
101
needs_newline : bool ,
99
102
}
100
103
@@ -118,6 +121,7 @@ where
118
121
needs_newline : false ,
119
122
#[ cfg( feature = "highlight-code" ) ]
120
123
code_highlighter : None ,
124
+ will_append_link : None ,
121
125
}
122
126
}
123
127
@@ -164,7 +168,7 @@ where
164
168
Tag :: Emphasis => self . push_inline_style ( Style :: new ( ) . italic ( ) ) ,
165
169
Tag :: Strong => self . push_inline_style ( Style :: new ( ) . bold ( ) ) ,
166
170
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 ) ,
168
172
Tag :: Image { .. } => warn ! ( "Image not yet supported" ) ,
169
173
Tag :: MetadataBlock ( _) => warn ! ( "Metadata block not yet supported" ) ,
170
174
Tag :: DefinitionList => warn ! ( "Definition list not yet supported" ) ,
@@ -190,7 +194,12 @@ where
190
194
TagEnd :: Emphasis => self . pop_inline_style ( ) ,
191
195
TagEnd :: Strong => self . pop_inline_style ( ) ,
192
196
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
+ }
194
203
TagEnd :: Image => { }
195
204
TagEnd :: MetadataBlock ( _) => { }
196
205
TagEnd :: DefinitionList => { }
@@ -413,6 +422,12 @@ where
413
422
self . push_line ( Line :: from ( vec ! [ span] ) ) ;
414
423
}
415
424
}
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
+ }
416
431
}
417
432
418
433
mod styles {
@@ -763,4 +778,15 @@ mod tests {
763
778
] ) )
764
779
) ;
765
780
}
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
+ }
766
792
}
0 commit comments