Add "hmarkdown" format which is more article like
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-11-16 22:48:54 +01:00
parent b8910f9aa6
commit 692a065f82

56
main.go
View File

@ -615,7 +615,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
format = "html" format = "html"
} }
if !(format == "html" || format == "markdown" || format == "json" || format == "metakv") { if !(format == "html" || format == "markdown" || format == "hmarkdown" || format == "json" || format == "metakv") {
http.Error(w, "unknown format", http.StatusBadRequest) http.Error(w, "unknown format", http.StatusBadRequest)
} }
@ -678,33 +678,41 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
builder := strings.Builder{} builder := strings.Builder{}
for _, item := range listItems { if format == "markdown" {
lines := strings.Split(item.Text, "\n") for _, item := range listItems {
if len(lines) > 1 { lines := strings.Split(item.Text, "\n")
first := true if len(lines) > 1 {
for _, line := range lines { first := true
if first { for _, line := range lines {
builder.WriteString(strings.Repeat(" ", item.Indented)) if first {
builder.WriteString("* ") builder.WriteString(strings.Repeat(" ", item.Indented))
builder.WriteString(line) builder.WriteString("* ")
builder.WriteByte('\n') builder.WriteString(line)
first = false builder.WriteByte('\n')
} else { first = false
builder.WriteString(strings.Repeat(" ", item.Indented+1)) } else {
builder.WriteString(line) builder.WriteString(strings.Repeat(" ", item.Indented+1))
builder.WriteByte('\n') builder.WriteString(line)
builder.WriteByte('\n')
}
} }
} } else {
} else { if matches := metaKV.FindStringSubmatch(item.Text); matches != nil {
if matches := metaKV.FindStringSubmatch(item.Text); matches != nil { if matches[1] == "Title" {
if matches[1] == "Title" { title = matches[2]
title = matches[2] }
} }
builder.WriteString(strings.Repeat(" ", item.Indented))
builder.WriteString("* ")
builder.WriteString(item.Text)
builder.WriteByte('\n')
} }
builder.WriteString(strings.Repeat(" ", item.Indented)) }
builder.WriteString("* ") } else if format == "hmarkdown" {
for _, item := range listItems {
builder.WriteString(item.Text) builder.WriteString(item.Text)
builder.WriteByte('\n') builder.WriteByte('\n')
builder.WriteByte('\n')
} }
} }
pageText = builder.String() pageText = builder.String()
@ -742,7 +750,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// http.Error(w, err.Error(), 500) // http.Error(w, err.Error(), 500)
return return
} }
} else if format == "markdown" { } else if format == "markdown" || format == "hmarkdown" {
w.Header().Set("Content-Type", "text/markdown") w.Header().Set("Content-Type", "text/markdown")
w.Header().Set("Cache-Control", "public, max-age=600") w.Header().Set("Cache-Control", "public, max-age=600")
_, err = io.WriteString(w, "# ") _, err = io.WriteString(w, "# ")