From fb448e2c3cf351ce4083204dde0b99d0cd028f33 Mon Sep 17 00:00:00 2001 From: Justin Pierce Date: Tue, 18 Jun 2024 11:20:20 -0400 Subject: [PATCH] Improve YAML header parsing 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. --- hack/Dockerfile.markdownlint | 2 +- tools/enhancements/metadata.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/hack/Dockerfile.markdownlint b/hack/Dockerfile.markdownlint index 5ad62848d0..d511fdf66d 100644 --- a/hack/Dockerfile.markdownlint +++ b/hack/Dockerfile.markdownlint @@ -1,4 +1,4 @@ -FROM fedora +FROM fedora:38 WORKDIR /workdir RUN dnf install -y git golang COPY install-markdownlint.sh /tmp diff --git a/tools/enhancements/metadata.go b/tools/enhancements/metadata.go index ee29f98237..8e94fb650e 100644 --- a/tools/enhancements/metadata.go +++ b/tools/enhancements/metadata.go @@ -3,6 +3,7 @@ package enhancements import ( "fmt" "net/url" + "strings" "gopkg.in/yaml.v3" ) @@ -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) }