Skip to content
quarry Artifact [dab7534afe]
Login

Artifact [dab7534afe]

Artifact dab7534afe29bdace46dab6ffe40c897bd8f67160531aea3206c40a6d0e7adb9:


package markup

import (
	"bytes"
	"compress/flate"
	"encoding/base64"
)

// StudioURL is pikchr-studio, where a diagram's "Edit in pikchr-studio"
// link opens it. Empty turns the link off.
var StudioURL = "https://pikchr-studio.daniel-mestas.workers.dev/"

// maxStudioLink bounds the link. The source rides in the URL, and past
// a few kilobytes browsers, proxies and chat clients start cutting
// URLs; a diagram that large keeps Copy and loses the link.
const maxStudioLink = 8000

// StudioLink is the address that opens src in pikchr-studio, or "" when
// there is no studio or the source is too large to carry.
//
// The source travels in the fragment, as z=<base64url of raw DEFLATE of
// the UTF-8 source>, no padding. A browser never sends the fragment to
// a server, so a diagram from a private repository reaches the reader's
// own browser and nothing on the studio's side: not its host, not its
// logs. The studio decodes it (DecompressionStream "deflate-raw") and
// loads it into the editor.
func StudioLink(src []byte) string {
	if StudioURL == "" || len(bytes.TrimSpace(src)) == 0 {
		return ""
	}
	var buf bytes.Buffer
	w, err := flate.NewWriter(&buf, flate.BestCompression)
	if err != nil {
		return ""
	}
	if _, err := w.Write(src); err != nil {
		return ""
	}
	if err := w.Close(); err != nil {
		return ""
	}
	link := StudioURL + "#z=" + base64.RawURLEncoding.EncodeToString(buf.Bytes())
	if len(link) > maxStudioLink {
		return ""
	}
	return link
}