This commit is contained in:
Max Richter
2025-09-25 16:41:26 +02:00
parent 3f0d25f935
commit b13d5015f4
75 changed files with 3881 additions and 141 deletions

59
playground/wasm/main.go Normal file
View File

@@ -0,0 +1,59 @@
//go:build js && wasm
package main
import (
"encoding/json"
"syscall/js"
p "git.max-richter.dev/max/marka/parser"
)
func matchBlocks(_ js.Value, args []js.Value) any {
if len(args) == 0 {
return js.ValueOf(map[string]any{"error": "missing markdown"})
}
t, err := p.MatchBlocks(args[0].String())
if err != nil {
return js.ValueOf(map[string]any{"error": err.Error()})
}
jsonString,_ := json.Marshal(t)
return js.ValueOf(string(jsonString)) // plain string
}
func detectType(_ js.Value, args []js.Value) any {
if len(args) == 0 {
return js.ValueOf(map[string]any{"error": "missing markdown"})
}
t, err := p.DetectType(args[0].String())
if err != nil {
return js.ValueOf(map[string]any{"error": err.Error()})
}
return js.ValueOf(t) // plain string
}
func parseFile(_ js.Value, args []js.Value) any {
if len(args) == 0 {
return js.ValueOf(map[string]any{"error": "missing markdown"})
}
res, err := p.ParseFile(args[0].String())
if err != nil {
return js.ValueOf(map[string]any{"error": err.Error()})
}
b, err := json.Marshal(res) // return JSON string to avoid reflect-heavy bridging
if err != nil {
return js.ValueOf(map[string]any{"error": err.Error()})
}
return js.ValueOf(string(b))
}
func main() {
js.Global().Set("markaDetectType", js.FuncOf(detectType))
js.Global().Set("markaParseFile", js.FuncOf(parseFile))
js.Global().Set("markaMatchBlocks", js.FuncOf(matchBlocks))
select {}
}