108 lines
6.2 KiB
Go
108 lines
6.2 KiB
Go
package markdown_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
"git.akyoto.dev/go/markdown"
|
|
)
|
|
|
|
func TestEmpty(t *testing.T) {
|
|
assert.Equal(t, markdown.Render(""), "")
|
|
}
|
|
|
|
func TestParagraph(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("Text"), "<p>Text</p>")
|
|
assert.Equal(t, markdown.Render("Text\n"), "<p>Text</p>")
|
|
assert.Equal(t, markdown.Render("Text\n\n"), "<p>Text</p>")
|
|
assert.Equal(t, markdown.Render("Text\n\n\n"), "<p>Text</p>")
|
|
assert.Equal(t, markdown.Render("Line 1\nLine 2"), "<p>Line 1 Line 2</p>")
|
|
assert.Equal(t, markdown.Render("Line 1\n\nLine 2"), "<p>Line 1</p><p>Line 2</p>")
|
|
}
|
|
|
|
func TestHeader(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("# Header"), "<h1>Header</h1>")
|
|
assert.Equal(t, markdown.Render("## Header"), "<h2>Header</h2>")
|
|
assert.Equal(t, markdown.Render("### Header"), "<h3>Header</h3>")
|
|
assert.Equal(t, markdown.Render("#### Header"), "<h4>Header</h4>")
|
|
assert.Equal(t, markdown.Render("##### Header"), "<h5>Header</h5>")
|
|
assert.Equal(t, markdown.Render("###### Header"), "<h6>Header</h6>")
|
|
}
|
|
|
|
func TestItalic(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("*italic*"), "<p><em>italic</em></p>")
|
|
assert.Equal(t, markdown.Render("_italic_"), "<p><em>italic</em></p>")
|
|
}
|
|
|
|
func TestBold(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("**bold**"), "<p><strong>bold</strong></p>")
|
|
assert.Equal(t, markdown.Render("__bold__"), "<p><strong>bold</strong></p>")
|
|
}
|
|
|
|
func TestStrike(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("~normal text~"), "<p>~normal text~</p>")
|
|
assert.Equal(t, markdown.Render("~~deleted text~~"), "<p><del>deleted text</del></p>")
|
|
}
|
|
|
|
func TestLink(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("[text](https://example.com/)"), "<p><a href=\"https://example.com/\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[text](https://example.com/"), "<p>[text](https://example.com/</p>")
|
|
assert.Equal(t, markdown.Render("[text]https://example.com/)"), "<p>[text]https://example.com/)</p>")
|
|
assert.Equal(t, markdown.Render("[text(https://example.com/)"), "<p>[text(https://example.com/)</p>")
|
|
assert.Equal(t, markdown.Render("text](https://example.com/)"), "<p>text](https://example.com/)</p>")
|
|
assert.Equal(t, markdown.Render("Prefix [text](https://example.com/) suffix."), "<p>Prefix <a href=\"https://example.com/\">text</a> suffix.</p>")
|
|
}
|
|
|
|
func TestList(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("- Entry"), "<ul><li>Entry</li></ul>")
|
|
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2"), "<ul><li>Entry 1</li><li>Entry 2</li></ul>")
|
|
assert.Equal(t, markdown.Render("- Entry 1\n- Entry 2\n- Entry 3"), "<ul><li>Entry 1</li><li>Entry 2</li><li>Entry 3</li></ul>")
|
|
assert.Equal(t, markdown.Render("-"), "<p>-</p>")
|
|
}
|
|
|
|
func TestTables(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("| Head |\n| --- |\n| Body |"), "<table><thead><tr><th>Head</th></tr></thead><tbody><tr><td>Body</td></tr></tbody></table>")
|
|
assert.Equal(t, markdown.Render("| 1 | 2 |\n| --- | --- |\n| 1 | 2 |"), "<table><thead><tr><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody></table>")
|
|
}
|
|
|
|
func TestCode(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("```\nText\n```"), "<pre><code>Text</code></pre>")
|
|
assert.Equal(t, markdown.Render("```go\ntype A struct {\n\t\n}\n```"), "<pre><code class=\"language-go\">type A struct {\n\t\n}</code></pre>")
|
|
assert.Equal(t, markdown.Render("`monospace`"), "<p><code>monospace</code></p>")
|
|
assert.Equal(t, markdown.Render("Inline `monospace` text."), "<p>Inline <code>monospace</code> text.</p>")
|
|
}
|
|
|
|
func TestQuote(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("> Line"), "<blockquote><p>Line</p></blockquote>")
|
|
assert.Equal(t, markdown.Render("> Line 1\n> Line 2"), "<blockquote><p>Line 1 Line 2</p></blockquote>")
|
|
assert.Equal(t, markdown.Render("> Line 1\n\nLine 2"), "<blockquote><p>Line 1</p></blockquote><p>Line 2</p>")
|
|
assert.Equal(t, markdown.Render("> Line 1\n>>Line 2"), "<blockquote><p>Line 1</p><blockquote><p>Line 2</p></blockquote></blockquote>")
|
|
assert.Equal(t, markdown.Render("Line 1\n> Line 2\n> Line 3\nLine 4"), "<p>Line 1</p><blockquote><p>Line 2 Line 3</p></blockquote><p>Line 4</p>")
|
|
}
|
|
|
|
func TestSeparator(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("---"), "<hr>")
|
|
assert.Equal(t, markdown.Render("Line 1\n\n---\n\nLine 2"), "<p>Line 1</p><hr><p>Line 2</p>")
|
|
}
|
|
|
|
func TestCombined(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("# Header\n\nLine 1."), "<h1>Header</h1><p>Line 1.</p>")
|
|
assert.Equal(t, markdown.Render("# Header\nLine 1.\nLine 2.\nLine 3."), "<h1>Header</h1><p>Line 1. Line 2. Line 3.</p>")
|
|
assert.Equal(t, markdown.Render("# Header 1\nLine 1.\n# Header 2\nLine 2."), "<h1>Header 1</h1><p>Line 1.</p><h1>Header 2</h1><p>Line 2.</p>")
|
|
assert.Equal(t, markdown.Render("# [Header Link](https://example.com/)"), "<h1><a href=\"https://example.com/\">Header Link</a></h1>")
|
|
assert.Equal(t, markdown.Render("# Title\n\n- Entry 1\n- Entry 2\n\nText."), "<h1>Title</h1><ul><li>Entry 1</li><li>Entry 2</li></ul><p>Text.</p>")
|
|
assert.Equal(t, markdown.Render("- Entry\n# Header"), "<ul><li>Entry</li></ul><h1>Header</h1>")
|
|
assert.Equal(t, markdown.Render("> - Entry\n> # Header"), "<blockquote><ul><li>Entry</li></ul><h1>Header</h1></blockquote>")
|
|
assert.Equal(t, markdown.Render("> **bold** and *italic* text."), "<blockquote><p><strong>bold</strong> and <em>italic</em> text.</p></blockquote>")
|
|
assert.Equal(t, markdown.Render("> __bold__ and _italic_ text."), "<blockquote><p><strong>bold</strong> and <em>italic</em> text.</p></blockquote>")
|
|
}
|
|
|
|
func TestSecurity(t *testing.T) {
|
|
assert.Equal(t, markdown.Render("[text](javascript:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[text](javAscRipt:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[text]( javascript:alert(\"xss\"))"), "<p><a href=\"\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[text]('javAscRipt:alert(\"xss\")')"), "<p><a href=\"'javAscRipt:alert("xss")'\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[text](\"><script>alert(123)</script>)"), "<p><a href=\""><script>alert(123)</script>\">text</a></p>")
|
|
assert.Equal(t, markdown.Render("[<script>alert(123)</script>]()"), "<p><a href=\"\"><script>alert(123)</script></a></p>")
|
|
}
|