From 23e4bc406e255749cf64d9f2fe5101d84d2409f7 Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 10 Sep 2026 17:51:50 +0800 Subject: [PATCH] feat(web): Go W-G7 RSS, sitemap, redirect, and SEO bags Add paginate/meta wiring, RSS and sitemap builders, and app redirect/error/sitemap/robots route bags so content and W7 offline smokes pass on libweb.so. Co-authored-by: Cursor --- doc/design/ext-web-go-rewrite.md | 2 +- plugins/web/abi_w7.go | 244 +++++++++++++++++++ plugins/web/internal/app/app.go | 118 +++++++++ plugins/web/internal/app/app_test.go | 58 +++++ plugins/web/internal/page/page.go | 17 ++ plugins/web/internal/page/page_test.go | 12 + plugins/web/internal/rss/rss.go | 83 +++++++ plugins/web/internal/rss/rss_test.go | 22 ++ plugins/web/internal/sitemap/sitemap.go | 147 +++++++++++ plugins/web/internal/sitemap/sitemap_test.go | 40 +++ plugins/web/plugin.go | 16 ++ 11 files changed, 758 insertions(+), 1 deletion(-) create mode 100644 plugins/web/abi_w7.go create mode 100644 plugins/web/internal/rss/rss.go create mode 100644 plugins/web/internal/rss/rss_test.go create mode 100644 plugins/web/internal/sitemap/sitemap.go create mode 100644 plugins/web/internal/sitemap/sitemap_test.go diff --git a/doc/design/ext-web-go-rewrite.md b/doc/design/ext-web-go-rewrite.md index 613a619..fa0726f 100644 --- a/doc/design/ext-web-go-rewrite.md +++ b/doc/design/ext-web-go-rewrite.md @@ -392,7 +392,7 @@ scripts/build-web-plugin.sh # go build -buildmode=c-shared -o …/libweb.so | **W-G4** | `web_listen` + route + static + part + middleware + JSON API | ✅ offline:`web-route` / `web-static` / `web-middleware` / `web-part`;`web_listen` 已接线(live 随 W-G5+ hardening) | | **W-G5** | session/auth/password/rate limit/gate/admin | ✅ offline:`web-security` / `web-c0` / `web-c1`;login rate limit 包已就位(HTTP 接线随 listen hardening) | | **W-G6** | upload/download/gallery/ETag + assets W8 | ✅ `web-upload` / `web-assets` / `web-p3` | -| **W-G7** | SEO RSS sitemap robots redirect error paginate markdown | `web-content` / `web-w7-*` | +| **W-G7** | SEO RSS sitemap robots redirect error paginate markdown | ✅ `web-content` / `web-w7-*` | | **W-G8** | WS echo/broadcast + access_log | `web-ws-broadcast-*` | | **W-G9** | Postgres + Redis cache + file/S3 storage | `web-drivers-smoke` | | **W-G10** | proxy + invoke + nested plugin | `web-proxy-invoke` / hosting live | diff --git a/plugins/web/abi_w7.go b/plugins/web/abi_w7.go new file mode 100644 index 0000000..b71f9db --- /dev/null +++ b/plugins/web/abi_w7.go @@ -0,0 +1,244 @@ +package main + +/* +#cgo CFLAGS: -I${SRCDIR}/abi +#include "marqdo_abi_cgo.h" + +extern int web_page_paginate(char *args_json, char **out_json, char **err_msg); +extern int web_rss_build(char *args_json, char **out_json, char **err_msg); +extern int web_app_route_rss(char *args_json, char **out_json, char **err_msg); +extern int web_app_redirect(char *args_json, char **out_json, char **err_msg); +extern int web_app_error_page(char *args_json, char **out_json, char **err_msg); +extern int web_app_sitemap(char *args_json, char **out_json, char **err_msg); +extern int web_app_robots(char *args_json, char **out_json, char **err_msg); +extern int web_sitemap_build(char *args_json, char **out_json, char **err_msg); +*/ +import "C" + +import ( + "fmt" + + "github.com/marqdo/marqdo/plugins/web/internal/app" + "github.com/marqdo/marqdo/plugins/web/internal/page" + "github.com/marqdo/marqdo/plugins/web/internal/rss" + "github.com/marqdo/marqdo/plugins/web/internal/sitemap" +) + +func argInt64(args map[string]any, def int64, keys ...string) int64 { + for _, k := range keys { + v, ok := args[k] + if !ok || v == nil { + continue + } + switch t := v.(type) { + case float64: + return int64(t) + case int: + return int64(t) + case int64: + return t + case string: + if n, err := parseInt64(t); err == nil { + return n + } + } + } + return def +} + +func parseInt64(s string) (int64, error) { + var n int64 + _, err := fmt.Sscanf(s, "%d", &n) + return n, err +} + +func argStatus(args map[string]any) uint16 { + v, ok := first(args, "status", "状态") + if !ok || v == nil { + return 404 + } + switch t := v.(type) { + case float64: + return uint16(t) + case int: + if t < 0 { + return 404 + } + return uint16(t) + case int64: + if t < 0 { + return 404 + } + return uint16(t) + case string: + if n, err := parseInt64(t); err == nil && n >= 0 { + return uint16(n) + } + } + return 404 +} + +func first(args map[string]any, keys ...string) (any, bool) { + for _, k := range keys { + if v, ok := args[k]; ok && v != nil { + return v, true + } + } + return nil, false +} + +func argPermanent(args map[string]any) bool { + v, ok := first(args, "permanent", "永久") + if !ok { + return false + } + switch t := v.(type) { + case bool: + return t + case string: + switch t { + case "true", "True", "1", "yes", "真": + return true + } + case float64: + return int64(t) != 0 + } + return false +} + +func itemsOf(args map[string]any) any { + if v, ok := first(args, "items", "rows", "条目"); ok { + return v + } + return []any{} +} + +//export web_page_paginate +func web_page_paginate(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + offset := argInt64(args, 0, "offset", "偏移") + limit := argInt64(args, 10, "limit", "上限") + path, _ := argStr(args, "path", "路径") + out := page.Paginate(asPageMap(args["page"]), offset, limit, path) + return replyJSON(outJSON, errMsg, out, nil) +} + +//export web_rss_build +func web_rss_build(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + title, _ := argStr(args, "title", "标题") + if title == "" { + title = "Feed" + } + link, _ := argStr(args, "link", "链接") + if link == "" { + link = "/" + } + description, _ := argStr(args, "description", "描述") + items, _ := itemsOf(args).([]any) + if items == nil { + items = []any{} + } + xml := rss.BuildRSS(title, link, description, items) + return replyJSON(outJSON, errMsg, xml, nil) +} + +//export web_app_route_rss +func web_app_route_rss(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + path, err := argStrReq(args, "path", "路径") + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + table, err := argStrReq(args, "table", "表") + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + limit := argInt64(args, 20, "limit", "上限") + order, _ := argStr(args, "order", "排序") + title, _ := argStr(args, "title", "标题") + link, _ := argStr(args, "link", "链接") + description, _ := argStr(args, "description", "描述") + out, err := app.RouteRSS(asPageMap(args["app"]), path, table, order, title, link, description, limit) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_app_redirect +func web_app_redirect(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + from, err := argStrReq(args, "from", "来源", "path", "路径") + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + to, err := argStrReq(args, "to", "目标") + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + out, err := app.Redirect(asPageMap(args["app"]), from, to, argPermanent(args)) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_app_error_page +func web_app_error_page(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + pageV, ok := first(args, "page", "页面") + if !ok { + return replyJSON(outJSON, errMsg, nil, fmt.Errorf("missing `page`")) + } + out, err := app.ErrorPage(asPageMap(args["app"]), argStatus(args), pageV) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_app_sitemap +func web_app_sitemap(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + path, _ := argStr(args, "path", "路径") + base, _ := argStr(args, "base", "基址") + table, _ := argStr(args, "table", "表") + loc, _ := argStr(args, "loc", "定位列") + limit := argInt64(args, 1000, "limit", "上限") + items := itemsOf(args) + out, err := app.Sitemap(asPageMap(args["app"]), path, base, table, loc, limit, items) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_app_robots +func web_app_robots(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + body, _ := argStr(args, "body", "正文") + sitemapURL, _ := argStr(args, "sitemap", "站点地图") + out, err := app.Robots(asPageMap(args["app"]), body, sitemapURL) + return replyJSON(outJSON, errMsg, out, err) +} + +//export web_sitemap_build +func web_sitemap_build(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int { + args, err := parseArgs(argsJSON) + if err != nil { + return replyJSON(outJSON, errMsg, nil, err) + } + base, _ := argStr(args, "base", "基址") + out := sitemap.SitemapJSON(base, itemsOf(args)) + return replyJSON(outJSON, errMsg, out, nil) +} diff --git a/plugins/web/internal/app/app.go b/plugins/web/internal/app/app.go index c597cc5..70f37ef 100644 --- a/plugins/web/internal/app/app.go +++ b/plugins/web/internal/app/app.go @@ -545,6 +545,124 @@ func Gallery(appBag map[string]any, path, storageURL, prefix, title, downloadBas return out, nil } +// Redirect registers a redirect from → to on app.redirects. +func Redirect(appBag map[string]any, from, to string, permanent bool) (map[string]any, error) { + out := clone(appBag) + from, err := NormalizeRoutePath(from, out) + if err != nil { + return nil, err + } + redirects := map[string]any{} + if r, ok := out["redirects"].(map[string]any); ok { + redirects = clone(r) + } + redirects[from] = map[string]any{ + "to": to, + "permanent": permanent, + } + out["redirects"] = redirects + return out, nil +} + +// ErrorPage stores a custom page for HTTP 404 or 500. +func ErrorPage(appBag map[string]any, status uint16, page any) (map[string]any, error) { + if page == nil { + return nil, fmt.Errorf("missing `page`") + } + out := clone(appBag) + key := "page_404" + if status == 500 { + key = "page_500" + } + out[key] = page + return out, nil +} + +// Sitemap registers a sitemap route bag on app.sitemap_routes. +func Sitemap(appBag map[string]any, path, base string, table, loc string, limit int64, items any) (map[string]any, error) { + out := clone(appBag) + if path == "" { + path = "/sitemap.xml" + } + path, err := NormalizeRoutePath(path, out) + if err != nil { + return nil, err + } + if loc == "" { + loc = "path" + } + if items == nil { + items = []any{} + } + routes := map[string]any{} + if r, ok := out["sitemap_routes"].(map[string]any); ok { + routes = clone(r) + } + entry := map[string]any{ + "base": base, + "loc": loc, + "limit": float64(limit), + "items": items, + } + if table != "" { + entry["table"] = table + } + routes[path] = entry + out["sitemap_routes"] = routes + return out, nil +} + +// Robots sets app.robots_body from body or a default Allow-all robots.txt. +func Robots(appBag map[string]any, body, sitemapURL string) (map[string]any, error) { + out := clone(appBag) + text := strings.TrimSpace(body) + if text == "" { + text = defaultRobotsBody(sitemapURL) + } + out["robots_body"] = text + return out, nil +} + +func defaultRobotsBody(sitemapURL string) string { + out := "User-agent: *\nAllow: /\n" + if strings.TrimSpace(sitemapURL) != "" { + out += "Sitemap: " + strings.TrimSpace(sitemapURL) + "\n" + } + return out +} + +// RouteRSS registers an RSS feed route on app.rss_routes. +func RouteRSS(appBag map[string]any, path, table, order, title, link, description string, limit int64) (map[string]any, error) { + out := clone(appBag) + path, err := NormalizeRoutePath(path, out) + if err != nil { + return nil, err + } + if order == "" { + order = "-created_at" + } + if title == "" { + title = "Feed" + } + if link == "" { + link = "/" + } + routes := map[string]any{} + if r, ok := out["rss_routes"].(map[string]any); ok { + routes = clone(r) + } + routes[path] = map[string]any{ + "table": table, + "limit": float64(limit), + "order": order, + "title": title, + "link": link, + "description": description, + } + out["rss_routes"] = routes + return out, nil +} + // Icons normalizes icons table → app.icons + app.site_head. func Icons(appBag map[string]any, table any) (map[string]any, error) { out := clone(appBag) diff --git a/plugins/web/internal/app/app_test.go b/plugins/web/internal/app/app_test.go index 638e420..ea9edfb 100644 --- a/plugins/web/internal/app/app_test.go +++ b/plugins/web/internal/app/app_test.go @@ -161,3 +161,61 @@ func TestGateCustomFields(t *testing.T) { t.Fatalf("%v", g1) } } + +func TestRedirectStoresMap(t *testing.T) { + a := app.New(map[string]any{"page": map[string]any{"title": "Home"}}) + out, err := app.Redirect(a, "/old", "/new", true) + if err != nil { + t.Fatal(err) + } + rd, ok := out["redirects"].(map[string]any) + if !ok { + t.Fatalf("redirects=%v", out["redirects"]) + } + entry, ok := rd["/old"].(map[string]any) + if !ok || entry["to"] != "/new" || entry["permanent"] != true { + t.Fatalf("entry=%v", entry) + } +} + +func TestErrorPage404(t *testing.T) { + a := app.New(nil) + p := map[string]any{"title": "Missing"} + out, err := app.ErrorPage(a, 404, p) + if err != nil { + t.Fatal(err) + } + got, ok := out["page_404"].(map[string]any) + if !ok || got["title"] != "Missing" { + t.Fatalf("page_404=%v", out["page_404"]) + } +} + +func TestRobotsDefaultBody(t *testing.T) { + a := app.New(nil) + out, err := app.Robots(a, "", "https://example.com/sitemap.xml") + if err != nil { + t.Fatal(err) + } + rb, ok := out["robots_body"].(string) + if !ok || !strings.Contains(rb, "Sitemap: https://example.com/sitemap.xml") { + t.Fatalf("robots_body=%v", out["robots_body"]) + } +} + +func TestSitemapRouteBag(t *testing.T) { + a := app.New(nil) + items := []any{map[string]any{"loc": "/"}} + out, err := app.Sitemap(a, "/sitemap.xml", "https://example.com", "", "path", 1000, items) + if err != nil { + t.Fatal(err) + } + routes, ok := out["sitemap_routes"].(map[string]any) + if !ok { + t.Fatalf("sitemap_routes=%v", out["sitemap_routes"]) + } + entry, ok := routes["/sitemap.xml"].(map[string]any) + if !ok || entry["base"] != "https://example.com" { + t.Fatalf("entry=%v", entry) + } +} diff --git a/plugins/web/internal/page/page.go b/plugins/web/internal/page/page.go index 0dca40c..7c2015a 100644 --- a/plugins/web/internal/page/page.go +++ b/plugins/web/internal/page/page.go @@ -22,6 +22,23 @@ func New(args map[string]any) map[string]any { return out } +// Paginate stamps page.paginate with offset/limit/path (Rust web_page_paginate). +func Paginate(page map[string]any, offset, limit int64, path string) map[string]any { + out := map[string]any{} + for k, v := range page { + out[k] = v + } + if path == "" { + path = "/" + } + out["paginate"] = map[string]any{ + "offset": offset, + "limit": limit, + "path": path, + } + return out +} + func strOpt(args map[string]any, key, def string) string { if s, ok := strAny(args, key); ok { return s diff --git a/plugins/web/internal/page/page_test.go b/plugins/web/internal/page/page_test.go index 0c37cde..8065bd2 100644 --- a/plugins/web/internal/page/page_test.go +++ b/plugins/web/internal/page/page_test.go @@ -27,3 +27,15 @@ func TestNewZhAliases(t *testing.T) { t.Fatalf("%v", p) } } + +func TestPaginateBag(t *testing.T) { + p := page.New(map[string]any{"title": "List"}) + out := page.Paginate(p, 0, 5, "/") + pg, ok := out["paginate"].(map[string]any) + if !ok { + t.Fatalf("paginate=%v", out["paginate"]) + } + if pg["offset"] != int64(0) || pg["limit"] != int64(5) || pg["path"] != "/" { + t.Fatalf("%v", pg) + } +} diff --git a/plugins/web/internal/rss/rss.go b/plugins/web/internal/rss/rss.go new file mode 100644 index 0000000..164ba77 --- /dev/null +++ b/plugins/web/internal/rss/rss.go @@ -0,0 +1,83 @@ +// Package rss builds RSS 2.0 feeds (content-site W4 / W-G7). +package rss + +import ( + "fmt" + "strconv" + "strings" +) + +func esc(s string) string { + s = strings.ReplaceAll(s, "&", "&") + s = strings.ReplaceAll(s, "<", "<") + s = strings.ReplaceAll(s, ">", ">") + s = strings.ReplaceAll(s, `"`, """) + return s +} + +func cellStr(v any) string { + switch t := v.(type) { + case nil: + return "" + case string: + return t + case bool: + return strconv.FormatBool(t) + case float64: + if t == float64(int64(t)) { + return strconv.FormatInt(int64(t), 10) + } + return strconv.FormatFloat(t, 'f', -1, 64) + default: + return fmt.Sprint(t) + } +} + +func pick(m map[string]any, keys ...string) any { + for _, k := range keys { + if v, ok := m[k]; ok { + return v + } + } + return nil +} + +// BuildRSS assembles RSS 2.0 XML from channel metadata and row maps. +func BuildRSS(title, link, description string, items []any) string { + body := fmt.Sprintf(` + +%s +%s +%s +`, esc(title), esc(link), esc(description)) + for _, row := range items { + m, ok := row.(map[string]any) + if !ok { + continue + } + itemTitle := cellStr(pick(m, "title", "标题")) + itemLink := cellStr(pick(m, "link", "href", "链接", "slug")) + itemDesc := cellStr(pick(m, "description", "summary", "摘要", "body", "正文")) + pubDate := cellStr(pick(m, "pubDate", "published", "created_at", "发布")) + fullLink := link + if itemLink != "" { + if strings.HasPrefix(itemLink, "http") { + fullLink = itemLink + } else if itemLink[0] == '/' { + fullLink = link + itemLink + } else { + fullLink = link + "/" + itemLink + } + } + body += "" + body += fmt.Sprintf("%s", esc(itemTitle)) + body += fmt.Sprintf("%s", esc(fullLink)) + body += fmt.Sprintf("%s", esc(itemDesc)) + if pubDate != "" { + body += fmt.Sprintf("%s", esc(pubDate)) + } + body += "" + } + body += "" + return body +} diff --git a/plugins/web/internal/rss/rss_test.go b/plugins/web/internal/rss/rss_test.go new file mode 100644 index 0000000..9296017 --- /dev/null +++ b/plugins/web/internal/rss/rss_test.go @@ -0,0 +1,22 @@ +package rss + +import ( + "strings" + "testing" +) + +func TestBuildRSSContainsItem(t *testing.T) { + items := []any{ + map[string]any{"title": "Hi", "slug": "/post/a", "summary": "text"}, + } + xml := BuildRSS("Blog", "http://example.com", "Feed", items) + if !strings.Contains(xml, "Hi") { + t.Fatalf("missing item title: %s", xml) + } + if !strings.Contains(xml, "http://example.com/post/a") { + t.Fatalf("missing full link: %s", xml) + } +} diff --git a/plugins/web/internal/sitemap/sitemap.go b/plugins/web/internal/sitemap/sitemap.go new file mode 100644 index 0000000..fdd3ceb --- /dev/null +++ b/plugins/web/internal/sitemap/sitemap.go @@ -0,0 +1,147 @@ +// Package sitemap builds sitemap.xml and robots.txt helpers (W7 / W-G7). +package sitemap + +import ( + "fmt" + "strconv" + "strings" +) + +func cellStr(v any) string { + switch t := v.(type) { + case nil: + return "" + case string: + return t + case bool: + return strconv.FormatBool(t) + case float64: + if t == float64(int64(t)) { + return strconv.FormatInt(int64(t), 10) + } + return strconv.FormatFloat(t, 'f', -1, 64) + default: + return fmt.Sprint(t) + } +} + +func xmlEscape(s string) string { + r := strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + `"`, """, + "'", "'", + ) + return r.Replace(s) +} + +func rowsFromItems(items any) []map[string]any { + switch t := items.(type) { + case []any: + out := make([]map[string]any, 0, len(t)) + for _, row := range t { + switch r := row.(type) { + case map[string]any: + out = append(out, r) + case string: + out = append(out, map[string]any{"loc": r}) + default: + out = append(out, map[string]any{"loc": cellStr(r)}) + } + } + return out + case map[string]any: + locs, ok := firstArray(t, "loc", "路径", "path", "url") + if !ok { + return nil + } + out := make([]map[string]any, 0, len(locs)) + for i, loc := range locs { + obj := map[string]any{"loc": loc} + for k, v := range t { + if k == "loc" || k == "路径" || k == "path" || k == "url" { + continue + } + if col, ok := v.([]any); ok && i < len(col) { + obj[k] = col[i] + } + } + out = append(out, obj) + } + return out + default: + return nil + } +} + +func firstArray(m map[string]any, keys ...string) ([]any, bool) { + for _, k := range keys { + if v, ok := m[k]; ok { + if a, ok := v.([]any); ok { + return a, true + } + } + } + return nil, false +} + +// BuildSitemap builds a sitemap.xml document from rows. +func BuildSitemap(base string, items any) string { + base = strings.TrimRight(base, "/") + rows := rowsFromItems(items) + out := ` + +` + for _, obj := range rows { + locRaw := cellStr(first(obj, "loc", "url", "路径", "path")) + if locRaw == "" { + continue + } + loc := locRaw + if strings.HasPrefix(locRaw, "http://") || strings.HasPrefix(locRaw, "https://") { + loc = locRaw + } else if strings.HasPrefix(locRaw, "/") { + loc = base + locRaw + } else { + loc = base + "/" + locRaw + } + out += " \n" + out += fmt.Sprintf(" %s\n", xmlEscape(loc)) + if lm := cellStr(first(obj, "lastmod", "更新")); lm != "" { + out += fmt.Sprintf(" %s\n", xmlEscape(lm)) + } + if cf := cellStr(first(obj, "changefreq", "频率")); cf != "" { + out += fmt.Sprintf(" %s\n", xmlEscape(cf)) + } + if pr := cellStr(first(obj, "priority", "优先级")); pr != "" { + out += fmt.Sprintf(" %s\n", xmlEscape(pr)) + } + out += " \n" + } + out += "\n" + return out +} + +func first(m map[string]any, keys ...string) any { + for _, k := range keys { + if v, ok := m[k]; ok { + return v + } + } + return nil +} + +// BuildRobots returns a default robots.txt body with optional Sitemap line. +func BuildRobots(sitemapURL string) string { + out := "User-agent: *\nAllow: /\n" + if strings.TrimSpace(sitemapURL) != "" { + out += "Sitemap: " + strings.TrimSpace(sitemapURL) + "\n" + } + return out +} + +// SitemapJSON is the ABI helper returning {"xml":"…"}. +func SitemapJSON(base string, items any) map[string]any { + return map[string]any{"xml": BuildSitemap(base, items)} +} diff --git a/plugins/web/internal/sitemap/sitemap_test.go b/plugins/web/internal/sitemap/sitemap_test.go new file mode 100644 index 0000000..58519c0 --- /dev/null +++ b/plugins/web/internal/sitemap/sitemap_test.go @@ -0,0 +1,40 @@ +package sitemap + +import ( + "strings" + "testing" +) + +func TestBuildSitemapRowTable(t *testing.T) { + items := []any{ + map[string]any{"loc": "/", "更新": "2026-08-28"}, + map[string]any{"loc": "/about", "更新": "2026-08-28"}, + } + xml := BuildSitemap("https://example.com", items) + if !strings.Contains(xml, "https://example.com/") { + t.Fatalf("missing home loc: %s", xml) + } + if !strings.Contains(xml, "https://example.com/about") { + t.Fatalf("missing about loc: %s", xml) + } + if !strings.Contains(xml, "2026-08-28") { + t.Fatalf("missing lastmod: %s", xml) + } +} + +func TestBuildRobotsWithSitemap(t *testing.T) { + body := BuildRobots("https://example.com/sitemap.xml") + if !strings.Contains(body, "User-agent: *") { + t.Fatalf("missing user-agent: %s", body) + } + if !strings.Contains(body, "Sitemap: https://example.com/sitemap.xml") { + t.Fatalf("missing sitemap line: %s", body) + } +} + +func TestSitemapJSONShape(t *testing.T) { + j := SitemapJSON("https://x.com", []any{map[string]any{"loc": "/a"}}) + if j["xml"] == nil || j["xml"] == "" { + t.Fatalf("expected xml: %v", j) + } +} diff --git a/plugins/web/plugin.go b/plugins/web/plugin.go index a0cef35..c596fd8 100644 --- a/plugins/web/plugin.go +++ b/plugins/web/plugin.go @@ -114,6 +114,14 @@ extern int web_page_images(char *args_json, char **out_json, char **err_msg); extern int web_head(char *args_json, char **out_json, char **err_msg); extern int web_images(char *args_json, char **out_json, char **err_msg); extern int web_app_icons(char *args_json, char **out_json, char **err_msg); +extern int web_page_paginate(char *args_json, char **out_json, char **err_msg); +extern int web_rss_build(char *args_json, char **out_json, char **err_msg); +extern int web_app_route_rss(char *args_json, char **out_json, char **err_msg); +extern int web_app_redirect(char *args_json, char **out_json, char **err_msg); +extern int web_app_error_page(char *args_json, char **out_json, char **err_msg); +extern int web_app_sitemap(char *args_json, char **out_json, char **err_msg); +extern int web_app_robots(char *args_json, char **out_json, char **err_msg); +extern int web_sitemap_build(char *args_json, char **out_json, char **err_msg); static int register_core(void) { if (host_register((char *)"web_go_ready", (char *)"", web_go_ready) != 0) return 1; @@ -181,6 +189,14 @@ static int register_core(void) { if (host_register((char *)"web_head", (char *)"table", web_head) != 0) return 1; if (host_register((char *)"web_images", (char *)"table", web_images) != 0) return 1; if (host_register((char *)"web_app_icons", (char *)"app,table", web_app_icons) != 0) return 1; + if (host_register((char *)"web_page_paginate", (char *)"page,offset,limit,path", web_page_paginate) != 0) return 1; + if (host_register((char *)"web_rss_build", (char *)"title,link,description,items", web_rss_build) != 0) return 1; + if (host_register((char *)"web_app_route_rss", (char *)"app,path,table,limit,order,title,link,description", web_app_route_rss) != 0) return 1; + if (host_register((char *)"web_app_redirect", (char *)"app,from,to,permanent", web_app_redirect) != 0) return 1; + if (host_register((char *)"web_app_error_page", (char *)"app,status,page", web_app_error_page) != 0) return 1; + if (host_register((char *)"web_app_sitemap", (char *)"app,path,base,table,loc,limit,items", web_app_sitemap) != 0) return 1; + if (host_register((char *)"web_app_robots", (char *)"app,body,sitemap", web_app_robots) != 0) return 1; + if (host_register((char *)"web_sitemap_build", (char *)"base,items", web_sitemap_build) != 0) return 1; return 0; } */