Add posts.ExtractTitle func

This commit is contained in:
Matt Baer
2018-10-10 15:16:54 -04:00
parent 73c7314873
commit 355476e321
2 changed files with 54 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
package posts
import (
"strings"
)
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
}