Skip to content

Commit

Permalink
Fix frontmatter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Mar 6, 2021
1 parent 6551115 commit 7fd04ad
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions pkg/mdformatter/mdformatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"sort"
"strings"

"github.com/Kunde21/markdownfmt/v2/markdown"
"github.com/bwplotka/mdox/pkg/gitdiff"
Expand Down Expand Up @@ -109,11 +110,31 @@ func (FormatFrontMatter) TransformFrontMatter(_ SourceContext, frontMatter map[s
}
sort.Sort(sort.Reverse(sort.StringSlice(keys)))

b := bytes.NewBuffer([]byte("---\n"))
b := bytes.NewBuffer([]byte("---"))
for _, k := range keys {
_, _ = fmt.Fprintf(b, "%v: %v\n", k, frontMatter[k])
// check if frontMatter[k] is a map
frontMatterStr := fmt.Sprintf("%s", frontMatter[k])
isMap := strings.HasPrefix(frontMatterStr, "map[")

// split string to get all key-value pairs
if isMap {
_, _ = fmt.Fprintf(b, "\n%v:", k)
pairs := strings.Split(frontMatterStr[4:len(frontMatterStr)-1], " ")
pair := make([]string, 2)
// ignore spaces in values
for _, val := range pairs {
if strings.Contains(val, ":") {
pair = strings.Split(val, ":")
_, _ = fmt.Fprintf(b, "\n %v: %v", pair[0], pair[1])
} else {
_, _ = fmt.Fprintf(b, " %v", val)
}
}
} else {
_, _ = fmt.Fprintf(b, "\n%v: %v", k, frontMatter[k])
}
}
_, _ = b.Write([]byte("---\n\n"))
_, _ = b.Write([]byte("\n---\n\n"))
return b.Bytes(), nil
}

Expand Down

0 comments on commit 7fd04ad

Please sign in to comment.