From 7db10ecc6f1e670f15f94c4c393c8e812ec35f8b Mon Sep 17 00:00:00 2001 From: Dima Krasner Date: Sat, 23 Dec 2023 16:50:11 +0200 Subject: [PATCH] wrap lines in multiline posts with p tags --- front/text/plain/convert.go | 13 ++++++++++++- front/text/plain/convert_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/front/text/plain/convert.go b/front/text/plain/convert.go index 3ca3ced4..586478a5 100644 --- a/front/text/plain/convert.go +++ b/front/text/plain/convert.go @@ -122,5 +122,16 @@ func ToHTML(text string) string { text = strings.ReplaceAll(text, link, fmt.Sprintf(`%s`, link, link)) } - return text + lines := strings.Split(text, "\n") + if len(lines) == 1 { + return text + } + + var b strings.Builder + for _, line := range lines { + b.WriteString("

") + b.WriteString(line) + b.WriteString("

") + } + return b.String() } diff --git a/front/text/plain/convert_test.go b/front/text/plain/convert_test.go index 4161208c..e44f5136 100644 --- a/front/text/plain/convert_test.go +++ b/front/text/plain/convert_test.go @@ -143,6 +143,14 @@ func TestToHTML_Plain(t *testing.T) { assert.Equal(t, expected, html) } +func TestToHTML_LineBreak(t *testing.T) { + post := "this is a line\nthis is another line" + expected := `

this is a line

this is another line

` + + html := ToHTML(post) + assert.Equal(t, expected, html) +} + func TestToHTML_Link(t *testing.T) { post := `this is a plain post with a link: gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh` expected := `this is a plain post with a link: gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh` @@ -151,6 +159,14 @@ func TestToHTML_Link(t *testing.T) { assert.Equal(t, expected, html) } +func TestToHTML_LinkAndLineBreak(t *testing.T) { + post := "this is a plain post with a link: gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh\n... and a line break" + expected := `

this is a plain post with a link: gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh

... and a line break

` + + html := ToHTML(post) + assert.Equal(t, expected, html) +} + func TestToHTML_LinkStart(t *testing.T) { post := `gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh is a link` expected := `gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh is a link`