markdown param for get one

master
breel 2020-08-28 14:38:15 -06:00
parent d67654e601
commit c2cb535105
3 changed files with 31 additions and 1 deletions

View File

@ -10,6 +10,9 @@ import (
"path"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
"gopkg.in/mgo.v2/bson"
@ -119,6 +122,12 @@ func (rest *REST) entitiesGetOne(w http.ResponseWriter, r *http.Request) {
}
resp[entity.Connections] = m
}
_, md := r.URL.Query()["md"]
if md {
renderer := html.NewRenderer(html.RendererOptions{Flags: html.CommonFlags | html.TOC})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
resp["md"] = markdownHead + string(markdown.ToHTML([]byte(one.Text), parser, renderer)) + markdownTail
}
rest.respMap(w, entityScope[0], resp)
}

View File

@ -26,10 +26,21 @@ func TestEntities(t *testing.T) {
return one.Name == "myname"
})
w = testEntitiesMethod(t, authit, rest, http.MethodGet, "/"+id, ``)
w = testEntitiesMethod(t, authit, rest, http.MethodGet, "/"+id+"?md", ``)
if w.Code != http.StatusOK {
t.Fatal(w.Code)
}
b := w.Body.Bytes()
var markdowned map[string]struct {
MD string `json:"md"`
}
if err := json.Unmarshal(b, &markdowned); err != nil {
t.Fatal(err)
} else if len(markdowned) != 1 {
t.Fatal(len(markdowned))
} else if len(markdowned[id].MD) == 0 {
t.Fatal(markdowned, string(b))
}
id2 := testEntitiesGetOneResponse(t, w.Body, func(one entity.One) bool {
return one.Name == "myname"
})

10
server/markdown.go Normal file
View File

@ -0,0 +1,10 @@
package server
const (
markdownHead = `<div>
<style scoped>
@charset"UTF-8";button,input,textarea{transition:background-color 0.1s linear, border-color 0.1s linear, color 0.1s linear, box-shadow 0.1s linear, transform 0.1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#000000}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid #0096bfab;margin:1.5em 0;padding:0.5em 1em;font-style:italic}blockquote > footer{margin-top:10px;font-style:normal}blockquote cite{font-style:normal}address{font-style:normal}a[href^='mailto']::before{content:'📧 '}a[href^='tel']::before{content:'📞 '}a[href^='sms']::before{content:'💬 '}button,input[type='submit'],input[type='button'],input[type='checkbox']{cursor:pointer}input:not([type='checkbox']):not([type='radio']),select{display:block}button,input,select,textarea{color:#000000;background-color:#efefef;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}input:not([type='checkbox']):not([type='radio']),select,button,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type='submit'],input[type='button']{padding-right:30px;padding-left:30px}button:hover,input[type='submit']:hover,input[type='button']:hover{background:#dddddd}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px #0096bfab}input[type='checkbox']:active,input[type='radio']:active,input[type='submit']:active,input[type='button']:active,button:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:0.5}::-webkit-input-placeholder{color:#949494}:-ms-input-placeholder{color:#949494}::-ms-input-placeholder{color:#949494}::placeholder{color:#949494}a{text-decoration:none;color:#0076d1}a:hover{text-decoration:underline}code,kbd{background:#efefef;color:#000000;padding:5px;border-radius:6px}pre > code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(even){background-color:#efefef}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#efefef;border-radius:6px}::-webkit-scrollbar-thumb{background:#d5d5d5;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#c4c4c4}
</style>
`
markdownTail = `</div>`
)