Files
notas-online/internal/api/render.go
T
lgcosta 9bc5de4c56 feat: aplicação inicial — gerenciador de notas web estilo Mousepad
- Backend Go: API REST, auth por token (Bearer + cookie HttpOnly), storage em disco
  com revisões content-addressed (estilo Git: hash, autor, data, diff, restauração)
- Editor txt/md/html com preview, tags #tag, categorias, painel de histórico
- Preferências: números de linha, marcador de coluna (padrão 80), quebra de linha,
  fonte/tamanho, 4 temas e colorização de sintaxe (overlay no textarea)
- Frontend vanilla embutido via go:embed; Dockerfile multi-stage + docker-compose
2026-11-04 09:41:46 -03:00

36 lines
1.0 KiB
Go

package api
import (
"net/http"
"strings"
)
const maxBody = 4 << 20 // folga sobre o limite de conteúdo da store (2 MiB)
type renderInput struct {
Content string `json:"content"`
Format string `json:"format"`
}
// handleRender converte Markdown em HTML seguro (raw HTML do markdown é
// escapado pelo goldmark; a nota em si é sempre renderizada via innerHTML).
func (s *Server) handleRender(w http.ResponseWriter, r *http.Request) {
var in renderInput
if err := decodeJSON(r, &in, maxBody); err != nil {
writeError(w, http.StatusBadRequest, "corpo JSON inválido")
return
}
switch in.Format {
case "html":
// O preview de HTML usa iframe com sandbox no cliente; não renderiza aqui.
writeError(w, http.StatusBadRequest, "formato html usa preview em iframe")
return
}
var buf strings.Builder
if err := s.md.Convert([]byte(in.Content), &buf); err != nil {
writeError(w, http.StatusInternalServerError, "falha ao renderizar markdown")
return
}
writeJSON(w, http.StatusOK, map[string]string{"html": buf.String()})
}