go-html/cmd/wasm/main.go
Snider c525437ed6
Some checks failed
Security Scan / security (pull_request) Successful in 9s
Test / test (pull_request) Failing after 20s
refactor(module): migrate module path from forge.lthn.ai to dappco.re
Update module path from forge.lthn.ai/core/go-html to dappco.re/go/core/html.
Migrate all .go import paths, update dependency versions (core v0.5.0,
log v0.1.0, io v0.2.0), and add replace directives for local development.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 01:28:30 +00:00

59 lines
1.3 KiB
Go

//go:build js && wasm
package main
import (
"syscall/js"
html "dappco.re/go/core/html"
)
// renderToString builds an HLCRF layout from JS arguments and returns HTML.
// Slot content is injected via Raw() — the caller is responsible for sanitisation.
// This is intentional: the WASM module is a rendering engine for trusted content
// produced server-side or by the application's own templates.
func renderToString(_ js.Value, args []js.Value) any {
if len(args) < 1 {
return ""
}
variant := args[0].String()
ctx := html.NewContext()
if len(args) >= 2 {
ctx.Locale = args[1].String()
}
layout := html.NewLayout(variant)
if len(args) >= 3 && args[2].Type() == js.TypeObject {
slots := args[2]
for _, slot := range []string{"H", "L", "C", "R", "F"} {
content := slots.Get(slot)
if content.Type() == js.TypeString && content.String() != "" {
switch slot {
case "H":
layout.H(html.Raw(content.String()))
case "L":
layout.L(html.Raw(content.String()))
case "C":
layout.C(html.Raw(content.String()))
case "R":
layout.R(html.Raw(content.String()))
case "F":
layout.F(html.Raw(content.String()))
}
}
}
}
return layout.Render(ctx)
}
func main() {
js.Global().Set("gohtml", js.ValueOf(map[string]any{
"renderToString": js.FuncOf(renderToString),
}))
select {}
}