diff options
| author | 2026-04-15 16:33:08 +0700 | |
|---|---|---|
| committer | 2026-04-15 16:33:08 +0700 | |
| commit | 42bf9d8e7f459f9021fb6fb389d3b51fed5edc35 (patch) | |
| tree | 852cbd77ee6a6c6818caa9ba1d2033e1b21fccf7 /internal/frontmatter | |
| parent | ab557dd4df732c5d461bf7015f9eb0e761c982b7 (diff) | |
| download | web-42bf9d8e7f459f9021fb6fb389d3b51fed5edc35.tar.gz web-42bf9d8e7f459f9021fb6fb389d3b51fed5edc35.zip | |
refactor: simpler frontmatter parser (replace regexp with bytes pkg)
Diffstat (limited to 'internal/frontmatter')
| -rw-r--r-- | internal/frontmatter/parser.go | 14 |
1 files changed, 8 insertions, 6 deletions
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 { |