diff --git a/filters.go b/filters.go index 2298c85..1aa82f5 100644 --- a/filters.go +++ b/filters.go @@ -8,22 +8,24 @@ import ( ) var ( - paraPattern = regexp.MustCompile(`(\n|\r|\r\n)\s*`) + paraPattern = regexp.MustCompile(`(\n\n)\s*`) spacePattern = regexp.MustCompile("( )+") - multiNewlinePattern = regexp.MustCompile(`(\r\n|\r|\n){2,}`) + multiNewlinePattern = regexp.MustCompile(`(\n){2,}`) specialCharsPattern = regexp.MustCompile(`[^a-zA-Z0-9_-]`) ) // PFilter splits the content by new lines and wraps each one in a

tag. func PFilter(content string) template.HTML { - paragraphs := paraPattern.Split(content, -1) + normalized := strings.Replace(content, "\r\n", "\n", -1) + paragraphs := paraPattern.Split(normalized, -1) return template.HTML(fmt.Sprintf("

%s

", strings.Join(paragraphs, "

"))) } // ParaFilter splits the content by new lines and wraps each one in a tag. func ParaFilter(content string) string { - paragraphs := paraPattern.Split(content, -1) + normalized := strings.Replace(content, "\r\n", "\n", -1) + paragraphs := paraPattern.Split(normalized, -1) return fmt.Sprintf("%s", strings.Join(paragraphs, "")) } diff --git a/filters_test.go b/filters_test.go index 4207a8e..f79ece0 100644 --- a/filters_test.go +++ b/filters_test.go @@ -11,9 +11,9 @@ import ( func TestPFilter(t *testing.T) { tests := map[string]string{ "Some content.": "

Some content.

", - "Some content.\nRight here.": "

Some content.

Right here.

", - "Some content.\r\nRight here.": "

Some content.

Right here.

", - "Some content.\n\tRight here.": "

Some content.

Right here.

", + "Some content.\nRight here.": "

Some content.\nRight here.

", + "Some content.\r\nRight here.": "

Some content.\nRight here.

", + "Some content.\n\tRight here.": "

Some content.\n\tRight here.

", "Some content.\r\n\n \r\n Right here.": "

Some content.

Right here.

", } @@ -25,9 +25,9 @@ func TestPFilter(t *testing.T) { func TestParaFilter(t *testing.T) { tests := map[string]string{ "Some content.": "Some content.", - "Some content.\nRight here.": "Some content.Right here.", - "Some content.\r\nRight here.": "Some content.Right here.", - "Some content.\n\tRight here.": "Some content.Right here.", + "Some content.\nRight here.": "Some content.\nRight here.", + "Some content.\r\nRight here.": "Some content.\nRight here.", + "Some content.\n\tRight here.": "Some content.\n\tRight here.", "Some content.\r\n\n \r\n Right here.": "Some content.Right here.", }