teach mdproc hugo to parse out title

This commit is contained in:
dre 2021-07-05 22:11:40 +08:00
parent 074515be5d
commit 8acd0f6489

View file

@ -2,6 +2,7 @@ package mdproc
import (
"bytes"
"fmt"
"regexp"
"github.com/n0x1m/md2gmi/pipe"
@ -11,12 +12,19 @@ func RemoveFrontMatter(in chan pipe.StreamItem) chan pipe.StreamItem {
out := make(chan pipe.StreamItem)
go func() {
// delete the entire front matter
re := regexp.MustCompile(`---.*---`)
// but parse out the title as we want to reinject it
re2 := regexp.MustCompile(`title:[ "]*([a-zA-Z0-9 :!@#$%^&*)(]+)["]*`)
for b := range in {
data := b.Payload()
for _, match := range re.FindAllSubmatch(data, -1) {
data = bytes.Replace(data, match[0], []byte(""), 1)
for _, title := range re2.FindAllSubmatch(match[0], 1) {
// prefix title
data = append([]byte(fmt.Sprintf("# %s\n\n", title[1])), data...)
}
}
out <- pipe.NewItem(b.Index(), append(bytes.TrimSpace(data), '\n'))
}