Last active
August 26, 2024 00:14
-
-
Save yshavit/6af0a784e338dc32e66717aa6f495ffe to your computer and use it in GitHub Desktop.
markdown: footnotes within links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[cfg(test)] | |
mod test { | |
use markdown::mdast::{ | |
Definition, FootnoteDefinition, FootnoteReference, Link, LinkReference, Node, Paragraph, ReferenceKind, Root, | |
Text, | |
}; | |
use markdown::ParseOptions; | |
mod inline_link { | |
use super::*; | |
use markdown::mdast::{FootnoteDefinition, FootnoteReference, Node, Root}; | |
use markdown::ParseOptions; | |
const MARKDOWN: &str = " | |
[link text with [^1] footnote](https://example.com) | |
[^1]: footnote text"; | |
#[test] | |
fn check_html_inline_link() { | |
let html = markdown::to_html(MARKDOWN); | |
let expected = r#"<p><a href="https://example.com">link text with [^1] footnote</a></p> | |
<p>[^1]: footnote text</p>"#; | |
assert_eq!(html, expected) | |
} | |
#[test] | |
fn check_ast_inline_link() { | |
let mut ast = markdown::to_mdast(MARKDOWN, &ParseOptions::gfm()).unwrap(); | |
strip_positions(&mut ast); | |
let expected = Node::Root(Root { | |
children: vec![ | |
Node::Paragraph(Paragraph { | |
children: vec![Node::Link(Link { | |
children: vec![ | |
Node::Text(Text { | |
value: "link text with ".to_string(), | |
position: None, | |
}), | |
Node::FootnoteReference(FootnoteReference { | |
identifier: "1".to_string(), | |
label: Some("1".to_string()), | |
position: None, | |
}), | |
Node::Text(Text { | |
value: " footnote".to_string(), | |
position: None, | |
}), | |
], | |
url: "https://example.com".to_string(), | |
title: None, | |
position: None, | |
})], | |
position: None, | |
}), | |
Node::FootnoteDefinition(FootnoteDefinition { | |
identifier: "1".to_string(), | |
children: vec![Node::Paragraph(Paragraph { | |
children: vec![Node::Text(Text { | |
value: "footnote text".to_string(), | |
position: None, | |
})], | |
position: None, | |
})], | |
position: None, | |
label: Some("1".to_string()), | |
}), | |
], | |
position: None, | |
}); | |
assert_eq!(format!("{ast:#?}"), format!("{expected:#?}")); | |
} | |
} | |
mod ref_link { | |
use super::*; | |
const MARKDOWN: &str = " | |
[link text with [^1] footnote][a] | |
[a]: https://example.com | |
[^1]: footnote text"; | |
#[test] | |
fn check_html_ref_link() { | |
let html = markdown::to_html(MARKDOWN); | |
let expected = r#"<p><a href="https://example.com">link text with [^1] footnote</a></p> | |
<p>[^1]: footnote text</p>"#; | |
assert_eq!(html, expected) | |
} | |
#[test] | |
fn check_ast_ref_link() { | |
let mut ast = markdown::to_mdast(MARKDOWN, &ParseOptions::gfm()).unwrap(); | |
strip_positions(&mut ast); | |
let expected = Node::Root(Root { | |
children: vec![ | |
Node::Paragraph(Paragraph { | |
children: vec![Node::LinkReference(LinkReference { | |
children: vec![ | |
Node::Text(Text { | |
value: "link text with ".to_string(), | |
position: None, | |
}), | |
Node::FootnoteReference(FootnoteReference { | |
identifier: "1".to_string(), | |
label: Some("1".to_string()), | |
position: None, | |
}), | |
Node::Text(Text { | |
value: " footnote".to_string(), | |
position: None, | |
}), | |
], | |
reference_kind: ReferenceKind::Full, | |
identifier: "a".to_string(), | |
label: Some("a".to_string()), | |
position: None, | |
})], | |
position: None, | |
}), | |
Node::Definition(Definition { | |
url: "https://example.com".to_string(), | |
identifier: "a".to_string(), | |
label: Some("a".to_string()), | |
title: None, | |
position: None, | |
}), | |
Node::FootnoteDefinition(FootnoteDefinition { | |
identifier: "1".to_string(), | |
children: vec![Node::Paragraph(Paragraph { | |
children: vec![Node::Text(Text { | |
value: "footnote text".to_string(), | |
position: None, | |
})], | |
position: None, | |
})], | |
position: None, | |
label: Some("1".to_string()), | |
}), | |
], | |
position: None, | |
}); | |
assert_eq!(format!("{ast:#?}"), format!("{expected:#?}")); | |
} | |
} | |
/// Strip the positions, so that the test can focus ont the contents. | |
fn strip_positions(node: &mut Node) { | |
node.position_set(None); | |
if let Some(children) = node.children_mut() { | |
for child in children { | |
strip_positions(child); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rendered view of the inline link form:
link text with 1 footnote
Footnotes
footnote text ↩