Merge branch 'markdown-API' into activitypub-mentions

This commit is contained in:
Matt Baer 2019-11-28 10:31:35 -05:00
commit 8c1bf2ddd5
2 changed files with 45 additions and 0 deletions

View File

@ -11,9 +11,12 @@
package writefreely package writefreely
import ( import (
"encoding/json"
"fmt" "fmt"
"html" "html"
"html/template" "html/template"
"io/ioutil"
"net/http"
"regexp" "regexp"
"strings" "strings"
"unicode" "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))) 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
}

View File

@ -113,6 +113,9 @@ func InitRoutes(apper Apper, r *mux.Router) *mux.Router {
// Sign up validation // Sign up validation
write.HandleFunc("/api/alias", handler.All(handleUsernameCheck)).Methods("POST") 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 // Handle collections
write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST") write.HandleFunc("/api/collections", handler.All(newCollection)).Methods("POST")
apiColls := write.PathPrefix("/api/collections/").Subrouter() apiColls := write.PathPrefix("/api/collections/").Subrouter()