From 42bf9d8e7f459f9021fb6fb389d3b51fed5edc35 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Wed, 15 Apr 2026 16:33:08 +0700 Subject: refactor: simpler frontmatter parser (replace regexp with bytes pkg) --- internal/frontmatter/parser.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'internal/frontmatter/parser.go') diff --git a/internal/frontmatter/parser.go b/internal/frontmatter/parser.go index 6062113..942760c 100644 --- a/internal/frontmatter/parser.go +++ b/internal/frontmatter/parser.go @@ -1,8 +1,8 @@ package frontmatter import ( + "bytes" "fmt" - "regexp" "time" "gopkg.in/yaml.v3" @@ -22,15 +22,17 @@ type Metadata struct { } func ParseFrontmatter(content []byte) (metadata *Metadata, markdown []byte, err error) { - frontmatterRegex := regexp.MustCompile(`^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n([\s\S]*)$`) - matches := frontmatterRegex.FindSubmatch(content) + if !bytes.HasPrefix(content, []byte("---\n")) { + return nil, content, nil + } - if len(matches) != 3 { + end := bytes.Index(content[4:], []byte("\n---\n")) + if end == -1 { return nil, content, nil } - yamlContent := matches[1] - markdownContent := matches[2] + yamlContent := content[4 : end+4] + markdownContent := content[end+9:] metadata = &Metadata{} if err := yaml.Unmarshal([]byte(yamlContent), &metadata); err != nil { -- cgit v1.3.1+13