Parse titles from pages

This commit is contained in:
adnano 2020-09-22 17:18:50 -04:00
parent 40ae714d6d
commit 81bf4f8e9b

View file

@ -3,6 +3,7 @@ package main
import (
"log"
"path/filepath"
"regexp"
"time"
)
@ -46,6 +47,8 @@ type Page struct {
Content string
}
var titleRE = regexp.MustCompile("^# ?([^#\r\n]+)")
func NewPage(path string, content string) *Page {
page := &Page{
Path: path,
@ -63,6 +66,11 @@ func NewPage(path string, content string) *Page {
}
}
// Try to parse the title from the contents
if submatches := titleRE.FindStringSubmatch(content); submatches != nil {
page.Title = submatches[1]
}
return page
}