diff --git a/parser/decoders/yaml.go b/parser/decoders/yaml.go index 9a544e5..26ef78f 100644 --- a/parser/decoders/yaml.go +++ b/parser/decoders/yaml.go @@ -33,6 +33,13 @@ func Yaml(input string, block template.Block) (value any, error error) { continue } + for _, alias := range f.PathAliases { + if value, ok := res[alias]; ok { + out = utils.SetPathValue(f.Path, value, out) + continue + } + } + if value, ok := renderUtils.GetValueFromPath(res, f.Path); ok { out = utils.SetPathValue(f.Path, value, out) continue diff --git a/playground/src/lib/components/Playground.svelte b/playground/src/lib/components/Playground.svelte index 3a25338..f4b1772 100644 --- a/playground/src/lib/components/Playground.svelte +++ b/playground/src/lib/components/Playground.svelte @@ -87,7 +87,8 @@ My favourite baguette recipe return; } try { - compileTemplate(templateValue); + const compiledTemplate = compileTemplate(templateValue); + console.log({ compiledTemplate, templateValue }); const result = templateValue ? parseMarkdownWithTemplate(markdownValue, templateValue) diff --git a/playground/static/main.wasm b/playground/static/main.wasm index 580ea6b..4bcbea9 100644 Binary files a/playground/static/main.wasm and b/playground/static/main.wasm differ diff --git a/registry/templates/Article.marka b/registry/templates/Article.marka index e2f8ac0..b0c5d5c 100644 --- a/registry/templates/Article.marka +++ b/registry/templates/Article.marka @@ -14,6 +14,7 @@ - path: about - path: url - path: author.name + pathAlias: author - path: author._type codec: const value: Person @@ -21,11 +22,14 @@ - path: datePublished - path: articleSection - path: reviewRating.ratingValue - pathAlias: rating + pathAlias: + - rating - path: reviewRating.bestRating + value: 5 codec: const hidden: true - path: reviewRating.worstRating + value: 1 codec: const hidden: true } diff --git a/registry/templates/Recipe.marka b/registry/templates/Recipe.marka index 0bf9abb..f26b27f 100644 --- a/registry/templates/Recipe.marka +++ b/registry/templates/Recipe.marka @@ -17,8 +17,12 @@ hidden: true value: Person - path: author.name + pathAlias: + - author - path: author.email - path: datePublished + pathAlias: + - date - path: prepTime - path: cookTime - path: recipeYield diff --git a/registry/templates/Review.marka b/registry/templates/Review.marka index 6c57232..ed71eb3 100644 --- a/registry/templates/Review.marka +++ b/registry/templates/Review.marka @@ -13,13 +13,17 @@ - path: tmdbId - path: image - path: author.name + pathAlias: author - path: author._type codec: const value: Person hidden: true - path: datePublished - - path: itemReviewed.name + pathAlias: + - date - path: reviewRating.ratingValue + pathAlias: + - rating - path: reviewRating.bestRating codec: const value: 5 diff --git a/server/.air.toml b/server/.air.toml index b2eced4..7ce063e 100644 --- a/server/.air.toml +++ b/server/.air.toml @@ -9,11 +9,13 @@ cmd = "go build -o ./tmp/marka-server ./cmd/marka-server" bin = "./tmp/marka-server" # Command to run the application with arguments. -full_bin = "./tmp/marka-server -root=../examples -addr=:8080" +full_bin = "./tmp/marka-server -root=/home/max/Notes/resources -addr=:8080 -playground-root=./playground" # Watch these file extensions. include_ext = ["go", "http"] +include_dir = ["../registry","../renderer","../template"] + # Ignore these directories. exclude_dir = ["tmp"] diff --git a/template/blocks_yaml.go b/template/blocks_yaml.go index a4a9db9..31508b6 100644 --- a/template/blocks_yaml.go +++ b/template/blocks_yaml.go @@ -18,11 +18,11 @@ type yamlBlock struct { } type yamlField struct { - Path string `yaml:"path"` - Value any `yaml:"value,omitempty"` - Codec string `yaml:"codec"` - Hidden bool `yaml:"hidden,omitempty"` - PathAlias string `yaml:"pathAlias,omitempty"` + Path string `yaml:"path"` + Value any `yaml:"value,omitempty"` + Codec string `yaml:"codec"` + Hidden bool `yaml:"hidden,omitempty"` + PathAlias []string `yaml:"pathAlias,omitempty"` } func parseYamlTemplate(input Slice) (block Block, err error) { @@ -53,6 +53,7 @@ func parseYamlTemplate(input Slice) (block Block, err error) { var fields []BlockField for _, field := range blk.Fields { + if field.Path == "" { return block, fmt.Errorf("failed to parse field: %v", field) } @@ -66,18 +67,22 @@ func parseYamlTemplate(input Slice) (block Block, err error) { return block, fmt.Errorf("failed to parse codec -> %w", err) } - fields = append(fields, BlockField{ - Path: field.Path, - CodecType: fieldCodec, - Value: field.Value, - Hidden: field.Hidden, - }) + block := BlockField{ + Path: field.Path, + PathAliases: field.PathAlias, + CodecType: fieldCodec, + Value: field.Value, + Hidden: field.Hidden, + } + + fields = append(fields, block) } return Block{ Type: DataBlock, Path: blk.Path, + PathAliases: blk.PathAlias, Codec: codec, Fields: fields, ListTemplate: blk.ListTemplate, diff --git a/template/compile.go b/template/compile.go index d82794d..c0882a4 100644 --- a/template/compile.go +++ b/template/compile.go @@ -1,6 +1,8 @@ package template -import "strings" +import ( + "strings" +) // CompileTemplate scans once, emitting: // - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}" diff --git a/template/structs.go b/template/structs.go index ff8d6be..5f741c2 100644 --- a/template/structs.go +++ b/template/structs.go @@ -8,15 +8,17 @@ const ( ) type BlockField struct { - Path string - CodecType CodecType - Value any - Hidden bool + Path string + PathAliases []string + CodecType CodecType + Value any + Hidden bool } type Block struct { Type BlockType Path string + PathAliases []string Codec CodecType ListTemplate string Fields []BlockField