feat(web): live markdown preview via POST /_md host glue

Markdown form fields render a dual-pane shell; official boot posts textarea input to /_md for GFM HTML without author business JS.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-18 13:01:43 +08:00
1 parent 4b7643d833
commit 6bd127a313
5 files changed
+77 -2

No files matched your search

+7 -2
View File
@@ -801,9 +801,14 @@ func RenderBodyCtx(formBag map[string]any, formID string, data, errors any, csrf
if ty == "markdown" {
rows = "18"
cls = ` class="mq-markdown" data-mq-field="markdown"`
body.WriteString(`<div class="md-field">`)
fmt.Fprintf(&body, `<textarea name="%s" rows="%s"%s%s>%s</textarea>`,
esc(name), rows, cls, reqAttr, esc(value))
body.WriteString(`<div class="mq-md-preview md-pane-preview" aria-live="polite"></div></div>`)
} else {
fmt.Fprintf(&body, `<textarea name="%s" rows="%s"%s%s>%s</textarea>`,
esc(name), rows, cls, reqAttr, esc(value))
}
fmt.Fprintf(&body, `<textarea name="%s" rows="%s"%s%s>%s</textarea>`,
esc(name), rows, cls, reqAttr, esc(value))
} else {
inputType := "text"
switch ty {
+2
View File
@@ -82,6 +82,8 @@ func TestRenderMarkdownAndLabels(t *testing.T) {
for _, want := range []string{
`data-mq-field="markdown"`,
`class="mq-markdown"`,
`class="md-field"`,
`mq-md-preview`,
">发布文章</button>",
`href="/desk/posts"`,
">返回列表</a>",
+15
View File
@@ -117,6 +117,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
mux.HandleFunc("GET /_part/{part}", st.handleHomePart)
mux.HandleFunc("GET /_form/{id}", st.handleFormGet)
mux.HandleFunc("POST /_form/{id}", st.handleFormPost)
mux.HandleFunc("POST /_md", st.handleMarkdownPreview)
paths := sortedKeys(routes)
for _, p := range paths {
@@ -303,6 +304,20 @@ func (st *state) handleHomePart(w http.ResponseWriter, r *http.Request) {
st.writePart(w, r, st.page, r.PathValue("part"))
}
func (st *state) handleMarkdownPreview(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
html := render.MarkdownToHTML(string(body))
if html == "" {
html = `<p class="lede mq-md-empty">预览</p>`
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = io.WriteString(w, html)
}
func (st *state) preparePage(page map[string]any, r *http.Request) map[string]any {
p := cloneMap(page)
assets.MergeSiteHead(p, st.siteHead)
+5
View File
@@ -10,6 +10,11 @@ import (
"github.com/yuin/goldmark/renderer/html"
)
// MarkdownToHTML renders Markdown to an article-body HTML fragment (GFM subset).
func MarkdownToHTML(md string) string {
return markdownToHTML(md)
}
// markdownToHTML renders article body Markdown to HTML (GFM subset), matching
// the Rust web plugin's pulldown-cmark path.
func markdownToHTML(md string) string {
+48
View File
@@ -842,6 +842,9 @@ func headHTML(page map[string]any, defaultTitle string) string {
s.WriteString(katexBootScript())
}
}
if pageNeedsMarkdownPreview(page) {
s.WriteString(markdownPreviewBootScript())
}
s.WriteString(themeBootScript(page))
s.WriteString(clientEmbedScript(page))
return s.String()
@@ -878,6 +881,51 @@ document.addEventListener("DOMContentLoaded",function(){
</script>`
}
func pageNeedsMarkdownPreview(page map[string]any) bool {
if page == nil {
return false
}
frm, _ := page["form"].(map[string]any)
if frm == nil {
return false
}
fields, _ := frm["fields"].([]any)
for _, f := range fields {
fm, ok := f.(map[string]any)
if !ok {
continue
}
ty := strings.ToLower(strings.TrimSpace(text(fm["type"])))
if ty == "markdown" {
return true
}
}
return false
}
func markdownPreviewBootScript() string {
// Official host glue: live-preview textarea.mq-markdown via POST /_md.
return `<script defer>
document.addEventListener("DOMContentLoaded",function(){
var tas=document.querySelectorAll("textarea.mq-markdown[data-mq-field=markdown]");
for(var i=0;i<tas.length;i++){(function(ta){
var root=ta.closest(".md-field")||ta.parentElement;
var prev=root&&root.querySelector(".mq-md-preview");
if(!prev)return;
var timer=null;
function run(){
fetch("/_md",{method:"POST",headers:{"Content-Type":"text/plain;charset=utf-8"},body:ta.value||""})
.then(function(r){return r.text();})
.then(function(html){prev.innerHTML=html||'<p class="lede mq-md-empty">预览</p>';})
.catch(function(){});
}
ta.addEventListener("input",function(){clearTimeout(timer);timer=setTimeout(run,160);});
run();
})(tas[i]);}
});
</script>`
}
func cardHref(page map[string]any, href string) string {
href = strings.TrimSpace(href)
if href == "" {