19
19
html_logo_url = "https://raw.githubusercontent.com/wooorm/markdown-rs/8924580/media/logo-monochromatic.svg?sanitize=true"
20
20
) ]
21
21
22
- // ^-- Would be nice to use `logo-chromatic`, but that looks horrible on Safari
23
- // at relatively small sizes currently. 😢
24
-
25
22
extern crate alloc;
26
23
27
24
mod construct;
@@ -41,15 +38,12 @@ use alloc::{boxed::Box, fmt, string::String};
41
38
use mdast:: Node ;
42
39
use parser:: parse;
43
40
44
- // Do not use: exported for quick prototyping, will be removed.
45
41
#[ doc( hidden) ]
46
42
pub use util:: identifier:: { id_cont, id_start} ;
47
43
48
- // Do not use: exported for quick prototyping, will be removed.
49
44
#[ doc( hidden) ]
50
45
pub use util:: sanitize_uri:: sanitize;
51
46
52
- // Do not use: exported for quick prototyping, will be removed.
53
47
#[ doc( hidden) ]
54
48
pub use util:: location:: Location ;
55
49
@@ -115,11 +109,11 @@ impl LineEnding {
115
109
///
116
110
/// Panics if `code` is not `\r\n`, `\r`, or `\n`.
117
111
fn from_str ( str : & str ) -> LineEnding {
112
+ debug_assert ! ( matches!( str , "\r \n " | "\r " | "\n " ) , "expected eol" ) ;
118
113
match str {
119
114
"\r \n " => LineEnding :: CarriageReturnLineFeed ,
120
115
"\r " => LineEnding :: CarriageReturn ,
121
- "\n " => LineEnding :: LineFeed ,
122
- _ => unreachable ! ( "invalid str" ) ,
116
+ _ => LineEnding :: LineFeed ,
123
117
}
124
118
}
125
119
}
@@ -1538,6 +1532,7 @@ pub fn to_mdast(value: &str, options: &ParseOptions) -> Result<Node, String> {
1538
1532
mod tests {
1539
1533
extern crate std;
1540
1534
use super :: * ;
1535
+ use alloc:: format;
1541
1536
1542
1537
#[ test]
1543
1538
fn test_line_ending ( ) {
@@ -1576,10 +1571,203 @@ mod tests {
1576
1571
}
1577
1572
1578
1573
#[ test]
1579
- #[ should_panic = "invalid str " ]
1574
+ #[ should_panic = "expected eol " ]
1580
1575
fn test_line_ending_broken ( ) {
1581
1576
// Hide stack trace.
1582
- std:: panic:: set_hook ( Box :: new ( |_| { } ) ) ;
1583
1577
LineEnding :: from_str ( "a" ) ;
1584
1578
}
1579
+
1580
+ #[ test]
1581
+ fn test_constructs ( ) {
1582
+ let constructs = Constructs :: default ( ) ;
1583
+ assert ! ( constructs. attention, "should default to `CommonMark` (1)" ) ;
1584
+ assert ! (
1585
+ !constructs. gfm_autolink_literal,
1586
+ "should default to `CommonMark` (2)"
1587
+ ) ;
1588
+ assert ! (
1589
+ !constructs. mdx_jsx_flow,
1590
+ "should default to `CommonMark` (3)"
1591
+ ) ;
1592
+ assert ! (
1593
+ !constructs. frontmatter,
1594
+ "should default to `CommonMark` (4)"
1595
+ ) ;
1596
+
1597
+ let constructs = Constructs :: gfm ( ) ;
1598
+ assert ! ( constructs. attention, "should support `gfm` shortcut (1)" ) ;
1599
+ assert ! (
1600
+ constructs. gfm_autolink_literal,
1601
+ "should support `gfm` shortcut (2)"
1602
+ ) ;
1603
+ assert ! (
1604
+ !constructs. mdx_jsx_flow,
1605
+ "should support `gfm` shortcut (3)"
1606
+ ) ;
1607
+ assert ! ( !constructs. frontmatter, "should support `gfm` shortcut (4)" ) ;
1608
+
1609
+ let constructs = Constructs :: mdx ( ) ;
1610
+ assert ! ( constructs. attention, "should support `gfm` shortcut (1)" ) ;
1611
+ assert ! (
1612
+ !constructs. gfm_autolink_literal,
1613
+ "should support `mdx` shortcut (2)"
1614
+ ) ;
1615
+ assert ! ( constructs. mdx_jsx_flow, "should support `mdx` shortcut (3)" ) ;
1616
+ assert ! ( !constructs. frontmatter, "should support `mdx` shortcut (4)" ) ;
1617
+ }
1618
+
1619
+ #[ test]
1620
+ fn test_parse_options ( ) {
1621
+ let options = ParseOptions :: default ( ) ;
1622
+ assert ! (
1623
+ options. constructs. attention,
1624
+ "should default to `CommonMark` (1)"
1625
+ ) ;
1626
+ assert ! (
1627
+ !options. constructs. gfm_autolink_literal,
1628
+ "should default to `CommonMark` (2)"
1629
+ ) ;
1630
+ assert ! (
1631
+ !options. constructs. mdx_jsx_flow,
1632
+ "should default to `CommonMark` (3)"
1633
+ ) ;
1634
+
1635
+ let options = ParseOptions :: gfm ( ) ;
1636
+ assert ! (
1637
+ options. constructs. attention,
1638
+ "should support `gfm` shortcut (1)"
1639
+ ) ;
1640
+ assert ! (
1641
+ options. constructs. gfm_autolink_literal,
1642
+ "should support `gfm` shortcut (2)"
1643
+ ) ;
1644
+ assert ! (
1645
+ !options. constructs. mdx_jsx_flow,
1646
+ "should support `gfm` shortcut (3)"
1647
+ ) ;
1648
+
1649
+ let options = ParseOptions :: mdx ( ) ;
1650
+ assert ! (
1651
+ options. constructs. attention,
1652
+ "should support `mdx` shortcut (1)"
1653
+ ) ;
1654
+ assert ! (
1655
+ !options. constructs. gfm_autolink_literal,
1656
+ "should support `mdx` shortcut (2)"
1657
+ ) ;
1658
+ assert ! (
1659
+ options. constructs. mdx_jsx_flow,
1660
+ "should support `mdx` shortcut (3)"
1661
+ ) ;
1662
+
1663
+ assert_eq ! (
1664
+ format!( "{:?}" , ParseOptions :: default ( ) ) ,
1665
+ "ParseOptions { constructs: Constructs { attention: true, autolink: true, block_quote: true, character_escape: true, character_reference: true, code_indented: true, code_fenced: true, code_text: true, definition: true, frontmatter: false, gfm_autolink_literal: false, gfm_footnote_definition: false, gfm_label_start_footnote: false, gfm_strikethrough: false, gfm_table: false, gfm_task_list_item: false, hard_break_escape: true, hard_break_trailing: true, heading_atx: true, heading_setext: true, html_flow: true, html_text: true, label_start_image: true, label_start_link: true, label_end: true, list_item: true, math_flow: false, math_text: false, mdx_esm: false, mdx_expression_flow: false, mdx_expression_text: false, mdx_jsx_flow: false, mdx_jsx_text: false, thematic_break: true }, gfm_strikethrough_single_tilde: true, math_text_single_dollar: true, mdx_expression_parse: None, mdx_esm_parse: None }" ,
1666
+ "should support `Debug` trait"
1667
+ ) ;
1668
+ assert_eq ! (
1669
+ format!( "{:?}" , ParseOptions {
1670
+ mdx_esm_parse: Some ( Box :: new( |_value| {
1671
+ MdxSignal :: Ok
1672
+ } ) ) ,
1673
+ mdx_expression_parse: Some ( Box :: new( |_value, _kind| {
1674
+ MdxSignal :: Ok
1675
+ } ) ) ,
1676
+ ..Default :: default ( )
1677
+ } ) ,
1678
+ "ParseOptions { constructs: Constructs { attention: true, autolink: true, block_quote: true, character_escape: true, character_reference: true, code_indented: true, code_fenced: true, code_text: true, definition: true, frontmatter: false, gfm_autolink_literal: false, gfm_footnote_definition: false, gfm_label_start_footnote: false, gfm_strikethrough: false, gfm_table: false, gfm_task_list_item: false, hard_break_escape: true, hard_break_trailing: true, heading_atx: true, heading_setext: true, html_flow: true, html_text: true, label_start_image: true, label_start_link: true, label_end: true, list_item: true, math_flow: false, math_text: false, mdx_esm: false, mdx_expression_flow: false, mdx_expression_text: false, mdx_jsx_flow: false, mdx_jsx_text: false, thematic_break: true }, gfm_strikethrough_single_tilde: true, math_text_single_dollar: true, mdx_expression_parse: Some(\" [Function]\" ), mdx_esm_parse: Some(\" [Function]\" ) }" ,
1679
+ "should support `Debug` trait on mdx functions"
1680
+ ) ;
1681
+ }
1682
+
1683
+ #[ test]
1684
+ fn test_compile_options ( ) {
1685
+ let options = CompileOptions :: default ( ) ;
1686
+ assert ! (
1687
+ !options. allow_dangerous_html,
1688
+ "should default to safe `CommonMark` (1)"
1689
+ ) ;
1690
+ assert ! (
1691
+ !options. gfm_tagfilter,
1692
+ "should default to safe `CommonMark` (2)"
1693
+ ) ;
1694
+
1695
+ let options = CompileOptions :: gfm ( ) ;
1696
+ assert ! (
1697
+ !options. allow_dangerous_html,
1698
+ "should support safe `gfm` shortcut (1)"
1699
+ ) ;
1700
+ assert ! (
1701
+ options. gfm_tagfilter,
1702
+ "should support safe `gfm` shortcut (1)"
1703
+ ) ;
1704
+ }
1705
+
1706
+ #[ test]
1707
+ fn test_options ( ) {
1708
+ let options = Options :: default ( ) ;
1709
+ assert ! (
1710
+ options. parse. constructs. attention,
1711
+ "should default to safe `CommonMark` (1)"
1712
+ ) ;
1713
+ assert ! (
1714
+ !options. parse. constructs. gfm_autolink_literal,
1715
+ "should default to safe `CommonMark` (2)"
1716
+ ) ;
1717
+ assert ! (
1718
+ !options. parse. constructs. mdx_jsx_flow,
1719
+ "should default to safe `CommonMark` (3)"
1720
+ ) ;
1721
+ assert ! (
1722
+ !options. compile. allow_dangerous_html,
1723
+ "should default to safe `CommonMark` (4)"
1724
+ ) ;
1725
+
1726
+ let options = Options :: gfm ( ) ;
1727
+ assert ! (
1728
+ options. parse. constructs. attention,
1729
+ "should support safe `gfm` shortcut (1)"
1730
+ ) ;
1731
+ assert ! (
1732
+ options. parse. constructs. gfm_autolink_literal,
1733
+ "should support safe `gfm` shortcut (2)"
1734
+ ) ;
1735
+ assert ! (
1736
+ !options. parse. constructs. mdx_jsx_flow,
1737
+ "should support safe `gfm` shortcut (3)"
1738
+ ) ;
1739
+ assert ! (
1740
+ !options. compile. allow_dangerous_html,
1741
+ "should support safe `gfm` shortcut (4)"
1742
+ ) ;
1743
+ }
1744
+
1745
+ #[ test]
1746
+ fn test_to_html ( ) {
1747
+ assert_eq ! (
1748
+ to_html( "a" ) ,
1749
+ "<p>a</p>" ,
1750
+ "should support turning markdown into html with `to_html`"
1751
+ ) ;
1752
+ }
1753
+
1754
+ #[ test]
1755
+ fn test_to_html_with_options ( ) {
1756
+ assert_eq ! (
1757
+ to_html_with_options( "a" , & Options :: default ( ) ) . unwrap( ) ,
1758
+ "<p>a</p>" ,
1759
+ "should support turning markdown into html with `to_html_with_options`"
1760
+ ) ;
1761
+ }
1762
+
1763
+ #[ test]
1764
+ fn test_to_mdast ( ) {
1765
+ assert ! (
1766
+ matches!(
1767
+ to_mdast( "a" , & ParseOptions :: default ( ) ) . unwrap( ) ,
1768
+ mdast:: Node :: Root ( _)
1769
+ ) ,
1770
+ "should support turning markdown into mdast with `to_mdast`"
1771
+ ) ;
1772
+ }
1585
1773
}
0 commit comments