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", "

Added

"}, {"## 1.2.0 ##", "

1.2.0

"}, {"#hashtag", "

#hashtag

"}, {"Fixes:\n- one\n- two", "

Fixes:

"}, {"1. one\n2. two", "
  1. one
  2. two
"}, {"We shipped\n2. things", "

We shipped 2. things

"}, {"- a\n - b\n - c\n- d", ""}, {"1. a\n - b", "
  1. a
"}, {"- a long\nitem\n- b", ""}, {"- a\n\n more\n- b", ""}, {"- a\n\nAfter", "

After

"}, {"- a\n* b", ""}, {"- a\n1. b", "
  1. b
"}, {"> quoted\n> text", "

quoted text

"}, {"---", "
"}, {"- - -", "
"}, {"```\ncode\n\n x\n```\nafter", "
<b>code</b>\n\n  x

after

"}, {"**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* ` and ``x ` y``", "

a *b* <c> and x ` y

"}, {`\*not em\*`, "

*not em*

"}, {"[docs](https://example.com/a?b=1&c=2 \"Title\")", `

docs

`}, {"[**bold** link](https://example.com)", `

bold link

`}, {"[bad](javascript:alert(1))", "

bad)

"}, {"[rel](/path)", "

rel

"}, {"![logo](https://example.com/logo.png)", `

logo

`}, {" hi", `

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) } }