a73x

internal/shape/render.go

Ref:   Size: 826 B   History

package shape

import (
	_ "embed"
	"encoding/json"
	"strings"
)

//go:embed viewer.html
var viewerTemplate string

// dataMarker is the placeholder in viewer.html replaced with the JSON model.
const dataMarker = "/*SHAPE_DATA*/"

// RenderJSON marshals the model to pretty-printed, deterministic JSON with a
// trailing newline. The model contains only sorted slices (no maps), so output
// is byte-stable across runs.
func RenderJSON(m Model) ([]byte, error) {
	b, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return nil, err
	}
	return append(b, '\n'), nil
}

// RenderHTML inlines the exact JSON bytes into the static viewer template, so
// the HTML cannot drift independently of the JSON.
func RenderHTML(jsonBytes []byte) string {
	return strings.Replace(viewerTemplate, dataMarker, string(jsonBytes), 1)
}