// Copyright (c) 2022 Tulir Asokan // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package format_test import ( "strings" "testing" "github.com/stretchr/testify/assert" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/format/mdext" ) func TestRenderMarkdown_PlainText(t *testing.T) { content := format.RenderMarkdown("hello world", true, true) assert.Equal(t, event.MessageEventContent{MsgType: event.MsgText, Body: "hello world"}, content) content = format.RenderMarkdown("hello world", true, false) assert.Equal(t, event.MessageEventContent{MsgType: event.MsgText, Body: "hello world"}, content) content = format.RenderMarkdown("hello world", false, true) assert.Equal(t, event.MessageEventContent{MsgType: event.MsgText, Body: "hello world"}, content) content = format.RenderMarkdown("hello world", false, false) assert.Equal(t, event.MessageEventContent{MsgType: event.MsgText, Body: "hello world"}, content) } func TestRenderMarkdown_EscapeHTML(t *testing.T) { content := format.RenderMarkdown("hello world", true, false) assert.Equal(t, event.MessageEventContent{ MsgType: event.MsgText, Body: "hello world", Format: event.FormatHTML, FormattedBody: "<b>hello world</b>", }, content) } func TestRenderMarkdown_HTML(t *testing.T) { content := format.RenderMarkdown("hello world", false, true) assert.Equal(t, event.MessageEventContent{ MsgType: event.MsgText, Body: "**hello world**", Format: event.FormatHTML, FormattedBody: "hello world", }, content) content = format.RenderMarkdown("hello world", true, true) assert.Equal(t, event.MessageEventContent{ MsgType: event.MsgText, Body: "**hello world**", Format: event.FormatHTML, FormattedBody: "hello world", }, content) } var spoilerTests = map[string]string{ "test ||bar||": "test bar", "test ||reason|**bar**||": `test bar`, "test ||reason|[bar](https://example.com)||": `test bar`, "test [||reason|foo||](https://example.com)": `test foo`, "test [||foo||](https://example.com)": `test foo`, "test [||_foo_||](https://example.com)": `test foo`, // FIXME wrapping spoilers in italic/bold/strikethrough doesn't work for some reason //"test **[||foo||](https://example.com)**": `test foo`, //"test **||foo||**": `test foo`, "* ||foo||": `
", } func TestRenderMarkdown_Spoiler(t *testing.T) { for markdown, html := range spoilerTests { rendered := format.RenderMarkdown(markdown, true, false) assert.Equal(t, markdown, rendered.Body) assert.Equal(t, html, strings.ReplaceAll(rendered.FormattedBody, "\n", "")) } } var simpleSpoilerTests = map[string]string{ "test ||bar||": "test bar", "test [||foo||](https://example.com)": `test foo`, "test [||*foo*||](https://example.com)": `test foo`, "* ||foo||": `foo
", // Simple spoiler renderer supports wrapping fully already "test **[||foo||](https://example.com)**": `test foo`, "test **||foo||**": `test foo`, "test **||*foo*||**": `test foo`, "test ~~**||*foo*||**~~": `testfoo
", "a \\||test||": "a ||test||", "\\~~test~~": "~~test~~", "\\*test* hmm": "*test* hmm", } func render(renderer goldmark.Markdown, text string) string { var buf strings.Builder err := renderer.Convert([]byte(text), &buf) if err != nil { panic(err) } return buf.String() } func TestRenderMarkdown_SimpleSpoiler(t *testing.T) { renderer := goldmark.New(goldmark.WithExtensions(extension.Strikethrough, mdext.SimpleSpoiler, mdext.EscapeHTML), format.HTMLOptions) for markdown, html := range simpleSpoilerTests { rendered := format.UnwrapSingleParagraph(render(renderer, markdown)) assert.Equal(t, html, strings.ReplaceAll(rendered, "\n", "")) } } var discordUnderlineTests = map[string]string{ "**test**": "test", "*test*": "test", "_test_": "test", "__test__": "test", "__*test*__": "test", "___test___": "test", "____test____": "test", "**__test__**": "test", "__***test***__": "test", "__~~***test***~~__": "
foo