use markdown::{ mdast::{Emphasis, Node, Paragraph, Root, Strong, Text}, to_html, to_html_with_options, to_mdast, unist::Position, CompileOptions, Constructs, Options, ParseOptions, }; use pretty_assertions::assert_eq; #[test] fn attention() -> Result<(), String> { let danger = Options { compile: CompileOptions { allow_dangerous_html: true, allow_dangerous_protocol: true, ..Default::default() }, ..Default::default() }; // Rule 1. assert_eq!( to_html("*foo bar*"), "
foo bar
", "should support emphasis w/ `*`" ); assert_eq!( to_html("a * foo bar*"), "a * foo bar*
", "should not support emphasis if the opening is not left flanking (1)" ); assert_eq!( to_html("a*\"foo\"*"), "a*"foo"*
", "should not support emphasis if the opening is not left flanking (2b)" ); assert_eq!( to_html("* a *"), "* a *
", "should not support emphasis unicode whitespace either" ); assert_eq!( to_html("foo*bar*"), "foobar
", "should support intraword emphasis w/ `*` (1)" ); assert_eq!( to_html("5*6*78"), "5678
", "should support intraword emphasis w/ `*` (2)" ); // Rule 2. assert_eq!( to_html("_foo bar_"), "foo bar
", "should support emphasis w/ `_`" ); assert_eq!( to_html("_ foo bar_"), "_ foo bar_
", "should not support emphasis if the opening is followed by whitespace" ); assert_eq!( to_html("a_\"foo\"_"), "a_"foo"_
", "should not support emphasis if the opening is preceded by something else and followed by punctuation" ); assert_eq!( to_html("foo_bar_"), "foo_bar_
", "should not support intraword emphasis (1)" ); assert_eq!( to_html("5_6_78"), "5_6_78
", "should not support intraword emphasis (2)" ); assert_eq!( to_html("пристаням_стремятся_"), "пристаням_стремятся_
", "should not support intraword emphasis (3)" ); assert_eq!( to_html("aa_\"bb\"_cc"), "aa_"bb"_cc
", "should not support emphasis if the opening is right flanking and the closing is left flanking" ); assert_eq!( to_html("foo-_(bar)_"), "foo-(bar)
", "should support emphasis if the opening is both left and right flanking, if it’s preceded by punctuation" ); // Rule 3. assert_eq!( to_html("_foo*"), "_foo*
", "should not support emphasis if opening and closing markers don’t match" ); assert_eq!( to_html("*foo bar *"), "*foo bar *
", "should not support emphasis w/ `*` if the closing markers are preceded by whitespace" ); assert_eq!( to_html("*foo bar\n*"), "*foo bar\n*
", "should not support emphasis w/ `*` if the closing markers are preceded by a line break (also whitespace)" ); assert_eq!( to_html("*(*foo)"), "*(*foo)
", "should not support emphasis w/ `*` if the closing markers are not right flanking" ); assert_eq!( to_html("*(*foo*)*"), "(foo)
", "should support nested emphasis" ); // Rule 4. assert_eq!( to_html("_foo bar _"), "_foo bar _
", "should not support emphasis if the closing `_` is preceded by whitespace" ); assert_eq!( to_html("_(_foo)"), "_(_foo)
", "should not support emphasis w/ `_` if the closing markers are not right flanking" ); assert_eq!( to_html("_(_foo_)_"), "(foo)
", "should support nested emphasis w/ `_`" ); assert_eq!( to_html("_foo_bar"), "_foo_bar
", "should not support intraword emphasis w/ `_` (1)" ); assert_eq!( to_html("_пристаням_стремятся"), "_пристаням_стремятся
", "should not support intraword emphasis w/ `_` (2)" ); assert_eq!( to_html("_foo_bar_baz_"), "foo_bar_baz
", "should not support intraword emphasis w/ `_` (3)" ); assert_eq!( to_html("_(bar)_."), "(bar).
", "should support emphasis if the opening is both left and right flanking, if it’s followed by punctuation" ); // Rule 5. assert_eq!( to_html("**foo bar**"), "foo bar
", "should support strong emphasis" ); assert_eq!( to_html("** foo bar**"), "** foo bar**
", "should not support strong emphasis if the opening is followed by whitespace" ); assert_eq!( to_html("a**\"foo\"**"), "a**"foo"**
", "should not support strong emphasis if the opening is preceded by something else and followed by punctuation" ); assert_eq!( to_html("foo**bar**"), "foobar
", "should support strong intraword emphasis" ); // Rule 6. assert_eq!( to_html("__foo bar__"), "foo bar
", "should support strong emphasis w/ `_`" ); assert_eq!( to_html("__ foo bar__"), "__ foo bar__
", "should not support strong emphasis if the opening is followed by whitespace" ); assert_eq!( to_html("__\nfoo bar__"), "__\nfoo bar__
", "should not support strong emphasis if the opening is followed by a line ending (also whitespace)" ); assert_eq!( to_html("a__\"foo\"__"), "a__"foo"__
", "should not support strong emphasis if the opening is preceded by something else and followed by punctuation" ); assert_eq!( to_html("foo__bar__"), "foo__bar__
", "should not support strong intraword emphasis w/ `_` (1)" ); assert_eq!( to_html("5__6__78"), "5__6__78
", "should not support strong intraword emphasis w/ `_` (2)" ); assert_eq!( to_html("пристаням__стремятся__"), "пристаням__стремятся__
", "should not support strong intraword emphasis w/ `_` (3)" ); assert_eq!( to_html("__foo, __bar__, baz__"), "foo, bar, baz
", "should support nested strong emphasis" ); assert_eq!( to_html("foo-__(bar)__"), "foo-(bar)
", "should support strong emphasis if the opening is both left and right flanking, if it’s preceded by punctuation" ); // Rule 7. assert_eq!( to_html("**foo bar **"), "**foo bar **
", "should not support strong emphasis w/ `*` if the closing is preceded by whitespace" ); assert_eq!( to_html("**(**foo)"), "**(**foo)
", "should not support strong emphasis w/ `*` if the closing is preceded by punctuation and followed by something else" ); assert_eq!( to_html("*(**foo**)*"), "(foo)
", "should support strong emphasis in emphasis" ); assert_eq!( to_html( "**Gomphocarpus (*Gomphocarpus physocarpus*, syn.\n*Asclepias physocarpa*)**" ), "Gomphocarpus (Gomphocarpus physocarpus, syn.\nAsclepias physocarpa)
", "should support emphasis in strong emphasis (1)" ); assert_eq!( to_html("**foo \"*bar*\" foo**"), "foo "bar" foo
", "should support emphasis in strong emphasis (2)" ); assert_eq!( to_html("**foo**bar"), "foobar
", "should support strong intraword emphasis" ); // Rule 8. assert_eq!( to_html("__foo bar __"), "__foo bar __
", "should not support strong emphasis w/ `_` if the closing is preceded by whitespace" ); assert_eq!( to_html("__(__foo)"), "__(__foo)
", "should not support strong emphasis w/ `_` if the closing is preceded by punctuation and followed by something else" ); assert_eq!( to_html("_(__foo__)_"), "(foo)
", "should support strong emphasis w/ `_` in emphasis" ); assert_eq!( to_html("__foo__bar"), "__foo__bar
", "should not support strong intraword emphasis w/ `_` (1)" ); assert_eq!( to_html("__пристаням__стремятся"), "__пристаням__стремятся
", "should not support strong intraword emphasis w/ `_` (2)" ); assert_eq!( to_html("__foo__bar__baz__"), "foo__bar__baz
", "should not support strong intraword emphasis w/ `_` (3)" ); assert_eq!( to_html("__(bar)__."), "(bar).
", "should support strong emphasis if the opening is both left and right flanking, if it’s followed by punctuation" ); // Rule 9. assert_eq!( to_html("*foo [bar](/url)*"), "foo bar
", "should support content in emphasis" ); assert_eq!( to_html("*foo\nbar*"), "foo\nbar
", "should support line endings in emphasis" ); assert_eq!( to_html("_foo __bar__ baz_"), "foo bar baz
", "should support nesting emphasis and strong (1)" ); assert_eq!( to_html("_foo _bar_ baz_"), "foo bar baz
", "should support nesting emphasis and strong (2)" ); assert_eq!( to_html("__foo_ bar_"), "foo bar
", "should support nesting emphasis and strong (3)" ); assert_eq!( to_html("*foo *bar**"), "foo bar
", "should support nesting emphasis and strong (4)" ); assert_eq!( to_html("*foo **bar** baz*"), "foo bar baz
", "should support nesting emphasis and strong (5)" ); assert_eq!( to_html("*foo**bar**baz*"), "foobarbaz
", "should support nesting emphasis and strong (6)" ); assert_eq!( to_html("*foo**bar*"), "foo**bar
", "should not support adjacent emphasis in certain cases" ); assert_eq!( to_html("***foo** bar*"), "foo bar
", "complex (1)" ); assert_eq!( to_html("*foo **bar***"), "foo bar
", "complex (2)" ); assert_eq!( to_html("*foo**bar***"), "foobar
", "complex (3)" ); assert_eq!( to_html("foo***bar***baz"), "foobarbaz
", "complex (a)" ); assert_eq!( to_html("foo******bar*********baz"), "foobar***baz
", "complex (b)" ); assert_eq!( to_html("*foo **bar *baz* bim** bop*"), "foo bar baz bim bop
", "should support indefinite nesting of emphasis (1)" ); assert_eq!( to_html("*foo [*bar*](/url)*"), "foo bar
", "should support indefinite nesting of emphasis (2)" ); assert_eq!( to_html("** is not an empty emphasis"), "** is not an empty emphasis
", "should not support empty emphasis" ); assert_eq!( to_html("**** is not an empty emphasis"), "**** is not an empty emphasis
", "should not support empty strong emphasis" ); // Rule 10. assert_eq!( to_html("**foo [bar](/url)**"), "foo bar
", "should support content in strong emphasis" ); assert_eq!( to_html("**foo\nbar**"), "foo\nbar
", "should support line endings in emphasis" ); assert_eq!( to_html("__foo _bar_ baz__"), "foo bar baz
", "should support nesting emphasis and strong (1)" ); assert_eq!( to_html("__foo __bar__ baz__"), "foo bar baz
", "should support nesting emphasis and strong (2)" ); assert_eq!( to_html("____foo__ bar__"), "foo bar
", "should support nesting emphasis and strong (3)" ); assert_eq!( to_html("**foo **bar****"), "foo bar
", "should support nesting emphasis and strong (4)" ); assert_eq!( to_html("**foo *bar* baz**"), "foo bar baz
", "should support nesting emphasis and strong (5)" ); assert_eq!( to_html("**foo*bar*baz**"), "foobarbaz
", "should support nesting emphasis and strong (6)" ); assert_eq!( to_html("***foo* bar**"), "foo bar
", "should support nesting emphasis and strong (7)" ); assert_eq!( to_html("**foo *bar***"), "foo bar
", "should support nesting emphasis and strong (8)" ); assert_eq!( to_html("**foo *bar **baz**\nbim* bop**"), "foo bar baz\nbim bop
", "should support indefinite nesting of emphasis (1)" ); assert_eq!( to_html("**foo [*bar*](/url)**"), "foo bar
", "should support indefinite nesting of emphasis (2)" ); assert_eq!( to_html("__ is not an empty emphasis"), "__ is not an empty emphasis
", "should not support empty emphasis" ); assert_eq!( to_html("____ is not an empty emphasis"), "____ is not an empty emphasis
", "should not support empty strong emphasis" ); // Rule 11. assert_eq!( to_html("foo ***"), "foo ***
", "should not support emphasis around the same marker" ); assert_eq!( to_html("foo *\\**"), "foo *
", "should support emphasis around an escaped marker" ); assert_eq!( to_html("foo *_*"), "foo _
", "should support emphasis around the other marker" ); assert_eq!( to_html("foo *****"), "foo *****
", "should not support strong emphasis around the same marker" ); assert_eq!( to_html("foo **\\***"), "foo *
", "should support strong emphasis around an escaped marker" ); assert_eq!( to_html("foo **_**"), "foo _
", "should support strong emphasis around the other marker" ); assert_eq!( to_html("**foo*"), "*foo
", "should support a superfluous marker at the start of emphasis" ); assert_eq!( to_html("*foo**"), "foo*
", "should support a superfluous marker at the end of emphasis" ); assert_eq!( to_html("***foo**"), "*foo
", "should support a superfluous marker at the start of strong" ); assert_eq!( to_html("****foo*"), "***foo
", "should support multiple superfluous markers at the start of strong" ); assert_eq!( to_html("**foo***"), "foo*
", "should support a superfluous marker at the end of strong" ); assert_eq!( to_html("*foo****"), "foo***
", "should support multiple superfluous markers at the end of strong" ); // Rule 12. assert_eq!( to_html("foo ___"), "foo ___
", "should not support emphasis around the same marker" ); assert_eq!( to_html("foo _\\__"), "foo _
", "should support emphasis around an escaped marker" ); assert_eq!( to_html("foo _X_"), "foo X
", "should support emphasis around the other marker" ); assert_eq!( to_html("foo _____"), "foo _____
", "should not support strong emphasis around the same marker" ); assert_eq!( to_html("foo __\\___"), "foo _
", "should support strong emphasis around an escaped marker" ); assert_eq!( to_html("foo __X__"), "foo X
", "should support strong emphasis around the other marker" ); assert_eq!( to_html("__foo_"), "_foo
", "should support a superfluous marker at the start of emphasis" ); assert_eq!( to_html("_foo__"), "foo_
", "should support a superfluous marker at the end of emphasis" ); assert_eq!( to_html("___foo__"), "_foo
", "should support a superfluous marker at the start of strong" ); assert_eq!( to_html("____foo_"), "___foo
", "should support multiple superfluous markers at the start of strong" ); assert_eq!( to_html("__foo___"), "foo_
", "should support a superfluous marker at the end of strong" ); assert_eq!( to_html("_foo____"), "foo___
", "should support multiple superfluous markers at the end of strong" ); // Rule 13. assert_eq!( to_html("**foo**"), "foo
", "should support strong w/ `*`" ); assert_eq!( to_html("*_foo_*"), "foo
", "should support emphasis directly in emphasis w/ `_` in `*`" ); assert_eq!( to_html("__foo__"), "foo
", "should support strong w/ `_`" ); assert_eq!( to_html("_*foo*_"), "foo
", "should support emphasis directly in emphasis w/ `*` in `_`" ); assert_eq!( to_html("****foo****"), "foo
", "should support strong emphasis directly in strong emphasis w/ `*`" ); assert_eq!( to_html("____foo____"), "foo
", "should support strong emphasis directly in strong emphasis w/ `_`" ); assert_eq!( to_html("******foo******"), "foo
", "should support indefinite strong emphasis" ); // Rule 14. assert_eq!( to_html("***foo***"), "foo
", "should support strong directly in emphasis w/ `*`" ); assert_eq!( to_html("___foo___"), "foo
", "should support strong directly in emphasis w/ `_`" ); // Rule 15. assert_eq!( to_html("*foo _bar* baz_"), "foo _bar baz_
", "should not support mismatched emphasis" ); assert_eq!( to_html("*foo __bar *baz bim__ bam*"), "foo bar *baz bim bam
", "should not support mismatched strong emphasis" ); // Rule 16. assert_eq!( to_html("**foo **bar baz**"), "**foo bar baz
", "should not shortest strong possible" ); assert_eq!( to_html("*foo *bar baz*"), "*foo bar baz
", "should not shortest emphasis possible" ); // Rule 17. assert_eq!( to_html("*[bar*](/url)"), "*bar*
", "should not mismatch inside links (1)" ); assert_eq!( to_html("_[bar_](/url)"), "_bar_
", "should not mismatch inside links (1)" ); assert_eq!( to_html_with_options("*", &danger)?, "*
", "should not end inside HTML" ); assert_eq!( to_html_with_options("*", &danger)?, "*
", "should not end emphasis inside HTML" ); assert_eq!( to_html_with_options("**", &danger)?, "", "should not end strong inside HTML (1)" ); assert_eq!( to_html_with_options("__", &danger)?, "", "should not end strong inside HTML (2)" ); assert_eq!( to_html("*a `*`*"), "a *
a _
*a*
", "should support turning off attention" ); assert_eq!( to_mdast("a *alpha* b **bravo** c.", &Default::default())?, Node::Root(Root { children: vec![Node::Paragraph(Paragraph { children: vec![ Node::Text(Text { value: "a ".into(), position: Some(Position::new(1, 1, 0, 1, 3, 2)) }), Node::Emphasis(Emphasis { children: vec![Node::Text(Text { value: "alpha".into(), position: Some(Position::new(1, 4, 3, 1, 9, 8)) }),], position: Some(Position::new(1, 3, 2, 1, 10, 9)) }), Node::Text(Text { value: " b ".into(), position: Some(Position::new(1, 10, 9, 1, 13, 12)) }), Node::Strong(Strong { children: vec![Node::Text(Text { value: "bravo".into(), position: Some(Position::new(1, 15, 14, 1, 20, 19)) }),], position: Some(Position::new(1, 13, 12, 1, 22, 21)) }), Node::Text(Text { value: " c.".into(), position: Some(Position::new(1, 22, 21, 1, 25, 24)) }) ], position: Some(Position::new(1, 1, 0, 1, 25, 24)) })], position: Some(Position::new(1, 1, 0, 1, 25, 24)) }), "should support attention as `Emphasis`, `Strong`s in mdast" ); Ok(()) }