teach mdproc hugo to parse out title
This commit is contained in:
parent
074515be5d
commit
8acd0f6489
1 changed files with 8 additions and 0 deletions
|
@ -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'))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue