Add export format: "markdown"
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-06-11 22:20:17 +02:00
parent 20e454d4a3
commit 524b0140b5

19
main.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"html"
"html/template"
"io"
"log"
"net/http"
"net/url"
@ -441,6 +442,15 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()
format := r.URL.Query().Get("format")
if format == "" {
format = "html"
}
if !(format == "html" || format == "markdown") {
http.Error(w, "unknown format", http.StatusBadRequest)
}
page := r.URL.Path[1:]
if page == "" {
page = "Home"
@ -501,8 +511,8 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pageText = builder.String()
}
if format == "html" {
pageText = renderLinks(pageText)
pageText = renderMarkdown2(pageText)
pageBase := getPageBase()
@ -526,7 +536,12 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// http.Error(w, err.Error(), 500)
return
}
} else if format == "markdown" {
_, err = io.WriteString(w, "# ")
_, err = io.WriteString(w, cleanTitle(page))
_, err = io.WriteString(w, "\n\n")
_, err = io.WriteString(w, pageText)
}
}
func renderLinks(pageText string) string {