Merge branch 'markdown-API' into activitypub-mentions
This commit is contained in:
commit
8c1bf2ddd5
|
@ -11,9 +11,12 @@
|
|||
package writefreely
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
@ -237,3 +240,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
|
||||
}
|
||||
|
|
|
@ -113,6 +113,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…
Reference in New Issue