add basic API endpoint for rendering markdown

pull/197/head
Rob Loranger 5 years ago
parent 3759f16ed3
commit d2480cb3aa
No known key found for this signature in database
GPG Key ID: D6F1633A4F0903B8
  1. 42
      postrender.go
  2. 3
      routes.go

@ -11,9 +11,12 @@
package writefreely
import (
"encoding/json"
"fmt"
"html"
"html/template"
"io/ioutil"
"net/http"
"regexp"
"strings"
"unicode"
@ -234,3 +237,42 @@ func shortPostDescription(content string) string {
}
return strings.TrimSpace(fmt.Sprintf(fmtStr, strings.Replace(stringmanip.Substring(content, 0, maxLen-truncation), "\n", " ", -1)))
}
func handleRenderMarkdown(app *App, w http.ResponseWriter, r *http.Request) error {
// TODO: accept header
if !IsJSON(r.Header.Get("Content-Type")) {
fmt.Println("missing header")
}
in := struct {
BaseURL string `json:"base_url"`
RawBody string `json:"raw_body"`
}{
BaseURL: "",
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return ErrInternalGeneral
}
err = json.Unmarshal(body, &in)
if err != nil {
return ErrInternalGeneral
}
out := struct {
Body string `json:"body"`
}{
Body: applyMarkdown([]byte(in.RawBody), in.BaseURL, nil),
}
js, err := json.Marshal(out)
if err != nil {
return ErrInternalGeneral
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(js)
return nil
}

@ -110,6 +110,9 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router {
// Sign up validation
write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST")
apiGenerate := write.PathPrefix("/api/generate/").Subrouter()
apiGenerate.HandleFunc("/markdownify", handler.All(handleRenderMarkdown)).Methods("POST")
// Handle collections
write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
apiColls := write.PathPrefix("/api/collections/").Subrouter()

Loading…
Cancel
Save