| 1 | package extension |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/yuin/goldmark" |
| 8 | "github.com/yuin/goldmark/renderer/html" |
| 9 | "github.com/yuin/goldmark/testutil" |
| 10 | ) |
| 11 | |
| 12 | func TestLinkify(t *testing.T) { |
| 13 | markdown := goldmark.New( |
| 14 | goldmark.WithRendererOptions( |
| 15 | html.WithUnsafe(), |
| 16 | ), |
| 17 | goldmark.WithExtensions( |
| 18 | Linkify, |
| 19 | ), |
| 20 | ) |
| 21 | testutil.DoTestCaseFile(markdown, "_test/linkify.txt", t, testutil.ParseCliCaseArg()...) |
| 22 | } |
| 23 | |
| 24 | func TestLinkifyWithAllowedProtocols(t *testing.T) { |
| 25 | markdown := goldmark.New( |
| 26 | goldmark.WithRendererOptions( |
| 27 | html.WithXHTML(), |
| 28 | html.WithUnsafe(), |
| 29 | ), |
| 30 | goldmark.WithExtensions( |
| 31 | NewLinkify( |
| 32 | WithLinkifyAllowedProtocols([]string{ |
| 33 | "ssh:", |
| 34 | }), |
| 35 | WithLinkifyURLRegexp( |
| 36 | regexp.MustCompile(`\w+://[^\s]+`), |
| 37 | ), |
| 38 | ), |
| 39 | ), |
| 40 | ) |
| 41 | testutil.DoTestCase( |
| 42 | markdown, |
| 43 | testutil.MarkdownTestCase{ |
| 44 | No: 1, |
| 45 | Markdown: `hoge ssh://[email protected]. http://example.com/`, |
| 46 | Expected: `<p>hoge <a href="ssh://[email protected]">ssh://[email protected]</a>. http://example.com/</p>`, |
| 47 | }, |
| 48 | t, |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | func TestLinkifyWithWWWRegexp(t *testing.T) { |
| 53 | markdown := goldmark.New( |
| 54 | goldmark.WithRendererOptions( |
| 55 | html.WithXHTML(), |
| 56 | html.WithUnsafe(), |
| 57 | ), |
| 58 | goldmark.WithExtensions( |
| 59 | NewLinkify( |
| 60 | WithLinkifyWWWRegexp( |
| 61 | regexp.MustCompile(`www\.example\.com`), |
| 62 | ), |
| 63 | ), |
| 64 | ), |
| 65 | ) |
| 66 | testutil.DoTestCase( |
| 67 | markdown, |
| 68 | testutil.MarkdownTestCase{ |
| 69 | No: 1, |
| 70 | Markdown: `www.google.com www.example.com`, |
| 71 | Expected: `<p>www.google.com <a href="http://www.example.com">www.example.com</a></p>`, |
| 72 | }, |
| 73 | t, |
| 74 | ) |
| 75 | } |
| 76 | |
| 77 | func TestLinkifyWithEmailRegexp(t *testing.T) { |
| 78 | markdown := goldmark.New( |
| 79 | goldmark.WithRendererOptions( |
| 80 | html.WithXHTML(), |
| 81 | html.WithUnsafe(), |
| 82 | ), |
| 83 | goldmark.WithExtensions( |
| 84 | NewLinkify( |
| 85 | WithLinkifyEmailRegexp( |
| 86 | regexp.MustCompile(`user@example\.com`), |
| 87 | ), |
| 88 | ), |
| 89 | ), |
| 90 | ) |
| 91 | testutil.DoTestCase( |
| 92 | markdown, |
| 93 | testutil.MarkdownTestCase{ |
| 94 | No: 1, |
| 95 | Markdown: `[email protected] [email protected]`, |
| 96 | Expected: `<p>[email protected] <a href="mailto:[email protected]">[email protected]</a></p>`, |
| 97 | }, |
| 98 | t, |
| 99 | ) |
| 100 | } |