fix: trying to fix error in matching code
This commit is contained in:
@@ -17,14 +17,14 @@ func TestFuzzyFindAll(t *testing.T) {
|
|||||||
Needle string
|
Needle string
|
||||||
Start, End, StartIndex int
|
Start, End, StartIndex int
|
||||||
}{
|
}{
|
||||||
{StartIndex: 0, Needle: "# Ingredients\n", Start: 77, End: 91},
|
{StartIndex: 0, Needle: "## Ingredients\n", Start: 90, End: 105},
|
||||||
{StartIndex: 0, Needle: "# Ingrdients\n", Start: 77, End: 91},
|
{StartIndex: 0, Needle: "## Ingrdients\n", Start: 90, End: 105},
|
||||||
{StartIndex: 0, Needle: "# Inrdients\n", Start: 77, End: 91},
|
{StartIndex: 0, Needle: "## Inrdients\n", Start: 90, End: 105},
|
||||||
{StartIndex: 0, Needle: "---\n", Start: 0, End: 4},
|
{StartIndex: 0, Needle: "---\n", Start: 0, End: 4},
|
||||||
{StartIndex: 4, Needle: "---\n", Start: 29, End: 33},
|
{StartIndex: 4, Needle: "---\n", Start: 43, End: 47},
|
||||||
{StartIndex: 0, Needle: "# Steps\n", Start: 116, End: 124},
|
{StartIndex: 0, Needle: "## Steps\n", Start: 129, End: 138},
|
||||||
{StartIndex: 0, Needle: "# Stps\n", Start: 116, End: 124},
|
{StartIndex: 0, Needle: "## Stps\n", Start: 129, End: 138},
|
||||||
{StartIndex: 0, Needle: "# Step\n", Start: 116, End: 124},
|
{StartIndex: 0, Needle: "## Step\n", Start: 129, End: 138},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -51,6 +51,11 @@ func TestFuzzyBlockMatch(t *testing.T) {
|
|||||||
|
|
||||||
matches := matcher.MatchBlocksFuzzy(string(recipeMd), blocks, 0.3)
|
matches := matcher.MatchBlocksFuzzy(string(recipeMd), blocks, 0.3)
|
||||||
|
|
||||||
|
for _, b := range blocks {
|
||||||
|
fmt.Printf("Block: %+v\n", b)
|
||||||
|
fmt.Printf("Content: '%q'\n\n", b.GetContent())
|
||||||
|
}
|
||||||
|
|
||||||
expected := []struct {
|
expected := []struct {
|
||||||
value string
|
value string
|
||||||
}{
|
}{
|
||||||
@@ -80,7 +85,7 @@ func TestFuzzyBlockMatch(t *testing.T) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
if expected[i].value != m.GetContent() {
|
if expected[i].value != m.GetContent() {
|
||||||
t.Errorf("Match %d did not match expected: %q", i, m.GetContent())
|
t.Errorf("Match %d did not match expected: %q", i, expected[i].value)
|
||||||
}
|
}
|
||||||
fmt.Printf("match: %s->%q\n", m.Block.Path, m.GetContent())
|
fmt.Printf("match: %s->%q\n", m.Block.Path, m.GetContent())
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,9 @@ func DetectType(markdownContent string) (string, error) {
|
|||||||
|
|
||||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3)
|
blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3)
|
||||||
|
|
||||||
|
fmt.Printf("%+v\n", blocks[0])
|
||||||
|
fmt.Printf("Content: '%q'\n", blocks[0].GetContent())
|
||||||
|
|
||||||
result, err := decoders.Parse(blocks)
|
result, err := decoders.Parse(blocks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to parse blocks -> %w", err)
|
return "", fmt.Errorf("failed to parse blocks -> %w", err)
|
||||||
@@ -37,6 +40,7 @@ func DetectType(markdownContent string) (string, error) {
|
|||||||
}
|
}
|
||||||
return "", fmt.Errorf("frontmatter did not contain '_type'")
|
return "", fmt.Errorf("frontmatter did not contain '_type'")
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("could not parse frontmatter")
|
return "", fmt.Errorf("could not parse frontmatter")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -38,7 +38,7 @@
|
|||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-1 flex-col overflow-hidden border-r border-gray-200 last:border-r-0">
|
<div class="relative flex flex-1 flex-col overflow-hidden border-r border-gray-200 last:border-r-0">
|
||||||
<div class="flex items-center border-b border-gray-200 bg-gray-50/50 px-4 py-3">
|
<div class="flex items-center border-b border-gray-200 bg-gray-50/50 px-4 py-3">
|
||||||
{#if status === 'success'}
|
{#if status === 'success'}
|
||||||
<CheckCircleIcon class="mr-2 h-5 w-5 text-green-500" />
|
<CheckCircleIcon class="mr-2 h-5 w-5 text-green-500" />
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<header class="sticky top-0 z-10 border-b border-gray-200 bg-white/80 backdrop-blur-sm">
|
<header class="sticky top-0 z-10 border-b border-gray-200 bg-white/80 backdrop-blur-sm">
|
||||||
<div class="container px-6 py-4">
|
<div class="px-6 py-4">
|
||||||
<div class="flex w-full items-center justify-between">
|
<div class="flex w-full items-center justify-between">
|
||||||
<div class="flex items-center gap-3">
|
<div class="flex items-center gap-3">
|
||||||
<Logo />
|
<Logo />
|
||||||
|
@@ -102,6 +102,7 @@ My favourite baguette recipe
|
|||||||
dataStatus = 'error';
|
dataStatus = 'error';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
templateError = undefined;
|
||||||
jsonOutput = JSON.stringify(result.data, null, 2);
|
jsonOutput = JSON.stringify(result.data, null, 2);
|
||||||
timings = result.timings;
|
timings = result.timings;
|
||||||
templateStatus = 'success';
|
templateStatus = 'success';
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
<img src="/logo.svg" alt="logo" width="100%" />
|
<img src="/logo-2.svg" alt="logo" width="100%" />
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
img {
|
img {
|
||||||
|
1
playground/src/routes/+layout.ts
Normal file
1
playground/src/routes/+layout.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export const prerender = true;
|
3
playground/static/logo-2.svg
Normal file
3
playground/static/logo-2.svg
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<svg width="202" height="202" viewBox="0 0 202 202" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M198 101C198 47.4284 154.572 4 101 4C63.6769 4 31.2771 25.0794 15.0566 55.9824C31.5802 36.8304 58.3585 25 87.5 25C107.135 25 125.868 30.5962 141.081 40.3643C163.17 54.5475 177.915 77.5995 176.999 105.066C176.961 122.231 171.229 138.064 161.595 150.771L159.861 153.057L133.999 112.606L100.778 164.568L65.0068 108.618L42.0176 144.873C57.3037 166.504 84.4388 181 114.5 181C149.395 181 180.331 159.267 193.398 130.6C196.385 121.268 198 111.323 198 101ZM29.0127 106.222C29.2632 118.862 33.0866 130.771 39.5967 141.223L64.9932 101.171L100.778 157.143L134 105.182L160.106 146.016C168.233 134.357 173 120.186 173 104.895H173.002C173.825 79.7876 160.762 58.4662 140.614 44.8486L101 108.688L61.3604 44.7793C41.8587 57.6651 29 79.7788 29 104.895L29.0127 106.222ZM202 101C202 156.781 156.781 202 101 202V198C134.1 198 163.327 181.42 180.831 156.113C164.258 173.559 140.379 185 114.5 185C82.6725 185 53.8784 169.41 37.9658 146.05C30.0365 134.409 25.3017 120.829 25.0137 106.303L25 104.895C25 77.6227 39.3659 53.7077 60.9336 40.3018L62.6338 39.2441L101 101.101L137.249 42.6855C123.002 33.9863 105.671 29 87.5 29C51.3264 29 19.6837 47.7188 7.41992 75.377C5.19019 83.5392 4 92.1306 4 101C4 154.572 47.4284 198 101 198V202C45.2192 202 0 156.781 0 101C0 45.2192 45.2192 0 101 0C156.781 0 202 45.2192 202 101Z" fill="#555"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
@@ -65,5 +65,5 @@ func ParseTemplateBlock(template Slice, blockType BlockType) (block Block, err e
|
|||||||
return parseYamlTemplate(template)
|
return parseYamlTemplate(template)
|
||||||
}
|
}
|
||||||
|
|
||||||
return block, NewErrorf("invalid template").WithPosition(template.start, template.end)
|
return block, NewErrorf("invalid template: '%s'", template.String()).WithPosition(template.start, template.end)
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// CompileTemplate scans once, emitting:
|
// CompileTemplate scans once, emitting:
|
||||||
// - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}"
|
// - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}"
|
||||||
// - matching blocks: gaps between data blocks (excluding the brace lines themselves)
|
// - matching blocks: gaps between data blocks (excluding the brace lines themselves)
|
||||||
@@ -33,6 +35,7 @@ func CompileTemplate(templateSource string) ([]Block, error) {
|
|||||||
|
|
||||||
if curlyIndex == 0 && nextCurlyIndex == 1 {
|
if curlyIndex == 0 && nextCurlyIndex == 1 {
|
||||||
if i > start {
|
if i > start {
|
||||||
|
fmt.Printf("BLockContent 1: %q\n", template.Slice(start, i).String())
|
||||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
|
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
|
||||||
@@ -42,6 +45,7 @@ func CompileTemplate(templateSource string) ([]Block, error) {
|
|||||||
start = i
|
start = i
|
||||||
blockType = DataBlock
|
blockType = DataBlock
|
||||||
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
|
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
|
||||||
|
fmt.Printf("BLockContent 2: %q\n", template.Slice(start, i).String())
|
||||||
if i > start {
|
if i > start {
|
||||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -3,6 +3,8 @@ package template_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.max-richter.dev/max/marka/registry"
|
"git.max-richter.dev/max/marka/registry"
|
||||||
"git.max-richter.dev/max/marka/template"
|
"git.max-richter.dev/max/marka/template"
|
||||||
)
|
)
|
||||||
@@ -10,13 +12,13 @@ import (
|
|||||||
func TestExtractBlocks(t *testing.T) {
|
func TestExtractBlocks(t *testing.T) {
|
||||||
src, err := registry.GetTemplate("Recipe")
|
src, err := registry.GetTemplate("Recipe")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to extract blocks: %s", err.Error())
|
t.Errorf("failed to load template 'Recipe' -> %s", err.Error())
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
templateBlocks, err := template.CompileTemplate(src)
|
templateBlocks, err := template.CompileTemplate(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to extract blocks: %s", err.Error())
|
t.Errorf("failed to compile template -> %s", err.Error())
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +60,8 @@ func TestExtractBlocks(t *testing.T) {
|
|||||||
{Type: template.MatchingBlock},
|
{Type: template.MatchingBlock},
|
||||||
{Type: template.DataBlock, Path: "name", Codec: "text"},
|
{Type: template.DataBlock, Path: "name", Codec: "text"},
|
||||||
{Type: template.MatchingBlock},
|
{Type: template.MatchingBlock},
|
||||||
|
{Type: template.DataBlock, Path: "keywords", Codec: "hashtags", Optional: true},
|
||||||
|
{Type: template.MatchingBlock},
|
||||||
{Type: template.DataBlock, Path: "description", Codec: "text"},
|
{Type: template.DataBlock, Path: "description", Codec: "text"},
|
||||||
{Type: template.MatchingBlock},
|
{Type: template.MatchingBlock},
|
||||||
{Type: template.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
|
{Type: template.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
|
||||||
@@ -69,6 +73,9 @@ func TestExtractBlocks(t *testing.T) {
|
|||||||
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
|
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%+v\n", templateBlocks[0]);
|
||||||
|
fmt.Printf("Content: %q\n", templateBlocks[0].GetContent());
|
||||||
|
|
||||||
for i, b := range templateBlocks {
|
for i, b := range templateBlocks {
|
||||||
exp := expected[i]
|
exp := expected[i]
|
||||||
if b.Type != exp.Type {
|
if b.Type != exp.Type {
|
||||||
|
Reference in New Issue
Block a user