Skip to content

Commit

Permalink
fix <a> is nested when a text inside a link is auto-linked
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Sep 21, 2024
1 parent 56b99c2 commit 407b98c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
15 changes: 12 additions & 3 deletions v2/src/markdown/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu
fn events(&mut self, parser: Parser<'input>) -> Result<()> {
let mut in_code_block = false;
let mut in_auto_link = false;
let mut in_link = false;

let mut events = parser.into_offset_iter().peekable();
while let Some((event, range)) = events.next() {
Expand Down Expand Up @@ -669,6 +670,8 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu
self.out.write_all(br#","title":"#)?;
self.string(&title)?;
}

in_link = true;
}
Image { dest_url, title, .. } => {
self.tag("img")?;
Expand Down Expand Up @@ -717,9 +720,14 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu
use TagEnd::*;
match tag_end {
Link if in_auto_link => in_auto_link = false,
Link => {
in_link = false;
self.tag_end()?
}
Paragraph | Heading(_) | TableRow | TableCell | BlockQuote(_) | List(_)
| Item | Emphasis | Strong | Strikethrough | Link | Image
| FootnoteDefinition => self.tag_end()?,
| Item | Emphasis | Strong | Strikethrough | Image | FootnoteDefinition => {
self.tag_end()?
}
CodeBlock => {
in_code_block = false;
self.tag_end()?;
Expand All @@ -742,7 +750,7 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu
| MetadataBlock(_) => unreachable!("disabled markdown feature"), // This option is not enabled
}
}
Event::Text(text) if in_code_block => self.text(&text, range)?,
Event::Text(text) if in_code_block || in_link => self.text(&text, range)?,
Event::Text(text) => self.autolink_text(&text, range)?,
Event::Code(text) => {
let pad = (range.len() - text.len()) / 2;
Expand Down Expand Up @@ -1042,6 +1050,7 @@ mod tests {
snapshot_test!(inline_items_nested_in_inline_html);
snapshot_test!(escaped_chars_in_text);
snapshot_test!(alert);
snapshot_test!(url_inside_link);

// Offset
snapshot_test!(offset_block, Some(30));
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions v2/src/markdown/testdata/url_inside_link.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 407b98c

Please sign in to comment.