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 ( import (
"log" "log"
"path/filepath" "path/filepath"
"regexp"
"time" "time"
) )
@ -46,6 +47,8 @@ type Page struct {
Content string Content string
} }
var titleRE = regexp.MustCompile("^# ?([^#\r\n]+)")
func NewPage(path string, content string) *Page { func NewPage(path string, content string) *Page {
page := &Page{ page := &Page{
Path: path, 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 return page
} }