mirror of https://github.com/mickael-menu/zk
Parse notes title and body
parent
6dc9a65d21
commit
b78609269b
@ -0,0 +1,26 @@
|
||||
package note
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Content struct {
|
||||
Title string
|
||||
Body string
|
||||
}
|
||||
|
||||
var contentRegex = regexp.MustCompile(`(?m)^#\s+(.+?)\s*$`)
|
||||
|
||||
func Parse(content string) Content {
|
||||
var res Content
|
||||
|
||||
if match := contentRegex.FindStringSubmatchIndex(content); len(match) >= 4 {
|
||||
res.Title = content[match[2]:match[3]]
|
||||
res.Body = strings.TrimSpace(content[match[3]:])
|
||||
} else {
|
||||
res.Body = strings.TrimSpace(content)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue