Skip to content

Commit

Permalink
replace br tags with a single \n, not two
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Dec 23, 2023
1 parent 7db10ec commit 5fe8909
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions front/text/plain/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var (
mentionTags = regexp.MustCompile(`<a\s+(?:[^\s<]+\s+)*class="(?:[^\s"]+\s+)*mention(?:\s+[^\s"]+)*"[^>]*>`)
invisibleSpanTags = regexp.MustCompile(`<span class="invisible">[^<]*</span>`)
ellipsisSpanTags = regexp.MustCompile(`<span class="ellipsis">[^<]*</span>`)
brTags = regexp.MustCompile(`<(?:br\s*\/*|\/p|\/h\d+)>`)
pTags = regexp.MustCompile(`<(?:/p|\/h\d+)>`)
brTags = regexp.MustCompile(`<br\s*\/*>`)
openTags = regexp.MustCompile(`(?:<[a-zA-Z0-9]+\s*[^>]*>)+`)
closeTags = regexp.MustCompile(`(?:<\/[a-zA-Z0-9]+\s*[^>]*>)+`)
urlRegex = regexp.MustCompile(`\b(https|http|gemini|gopher|gophers):\/\/\S+\b`)
Expand All @@ -46,10 +47,14 @@ func FromHTML(text string) (string, data.OrderedMap[string, string]) {
res = strings.Replace(res, m, "", 1)
}

for _, m := range brTags.FindAllString(res, -1) {
for _, m := range pTags.FindAllString(res, -1) {
res = strings.Replace(res, m, "\n\n", 1)
}

for _, m := range brTags.FindAllString(res, -1) {
res = strings.Replace(res, m, "\n", 1)
}

for _, m := range invisibleSpanTags.FindAllString(res, -1) {
res = strings.Replace(res, m, "", 1)
}
Expand Down
2 changes: 1 addition & 1 deletion front/text/plain/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestFromHTML_TitleParagraphSubtitleAndParagraph(t *testing.T) {

func TestFromHTML_LineBreak(t *testing.T) {
post := `<p>this is a line<br/>this is another line</p>`
expected := "this is a line\n\nthis is another line"
expected := "this is a line\nthis is another line"
expectedLinks := data.OrderedMap[string, string]{}

raw, links := FromHTML(post)
Expand Down

0 comments on commit 5fe8909

Please sign in to comment.