60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
//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 {}
|
|
}
|
|
|