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:
1 parent
4b7643d833
commit
6bd127a313
5 files changed
+77
-2
No files matched your search
@@ -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 == "" {
|
||||
|
||||
Reference in new issue
Block a user