Skip to content

Commit

Permalink
wrap lines in multiline posts with p tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Dec 23, 2023
1 parent c387ae9 commit 7db10ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion front/text/plain/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,16 @@ func ToHTML(text string) string {
text = strings.ReplaceAll(text, link, fmt.Sprintf(`<a href="%s" target="_blank">%s</a>`, 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("<p>")
b.WriteString(line)
b.WriteString("</p>")
}
return b.String()
}
16 changes: 16 additions & 0 deletions front/text/plain/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<p>this is a line</p><p>this is another line</p>`

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: <a href="gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh" target="_blank">gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh</a>`
Expand All @@ -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 := `<p>this is a plain post with a link: <a href="gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh" target="_blank">gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh</a></p><p>... and a line break</p>`

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 := `<a href="gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh" target="_blank">gemini://aa.bb.com/cc?dd=ee&ff=gg%20hh</a> is a link`
Expand Down

0 comments on commit 7db10ec

Please sign in to comment.