Skip to content

Commit

Permalink
Improve YAML header parsing
Browse files Browse the repository at this point in the history
The previous implementation required yaml doc separator to ignore
everything after the second delimiter.
I believe the YAML standard expects subsequent documents to also be
valid YAML. This change truncates the markdown prior to
trying to parse the YAML header.
  • Loading branch information
jupierce committed Jun 18, 2024
1 parent b9df937 commit fb448e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hack/Dockerfile.markdownlint
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM fedora
FROM fedora:38
WORKDIR /workdir
RUN dnf install -y git golang
COPY install-markdownlint.sh /tmp
Expand Down
10 changes: 9 additions & 1 deletion tools/enhancements/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package enhancements
import (
"fmt"
"net/url"
"strings"

"gopkg.in/yaml.v3"
)
Expand All @@ -21,7 +22,14 @@ type MetaData struct {
func NewMetaData(content []byte) (*MetaData, error) {
result := MetaData{}

err := yaml.Unmarshal(content, &result)
strContent := string(content)
parts := strings.Split(strContent, "---")
if len(parts) < 3 {
return nil, fmt.Errorf("could not extract meta data from header: yaml was not delineated by '---' per the template")
}
yamlContent := strings.TrimSpace(parts[1])
yamlBytes := []byte(yamlContent)
err := yaml.Unmarshal(yamlBytes, &result)
if err != nil {
return nil, fmt.Errorf("could not extract meta data from header: %w", err)
}
Expand Down

0 comments on commit fb448e2

Please sign in to comment.