2018-10-10 15:16:54 -04:00
|
|
|
package posts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2019-05-30 10:12:34 -04:00
|
|
|
// ExtractTitle takes the given raw post text and returns a title, if explicitly
|
|
|
|
|
// provided, and a body.
|
2018-10-10 15:16:54 -04:00
|
|
|
func ExtractTitle(content string) (title string, body string) {
|
|
|
|
|
if hashIndex := strings.Index(content, "# "); hashIndex == 0 {
|
|
|
|
|
eol := strings.IndexRune(content, '\n')
|
|
|
|
|
// First line should start with # and end with \n
|
|
|
|
|
if eol != -1 {
|
|
|
|
|
body = strings.TrimLeft(content[eol:], " \t\n\r")
|
|
|
|
|
title = content[len("# "):eol]
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
body = content
|
|
|
|
|
return
|
|
|
|
|
}
|