wip
This commit is contained in:
21
playground/wasm/build.sh
Executable file
21
playground/wasm/build.sh
Executable file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
OUT_DIR="$SCRIPT_DIR/../static"
|
||||
OUT_WASM="$OUT_DIR/main.wasm"
|
||||
|
||||
mkdir -p "$OUT_DIR"
|
||||
|
||||
tinygo build -target=wasm -opt=z -no-debug -panic=trap -gc=leaking \
|
||||
-o "$OUT_WASM" "$SCRIPT_DIR"
|
||||
|
||||
# Optional post-process (run only if tools exist)
|
||||
# command -v wasm-opt >/dev/null && wasm-opt -Oz --strip-debug --strip-dwarf --strip-producers \
|
||||
# -o "$OUT_WASM.tmp" "$OUT_WASM" && mv "$OUT_WASM.tmp" "$OUT_WASM"
|
||||
# command -v wasm-strip >/dev/null && wasm-strip "$OUT_WASM"
|
||||
# command -v brotli >/dev/null && brotli -f -q 11 "$OUT_WASM" -o "$OUT_WASM.br"
|
||||
# command -v gzip >/dev/null && gzip -c -9 "$OUT_WASM" > "$OUT_WASM.gz"
|
||||
|
||||
# Copy TinyGo runtime
|
||||
cp -f "$(tinygo env TINYGOROOT)/targets/wasm_exec.js" "$OUT_DIR/wasm_exec.js"
|
3
playground/wasm/go.mod
Normal file
3
playground/wasm/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module git.max-richter.dev/max/marka/playground-wasm
|
||||
|
||||
go 1.24.7
|
59
playground/wasm/main.go
Normal file
59
playground/wasm/main.go
Normal 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 {}
|
||||
}
|
||||
|
Reference in New Issue
Block a user