From f9c4b7bc05cd905b07a5b897e727e9dfc7a9eb8c Mon Sep 17 00:00:00 2001 From: Max Richter Date: Tue, 10 Feb 2026 18:24:39 +0100 Subject: [PATCH] feat: some stuff? --- parser/decoders/yaml.go | 11 +++++++++-- parser/matcher/matcher.go | 3 ++- parser/matcher/matcher_test.go | 4 ++-- parser/parser.go | 6 ------ registry/schema-org/Recipe.schema.json | 6 ++++++ registry/templates/Review.marka | 1 + server/cmd/marka-server/main.go | 11 ++++++----- 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/parser/decoders/yaml.go b/parser/decoders/yaml.go index 26ef78f..28ee005 100644 --- a/parser/decoders/yaml.go +++ b/parser/decoders/yaml.go @@ -16,7 +16,7 @@ func Yaml(input string, block template.Block) (value any, error error) { return nil, fmt.Errorf("failed to parse yaml '%q': %w", input, err) } - var out any + var out any // Keep out as any for _, f := range block.Fields { if f.Path == "@schema" { @@ -25,7 +25,14 @@ func Yaml(input string, block template.Block) (value any, error error) { if f.CodecType == template.CodecConst { if f.Value != nil { - out = utils.SetPathValue(f.Path, f.Value, out) + // Only set const value if 'out' is a map and the path doesn't exist + if outMap, ok := out.(map[string]any); ok { + if _, exists := renderUtils.GetValueFromPath(outMap, f.Path); !exists { + out = utils.SetPathValue(f.Path, f.Value, out) + } + } else if out == nil { // If out is nil, it's the first value, so set it + out = utils.SetPathValue(f.Path, f.Value, out) + } } } else { if value, ok := res[f.Path]; ok { diff --git a/parser/matcher/matcher.go b/parser/matcher/matcher.go index b6d6f37..1e8c8a9 100644 --- a/parser/matcher/matcher.go +++ b/parser/matcher/matcher.go @@ -3,6 +3,7 @@ package matcher import ( "math" + "strings" "git.max-richter.dev/max/marka/parser/utils" "git.max-richter.dev/max/marka/template" @@ -20,7 +21,7 @@ func (m Block) GetContent() string { if m.src == nil || m.Start < 0 || m.End > len(*m.src) || m.Start > m.End { return "" } - return (*m.src)[m.Start:m.End] + return strings.TrimSpace((*m.src)[m.Start:m.End]) } // MatchBlocksFuzzy finds anchor positions for all BlockMatching blocks using diff --git a/parser/matcher/matcher_test.go b/parser/matcher/matcher_test.go index eb2ba4e..bf4d24f 100644 --- a/parser/matcher/matcher_test.go +++ b/parser/matcher/matcher_test.go @@ -72,7 +72,7 @@ func TestMatch_FuzzyBlockBaguette(t *testing.T) { value: "- Flour\n- Water\n- Salt", }, { - value: "1. Mix Flour Water and Salt\n2. Bake the bread\n", + value: "1. Mix Flour Water and Salt\n2. Bake the bread", }, } @@ -118,7 +118,7 @@ func TestMatch_FuzzyBlockSalad(t *testing.T) { value: "- 100 g lettuce\n- 5 cherry tomatoes\n- 1 tbsp olive oil\n- Pinch of salt", }, { - value: "1. Wash and dry the lettuce.\n2. Halve the cherry tomatoes.\n3. Toss with olive oil and salt.\n", + value: "1. Wash and dry the lettuce.\n2. Halve the cherry tomatoes.\n3. Toss with olive oil and salt.", }, } diff --git a/parser/parser.go b/parser/parser.go index ebb4e25..d95674d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -4,7 +4,6 @@ package parser import ( "fmt" - "strings" "git.max-richter.dev/max/marka/parser/decoders" "git.max-richter.dev/max/marka/parser/matcher" @@ -69,11 +68,6 @@ func ParseFileWithTemplate(markdownContent string, templateContent string) (any, return nil, fmt.Errorf("failed to compile template -> %w", err) } - for strings.HasSuffix(markdownContent, "\n") { - markdownContent = strings.TrimSuffix(markdownContent, "\n") - } - markdownContent = markdownContent + "\n" - blocks := matcher.MatchBlocksFuzzy(markdownContent, tpl, 0.3) result, err := decoders.Parse(blocks) diff --git a/registry/schema-org/Recipe.schema.json b/registry/schema-org/Recipe.schema.json index 0c1668a..5041ab2 100644 --- a/registry/schema-org/Recipe.schema.json +++ b/registry/schema-org/Recipe.schema.json @@ -66,6 +66,12 @@ { "type": "string" }, + { + "type": "array", + "items": { + "type": "string" + } + }, { "$ref": "schema:CreativeWork" }, diff --git a/registry/templates/Review.marka b/registry/templates/Review.marka index 6d19899..fe843fe 100644 --- a/registry/templates/Review.marka +++ b/registry/templates/Review.marka @@ -11,6 +11,7 @@ codec: const value: Review - path: tmdbId + - path: isbn - path: link pathAlias: link - path: image diff --git a/server/cmd/marka-server/main.go b/server/cmd/marka-server/main.go index 85267f0..88715b2 100644 --- a/server/cmd/marka-server/main.go +++ b/server/cmd/marka-server/main.go @@ -35,12 +35,13 @@ func main() { absPlaygroundRoot, err := filepath.Abs(*playgroundRoot) must(err) info, err := os.Stat(absPlaygroundRoot) - must(err) - if !info.IsDir() { - log.Fatalf("playground-root %s is not a directory", *playgroundRoot) + if err == nil { + if !info.IsDir() { + log.Fatalf("playground-root %s is not a directory", *playgroundRoot) + } + log.Printf("serving playground from %s", absPlaygroundRoot) + http.Handle("/_playground/", http.StripPrefix("/_playground/", http.FileServer(http.Dir(absPlaygroundRoot)))) } - log.Printf("serving playground from %s", absPlaygroundRoot) - http.Handle("/_playground/", http.StripPrefix("/_playground/", http.FileServer(http.Dir(absPlaygroundRoot)))) } if len(roots) == 0 {