From 6bd127a3130f106966c0b935424a2ba9ab90d072 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 18 Sep 2026 13:01:43 +0800 Subject: [PATCH] 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 --- plugins/web/internal/form/form.go | 9 +++-- plugins/web/internal/form/form_test.go | 2 ++ plugins/web/internal/httpx/listen.go | 15 ++++++++ plugins/web/internal/render/markdown.go | 5 +++ plugins/web/internal/render/render.go | 48 +++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/plugins/web/internal/form/form.go b/plugins/web/internal/form/form.go index eec01c8..182cd93 100644 --- a/plugins/web/internal/form/form.go +++ b/plugins/web/internal/form/form.go @@ -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(`
`) + fmt.Fprintf(&body, ``, + esc(name), rows, cls, reqAttr, esc(value)) + body.WriteString(`
`) + } else { + fmt.Fprintf(&body, ``, + esc(name), rows, cls, reqAttr, esc(value)) } - fmt.Fprintf(&body, ``, - esc(name), rows, cls, reqAttr, esc(value)) } else { inputType := "text" switch ty { diff --git a/plugins/web/internal/form/form_test.go b/plugins/web/internal/form/form_test.go index c63fc6d..c28aa21 100644 --- a/plugins/web/internal/form/form_test.go +++ b/plugins/web/internal/form/form_test.go @@ -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`, ">发布文章", `href="/desk/posts"`, ">返回列表", diff --git a/plugins/web/internal/httpx/listen.go b/plugins/web/internal/httpx/listen.go index b2339a6..3a7ab69 100644 --- a/plugins/web/internal/httpx/listen.go +++ b/plugins/web/internal/httpx/listen.go @@ -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 = `

预览

` + } + 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) diff --git a/plugins/web/internal/render/markdown.go b/plugins/web/internal/render/markdown.go index 5a331a2..7eb6b26 100644 --- a/plugins/web/internal/render/markdown.go +++ b/plugins/web/internal/render/markdown.go @@ -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 { diff --git a/plugins/web/internal/render/render.go b/plugins/web/internal/render/render.go index 9b9b5fa..8ed963a 100644 --- a/plugins/web/internal/render/render.go +++ b/plugins/web/internal/render/render.go @@ -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(){ ` } +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 `` +} + func cardHref(page map[string]any, href string) string { href = strings.TrimSpace(href) if href == "" {