Skip to content

Commit

Permalink
Added tests for convert_footnotes_to_github_styles()
Browse files Browse the repository at this point in the history
  • Loading branch information
totikom committed Apr 24, 2024
1 parent 4727752 commit db7f4e8
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 4 deletions.
98 changes: 94 additions & 4 deletions components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn get_heading_refs(events: &[Event]) -> Vec<HeadingRef> {
heading_refs
}

fn convert_footnotes_to_github_styles(old_events: &mut Vec<Event>) {
fn convert_footnotes_to_github_style(old_events: &mut Vec<Event>) {
let events = std::mem::take(old_events);
// step 1: We need to extract footnotes from the event stream and tweak footnote references

Expand Down Expand Up @@ -778,7 +778,7 @@ pub fn markdown_to_html(
}

if context.config.markdown.bottom_footnotes {
convert_footnotes_to_github_styles(&mut events);
convert_footnotes_to_github_style(&mut events);
}

cmark::html::push_html(&mut html, events.into_iter());
Expand All @@ -800,10 +800,10 @@ pub fn markdown_to_html(
#[cfg(test)]
mod tests {
use config::Config;

use super::*;
#[test]
use insta::assert_debug_snapshot;

#[test]
fn insert_many_works() {
let mut v = vec![1, 2, 3, 4, 5];
insert_many(&mut v, vec![(0, 0), (2, -1), (5, 6)]);
Expand Down Expand Up @@ -872,4 +872,94 @@ mod tests {
assert_eq!(body, &bottom_rendered);
}
}

#[test]
fn no_footnotes() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "Some text *without* footnotes.\n\nOnly ~~fancy~~ formatting.";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}

#[test]
fn single_footnote() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "This text has a footnote[^1]\n [^1]:But it is meaningless.";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}

#[test]
fn reordered_footnotes() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "This text has two[^2] footnotes[^1]\n[^1]: not sorted.\n[^2]: But they are";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}

#[test]
fn def_before_use() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "[^1]:It's before the reference.\n\n There is footnote definition?[^1]";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}

#[test]
fn multiple_refs() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "This text has two[^1] identical footnotes[^1]\n[^1]: So one is present.\n[^2]: But another in not.";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}

#[test]
fn footnote_inside_footnote() {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);

let content = "This text has a footnote[^1]\n[^1]: But the footnote has another footnote[^2].\n[^2]: That's it.";
let mut events: Vec<_> = Parser::new_ext(&content, opts).collect();
convert_footnotes_to_github_style(&mut events);
assert_debug_snapshot!(events);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: components/markdown/src/markdown.rs
expression: events
---
[
Start(
Paragraph,
),
Text(
Borrowed(
"There is footnote definition?",
),
),
Html(
Boxed(
"<sup class=\"footnote-reference\" id=\"fr-1-1\"><a href=\"#fn-1\">[1]</a></sup>",
),
),
End(
Paragraph,
),
Html(
Borrowed(
"<hr><ol class=\"footnotes-list\">\n",
),
),
Html(
Boxed(
"<li id=\"fn-1\">",
),
),
Start(
Paragraph,
),
Text(
Borrowed(
"It's before the reference.",
),
),
Html(
Boxed(
" <a href=\"#fr-1-1\">↩</a></p>\n",
),
),
Html(
Borrowed(
"</li>\n",
),
),
Html(
Borrowed(
"</ol>\n",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
source: components/markdown/src/markdown.rs
expression: events
---
[
Start(
Paragraph,
),
Text(
Borrowed(
"This text has a footnote",
),
),
Html(
Boxed(
"<sup class=\"footnote-reference\" id=\"fr-1-1\"><a href=\"#fn-1\">[1]</a></sup>",
),
),
End(
Paragraph,
),
Html(
Borrowed(
"<hr><ol class=\"footnotes-list\">\n",
),
),
Html(
Boxed(
"<li id=\"fn-1\">",
),
),
Start(
Paragraph,
),
Text(
Borrowed(
"But the footnote has another footnote",
),
),
Html(
Boxed(
"<sup class=\"footnote-reference\" id=\"fr-2-1\"><a href=\"#fn-2\">[2]</a></sup>",
),
),
Text(
Borrowed(
".",
),
),
Html(
Boxed(
" <a href=\"#fr-1-1\">↩</a></p>\n",
),
),
Html(
Borrowed(
"</li>\n",
),
),
Html(
Boxed(
"<li id=\"fn-2\">",
),
),
Start(
Paragraph,
),
Text(
Borrowed(
"That's it.",
),
),
Html(
Boxed(
" <a href=\"#fr-2-1\">↩</a></p>\n",
),
),
Html(
Borrowed(
"</li>\n",
),
),
Html(
Borrowed(
"</ol>\n",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
source: components/markdown/src/markdown.rs
expression: events
---
[
Start(
Paragraph,
),
Text(
Borrowed(
"This text has two",
),
),
Html(
Boxed(
"<sup class=\"footnote-reference\" id=\"fr-1-1\"><a href=\"#fn-1\">[1]</a></sup>",
),
),
Text(
Borrowed(
" identical footnotes",
),
),
Html(
Boxed(
"<sup class=\"footnote-reference\" id=\"fr-1-2\"><a href=\"#fn-1\">[1]</a></sup>",
),
),
End(
Paragraph,
),
Html(
Borrowed(
"<hr><ol class=\"footnotes-list\">\n",
),
),
Html(
Boxed(
"<li id=\"fn-1\">",
),
),
Start(
Paragraph,
),
Text(
Borrowed(
"So one is present.",
),
),
Html(
Boxed(
" <a href=\"#fr-1-1\">↩</a> <a href=\"#fr-1-2\">↩2</a></p>\n",
),
),
Html(
Borrowed(
"</li>\n",
),
),
Html(
Borrowed(
"</ol>\n",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
source: components/markdown/src/markdown.rs
expression: events
---
[
Start(
Paragraph,
),
Text(
Borrowed(
"Some text ",
),
),
Start(
Emphasis,
),
Text(
Borrowed(
"without",
),
),
End(
Emphasis,
),
Text(
Borrowed(
" footnotes.",
),
),
End(
Paragraph,
),
Start(
Paragraph,
),
Text(
Borrowed(
"Only ",
),
),
Start(
Strikethrough,
),
Text(
Borrowed(
"fancy",
),
),
End(
Strikethrough,
),
Text(
Borrowed(
" formatting.",
),
),
End(
Paragraph,
),
]
Loading

0 comments on commit db7f4e8

Please sign in to comment.