36 lines
777 B
Go
36 lines
777 B
Go
|
|
package threadspost
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// FormatDraftText normalizes generated post bodies without destroying the author's voice.
|
||
|
|
func FormatDraftText(text string) string {
|
||
|
|
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
||
|
|
text = strings.TrimSpace(text)
|
||
|
|
if text == "" {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
|
||
|
|
lines := strings.Split(text, "\n")
|
||
|
|
out := make([]string, 0, len(lines))
|
||
|
|
blankCount := 0
|
||
|
|
for _, line := range lines {
|
||
|
|
line = strings.TrimSpace(line)
|
||
|
|
if line == "" {
|
||
|
|
blankCount++
|
||
|
|
if blankCount <= 1 && len(out) > 0 {
|
||
|
|
out = append(out, "")
|
||
|
|
}
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
blankCount = 0
|
||
|
|
out = append(out, line)
|
||
|
|
}
|
||
|
|
for len(out) > 0 && out[len(out)-1] == "" {
|
||
|
|
out = out[:len(out)-1]
|
||
|
|
}
|
||
|
|
return ClampPublish(strings.Join(out, "\n"))
|
||
|
|
}
|