md2gmi/mdproc/hugo.go

37 lines
802 B
Go
Raw Normal View History

2021-07-05 08:36:20 +03:00
package mdproc
import (
"bytes"
2021-07-05 17:11:40 +03:00
"fmt"
2021-07-05 08:36:20 +03:00
"regexp"
"gitrepo.ru/neonxp/md2gmi/pipe"
2021-07-05 08:36:20 +03:00
)
func RemoveFrontMatter(in chan pipe.StreamItem) chan pipe.StreamItem {
out := make(chan pipe.StreamItem)
go func() {
2021-07-05 17:11:40 +03:00
// delete the entire front matter
2021-07-05 08:36:20 +03:00
re := regexp.MustCompile(`---.*---`)
2021-07-05 17:11:40 +03:00
// but parse out the title as we want to reinject it
re2 := regexp.MustCompile(`title:[ "]*([а-яА-Яa-zA-Z0-9 :!'@#$%^&*)(]+)["]*`)
2021-07-05 08:36:20 +03:00
for b := range in {
data := b.Payload()
for _, match := range re.FindAllSubmatch(data, -1) {
data = bytes.Replace(data, match[0], []byte(""), 1)
2021-07-05 17:11:40 +03:00
for _, title := range re2.FindAllSubmatch(match[0], 1) {
2021-07-05 17:43:46 +03:00
// add title
2021-07-06 13:45:15 +03:00
data = []byte(fmt.Sprintf("# %s\n\n", title[1]))
2021-07-05 17:11:40 +03:00
}
2021-07-05 08:36:20 +03:00
}
2021-07-05 17:43:46 +03:00
out <- pipe.NewItem(b.Index(), data)
2021-07-05 08:36:20 +03:00
}
close(out)
}()
return out
}