package markdown import ( "reflect" "testing" ) func TestHTML(t *testing.T) { for _, tc := range []struct{ in, want string }{ {"", ""}, {"Hello\nworld", "
Hello world
"}, {"One\n\nTwo", "One
Two
"}, {"### Added\n\n- One\n- Two", "#hashtag
"}, {"Fixes:\n- one\n- two", "Fixes:
We shipped 2. things
"}, {"- a\n - b\n - c\n- d", "After
"}, {"- a\n* b", ""}, {"---", "quoted text
<b>code</b>\n\n xafter
"}, {"**bold** and *em* and _em_ and __bold__", "bold and em and em and bold
"}, {"snake_case_name and 2 * 3 * 4", "snake_case_name and 2 * 3 * 4
"}, {"**bold *em* bold**", "bold em bold
"}, {"`a *b*a *b* <c> and x ` y
*not em*
"}, {"[docs](https://example.com/a?b=1&c=2 \"Title\")", ``}, {"[**bold** link](https://example.com)", ``}, {"[bad](javascript:alert(1))", "bad)
"}, {"[rel](/path)", "rel
"}, {"", ``}, {"https://example.com <b>hi</b>
`}, {"See https://example.com/x_(y). Or (https://example.com).", `See https://example.com/x_(y). Or (https://example.com).
`}, {"", "<script>alert(1)</script>
"}, {`<img src=x onerror="alert(1)">
"}, {"[x](https://a\"onmouseover=\"alert(1))", `x)
`}, {"Café — ünïcode", "Café — ünïcode
"}, {"a\r\nb", "a b
"}, {"**unclosed", "**unclosed
"}, {"[unclosed](https://x", "[unclosed](https://x
"}, } { if got := HTML(Parse(tc.in)); got != tc.want { t.Errorf("HTML(Parse(%q))\n got %s\nwant %s", tc.in, got, tc.want) } } } func TestParse(t *testing.T) { got := Parse("## Fixed\n\n- A **bold [link](https://example.com)** fix\n 1. nested\n\n> `code`") want := []Block{ {Kind: Heading, Level: 2, Inlines: []Inline{{Kind: Text, Text: "Fixed"}}}, {Kind: List, Items: [][]Block{{ {Kind: Paragraph, Inlines: []Inline{ {Kind: Text, Text: "A "}, {Kind: Strong, Children: []Inline{ {Kind: Text, Text: "bold "}, {Kind: Link, URL: "https://example.com", Children: []Inline{{Kind: Text, Text: "link"}}}, }}, {Kind: Text, Text: " fix"}, }}, {Kind: List, Ordered: true, Items: [][]Block{{{Kind: Paragraph, Inlines: []Inline{{Kind: Text, Text: "nested"}}}}}}, }}}, {Kind: Quote, Blocks: []Block{{Kind: Paragraph, Inlines: []Inline{{Kind: CodeSpan, Text: "code"}}}}}, } if !reflect.DeepEqual(got, want) { t.Errorf("Parse\n got %+v\nwant %+v", got, want) } if blocks := Parse(" \n\n"); len(blocks) != 0 { t.Errorf("Parse of blank notes = %+v, want none", blocks) } }