htmx + Go
htmx
Post the editor like any form field and swap in the server’s response.
Current Markdown source
Integration code
<form method="post" action="/api/htmx/save"
hx-post="/api/htmx/save" hx-target="#htmx-output" hx-swap="innerHTML">
<label class="editor-label" for="draft">Markdown</label>
<tint-editor id="draft" name="body" required rows="6" value="# htmx
Tint submits with the rest of the form."></tint-editor>
<div class="row">
<button type="submit">Save</button>
<button type="reset">Reset</button>
</div>
</form>
<div id="htmx-output" class="result" role="status"></div>Server
The Go handler behind it, straight from cmd/site/integrations.go:
func saveHtmx(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 256<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte(markdownResult(r.PostForm.Get("body"))))
}
func markdownResult(markdown string) string {
return `<p>Markdown received.</p><pre class="output"><code>` + html.EscapeString(markdown) + `</code></pre>`
}The editor runs the code shown in the Code tab, with the setup from the guide. See generic examples for framework-free snippets.