From aeac4d6d207536310d36a79aa41887422f59a1e7 Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 18 Sep 2026 11:41:52 +0800 Subject: [PATCH] feat(web): SSR news rail via list target=rail and KaTeX boot glue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List binds with 插槽=rail render aside.side-rail and set has-rail; cards honor pinned; pages that load KaTeX auto-render get an official deferred math boot so authors need no volume.js. Co-authored-by: Cursor --- ext/web/网页.mq.md | 1 + plugins/web/internal/render/rail_test.go | 28 ++++ plugins/web/internal/render/render.go | 169 +++++++++++++++++++++-- 3 files changed, 186 insertions(+), 12 deletions(-) create mode 100644 plugins/web/internal/render/rail_test.go diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 68a7046..a721f94 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -235,6 +235,7 @@ import table:lib/table.mq.md 详情页副绑定(如评论列表)。绑定表形同 `主体装配`;`条件` 支持 `{参数}`; 可选 `插槽`(元素 id)把列表注入引言 HTML(同表单插槽)。 +`插槽=rail` / `轨` / `新闻轨` 时渲染为右侧 `aside.side-rail`(量子新闻轨),并为 body 加上 `has-rail`。 *> web_compose_list page=`self` main=`主体` query=`条件` order=`排序` target=`插槽`* diff --git a/plugins/web/internal/render/rail_test.go b/plugins/web/internal/render/rail_test.go new file mode 100644 index 0000000..e20d288 --- /dev/null +++ b/plugins/web/internal/render/rail_test.go @@ -0,0 +1,28 @@ +package render + +import ( + "strings" + "testing" +) + +func TestIsRailTarget(t *testing.T) { + for _, in := range []string{"rail", "轨", "新闻轨", "#side-rail", "aside.side-rail"} { + if !isRailTarget(in) { + t.Fatalf("expected rail: %q", in) + } + } + if isRailTarget("#comment-list") { + t.Fatal("comment list must not be rail") + } +} + +func TestRenderNewsRail(t *testing.T) { + html := renderNewsRail([]map[string]any{ + {"title": "A", "href": "https://ex.test/a", "meta": "2026-09-01", "tag": "IBM"}, + }) + for _, want := range []string{`side-rail--news`, `https://ex.test/a`, `target="_blank"`, `量子新闻`, `全部 →`} { + if !strings.Contains(html, want) { + t.Fatalf("missing %q in %s", want, html) + } + } +} diff --git a/plugins/web/internal/render/render.go b/plugins/web/internal/render/render.go index 50141df..96a7328 100644 --- a/plugins/web/internal/render/render.go +++ b/plugins/web/internal/render/render.go @@ -467,6 +467,18 @@ type listBind struct { Items []map[string]any Target string HTML string + Rail bool +} + +func isRailTarget(target string) bool { + t := strings.TrimSpace(strings.ToLower(target)) + t = strings.TrimPrefix(t, "#") + switch t { + case "rail", "轨", "side-rail", "aside.side-rail", "新闻轨": + return true + default: + return false + } } func resolveLists(page map[string]any, dbURL string) []listBind { @@ -496,7 +508,15 @@ func resolveLists(page map[string]any, dbURL string) []listBind { if o, ok := em["order"].(string); ok { sub["order"] = o } - data := selectPageData(dbURL, tn, sub, 200, 0) + limit := int64(200) + target := "" + if t, ok := em["target"].(string); ok { + target = t + } + if isRailTarget(target) { + limit = 24 + } + data := selectPageData(dbURL, tn, sub, limit, 0) rawRows, _ := data["rows"].([]any) projected := table.ProjectRows(arr, rawRows) parr, _ := projected.([]any) @@ -506,16 +526,61 @@ func resolveLists(page map[string]any, dbURL string) []listBind { items = append(items, m) } } - lb := listBind{Items: items} - if t, ok := em["target"].(string); ok { - lb.Target = t + lb := listBind{Items: items, Target: target, Rail: isRailTarget(target)} + if lb.Rail { + lb.HTML = renderNewsRail(items) + } else { + lb.HTML = renderListSection(page, items) } - lb.HTML = renderListSection(page, items) out = append(out, lb) } return out } +func renderNewsRail(items []map[string]any) string { + var list strings.Builder + if len(items) == 0 { + list.WriteString(`
  • 暂无快讯,请在库表 news 中添加。
  • `) + } else { + for _, it := range items { + title := text(it["title"]) + href := strings.TrimSpace(text(it["href"])) + if href == "" { + href = "#" + } + meta := text(it["meta"]) + if len(meta) > 10 { + meta = meta[:10] + } + tag := text(it["tag"]) + ext := strings.HasPrefix(href, "http://") || strings.HasPrefix(href, "https://") + list.WriteString(`
  • `) + list.WriteString(``) + list.WriteString(``) + if meta != "" { + list.WriteString(``) + } + if tag != "" { + list.WriteString(`` + esc(tag) + ``) + } + list.WriteString(``) + list.WriteString(`` + esc(title) + ``) + list.WriteString(`
  • `) + } + } + return `` +} + func renderListSection(page map[string]any, items []map[string]any) string { var s strings.Builder s.WriteString(`
    `) @@ -559,12 +624,19 @@ func renderListItem(page map[string]any, it map[string]any) string { return s.String() } -func applyListTargets(intro string, lists []listBind) (string, string) { +func applyListTargets(intro string, lists []listBind) (string, string, string) { rest := strings.Builder{} + rail := "" for _, lb := range lists { if lb.HTML == "" { continue } + if lb.Rail { + if rail == "" { + rail = lb.HTML + } + continue + } if lb.Target != "" && intro != "" { if injected, ok := injectHTMLIntoID(intro, lb.Target, lb.HTML); ok { intro = injected @@ -573,7 +645,7 @@ func applyListTargets(intro string, lists []listBind) (string, string) { } rest.WriteString(lb.HTML) } - return intro, rest.String() + return intro, rest.String(), rail } func fieldCSS(obj map[string]any, field string) string { @@ -766,10 +838,44 @@ func headHTML(page map[string]any, defaultTitle string) string { assetVersion = v } s.WriteString(assets.RenderHeadLinksWithVersion(links, assetVersion)) + if headNeedsKatexBoot(links) { + s.WriteString(katexBootScript()) + } } return s.String() } +func headNeedsKatexBoot(links []assets.HeadLink) bool { + hasAuto := false + for _, l := range links { + href := strings.ToLower(l.Href) + if strings.Contains(href, "auto-render") { + hasAuto = true + break + } + } + return hasAuto +} + +func katexBootScript() string { + // Official host glue: call KaTeX auto-render after deferred scripts load. + // Authors must not ship this as business JS. + return `` +} + func cardHref(page map[string]any, href string) string { href = strings.TrimSpace(href) if href == "" { @@ -798,8 +904,31 @@ func renderCard(page map[string]any, it map[string]any) string { tag := text(it["tag"]) tc := classAttr(fieldCSS(it, "title")) bc := classAttr(fieldCSS(it, "body")) + cls := "card" + switch v := it["pinned"].(type) { + case bool: + if v { + cls += " is-pinned" + } + case string: + if v == "1" || strings.EqualFold(v, "true") { + cls += " is-pinned" + } + case float64: + if v == 1 { + cls += " is-pinned" + } + case int: + if v == 1 { + cls += " is-pinned" + } + case int64: + if v == 1 { + cls += " is-pinned" + } + } var card strings.Builder - card.WriteString(`
    `) + card.WriteString(`
    `) if href != "" { card.WriteString(fmt.Sprintf(``, esc(href))) } @@ -1013,12 +1142,12 @@ func pushIntroAndForm(buf *strings.Builder, intro string, page map[string]any) { } if formHTML != "" && target != "" && intro != "" { if injected, ok := injectHTMLIntoID(intro, target, formHTML); ok { - buf.WriteString(fmt.Sprintf(`
    %s
    `, injected)) + buf.WriteString(fmt.Sprintf(`
    %s
    `, introWrapClass(injected), injected)) return } } if intro != "" { - buf.WriteString(fmt.Sprintf(`
    %s
    `, intro)) + buf.WriteString(fmt.Sprintf(`
    %s
    `, introWrapClass(intro), intro)) } if formHTML != "" { buf.WriteString(formHTML) @@ -1026,6 +1155,13 @@ func pushIntroAndForm(buf *strings.Builder, intro string, page map[string]any) { } } +func introWrapClass(intro string) string { + if strings.Contains(intro, "masthead-guide") { + return "main-intro masthead-split" + } + return "main-intro" +} + func renderFragment(page map[string]any, dbURL, slot string) string { slot = table.NormalizeSlot(slot) switch slot { @@ -1050,7 +1186,7 @@ func renderFragment(page map[string]any, dbURL, slot string) string { default: intro, items, _ := resolveMain(page, dbURL) lists := resolveLists(page, dbURL) - intro, listRest := applyListTargets(intro, lists) + intro, listRest, _ := applyListTargets(intro, lists) isDetail, _ := page["detail"].(bool) var body strings.Builder if isDetail { @@ -1180,7 +1316,7 @@ func RenderPage(page map[string]any, dbURL string, partID string) string { } intro = ApplyAuthFormIntoIntro(intro, spec, csrfTok, authNext, flashErr) } - intro, listRest := applyListTargets(intro, lists) + intro, listRest, railHTML := applyListTargets(intro, lists) isDetail, _ := page["detail"].(bool) if isDetail { if len(items) > 0 { @@ -1255,6 +1391,13 @@ func RenderPage(page map[string]any, dbURL string, partID string) string { bodyClass = bodyClass + " " + strings.TrimSpace(extraBody) } } + if railHTML != "" && !strings.Contains(bodyClass, "has-rail") { + if bodyClass == "" { + bodyClass = "has-rail" + } else { + bodyClass = bodyClass + " has-rail" + } + } html := fmt.Sprintf(` @@ -1268,6 +1411,7 @@ func RenderPage(page map[string]any, dbURL string, partID string) string { %s
    %s
    %s +%s `, headHTML(page, title), styleBlock, @@ -1277,6 +1421,7 @@ func RenderPage(page map[string]any, dbURL string, partID string) string { slotClass(page, "main", "main"), slotAttrs("main", parts, page), mainHTML.String(), + railHTML, footerHTML, ) return InjectAuthFields(html, csrfTok, authNext, flashErr)