release: v1.0.1 — fix Go libweb dynamic-route query, Markdown, CSS false positives
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
761b92afce
commit
3eb8e732ea
17 files changed
+375
-43
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
)
|
||||
|
||||
// markdownToHTML renders article body Markdown to HTML (GFM subset), matching
|
||||
// the Rust web plugin's pulldown-cmark path.
|
||||
func markdownToHTML(md string) string {
|
||||
md = strings.TrimSpace(md)
|
||||
if md == "" {
|
||||
return ""
|
||||
}
|
||||
gm := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
extension.GFM,
|
||||
extension.Strikethrough,
|
||||
),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithAutoHeadingID(),
|
||||
),
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithHardWraps(),
|
||||
html.WithXHTML(),
|
||||
),
|
||||
)
|
||||
var buf bytes.Buffer
|
||||
if err := gm.Convert([]byte(md), &buf); err != nil {
|
||||
return `<div class="article-body"><p class="article-p">` + esc(md) + `</p></div>`
|
||||
}
|
||||
return `<div class="article-body md">` + buf.String() + `</div>`
|
||||
}
|
||||
@@ -191,7 +191,8 @@ func navWhenVisible(when navWhen, auth *bool) bool {
|
||||
func selectPageData(url, tableName string, page map[string]any, limit, offset int64) map[string]any {
|
||||
var where any
|
||||
if q, ok := page["query"]; ok {
|
||||
where = q
|
||||
// Expand `{param}` placeholders from dynamic route params (ABI: 查询条件).
|
||||
where = resolvePlaceholders(q, pageParams(page))
|
||||
}
|
||||
order, _ := page["order"].(string)
|
||||
opts := db.SelectOpts{Where: where, Order: order}
|
||||
@@ -205,6 +206,78 @@ func selectPageData(url, tableName string, page map[string]any, limit, offset in
|
||||
return out
|
||||
}
|
||||
|
||||
func pageParams(page map[string]any) map[string]any {
|
||||
if page == nil {
|
||||
return nil
|
||||
}
|
||||
p, _ := page["params"].(map[string]any)
|
||||
return p
|
||||
}
|
||||
|
||||
// resolvePlaceholders walks query / route values and replaces `{name}` with
|
||||
// page.params[name]. Unmatched placeholders are left as `{name}` for debugging.
|
||||
func resolvePlaceholders(v any, params map[string]any) any {
|
||||
if len(params) == 0 {
|
||||
return v
|
||||
}
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
return resolveParamsString(t, params)
|
||||
case map[string]any:
|
||||
out := make(map[string]any, len(t))
|
||||
for k, val := range t {
|
||||
out[k] = resolvePlaceholders(val, params)
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(t))
|
||||
for i, val := range t {
|
||||
out[i] = resolvePlaceholders(val, params)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
// ResolveRouteParams substitutes `{name}` in a route pattern using params.
|
||||
func ResolveRouteParams(pattern string, params map[string]any) string {
|
||||
return resolveParamsString(pattern, params)
|
||||
}
|
||||
|
||||
func resolveParamsString(s string, params map[string]any) string {
|
||||
if !strings.Contains(s, "{") || len(params) == 0 {
|
||||
return s
|
||||
}
|
||||
var out strings.Builder
|
||||
rest := s
|
||||
for {
|
||||
start := strings.Index(rest, "{")
|
||||
if start < 0 {
|
||||
out.WriteString(rest)
|
||||
break
|
||||
}
|
||||
out.WriteString(rest[:start])
|
||||
rest = rest[start+1:]
|
||||
end := strings.Index(rest, "}")
|
||||
if end < 0 {
|
||||
out.WriteByte('{')
|
||||
out.WriteString(rest)
|
||||
break
|
||||
}
|
||||
key := rest[:end]
|
||||
if val, ok := params[key]; ok {
|
||||
out.WriteString(text(val))
|
||||
} else {
|
||||
out.WriteByte('{')
|
||||
out.WriteString(key)
|
||||
out.WriteByte('}')
|
||||
}
|
||||
rest = rest[end+1:]
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func resolveLinks(raw any, dbURL string, page map[string]any) []navLink {
|
||||
if raw == nil {
|
||||
return nil
|
||||
@@ -400,6 +473,7 @@ func fieldCSS(obj map[string]any, field string) string {
|
||||
|
||||
func partSrcPrefix(page map[string]any) string {
|
||||
r, _ := page["_route"].(string)
|
||||
r = resolveParamsString(r, pageParams(page))
|
||||
if r == "" || r == "/" {
|
||||
return ""
|
||||
}
|
||||
@@ -479,6 +553,10 @@ const shellWidgets = `
|
||||
.site-form { margin-top:1.25rem; max-width:28rem; }
|
||||
.article { background:#fff; border:1px solid var(--line); border-radius:8px; padding:2rem; margin-top:1.5rem; }
|
||||
.article-title { margin:0 0 .75rem; font-size:2rem; }
|
||||
.article-body { line-height:1.75; color:var(--ink); }
|
||||
.article-body.md { line-height: 1.75; }
|
||||
.article-body.md pre { background:#f5f5f4; border:1px solid var(--line); border-radius:6px; padding:1rem; overflow-x:auto; }
|
||||
.article-body.md code { font-size:.9em; }
|
||||
.pagination { display:flex; gap:1rem; align-items:center; margin:1.5rem 0; font-size:.95rem; }
|
||||
`
|
||||
|
||||
@@ -624,8 +702,7 @@ func renderArticle(it map[string]any) string {
|
||||
s.WriteString(fmt.Sprintf(`<div class="article-tags">%s</div>`, esc(tag)))
|
||||
}
|
||||
if body != "" {
|
||||
// Minimal: escape as paragraphs (full markdown deferred).
|
||||
s.WriteString(fmt.Sprintf(`<div class="article-body"><p class="article-p">%s</p></div>`, esc(body)))
|
||||
s.WriteString(markdownToHTML(body))
|
||||
}
|
||||
s.WriteString("</article>")
|
||||
return s.String()
|
||||
|
||||
@@ -227,3 +227,33 @@ func TestRenderPageNavChrome(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRouteParams(t *testing.T) {
|
||||
got := render.ResolveRouteParams("/post/{slug}", map[string]any{"slug": "hello-marqdo"})
|
||||
if got != "/post/hello-marqdo" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
got = render.ResolveRouteParams("/post/{slug}", map[string]any{})
|
||||
if got != "/post/{slug}" {
|
||||
t.Fatalf("unmatched must stay: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartSlotSrcUsesResolvedRoute(t *testing.T) {
|
||||
page := map[string]any{
|
||||
"title": "post",
|
||||
"intro": "",
|
||||
"_route": "/post/{slug}",
|
||||
"params": map[string]any{"slug": "hello-marqdo"},
|
||||
"parts": map[string]any{
|
||||
"index": map[string]any{"slot": "main"},
|
||||
},
|
||||
}
|
||||
html := render.RenderPage(page, "", "")
|
||||
if strings.Contains(html, "/post/{slug}/_part/") {
|
||||
t.Fatalf("slot src still has placeholder: %s", html)
|
||||
}
|
||||
if !strings.Contains(html, `data-slot-src="/post/hello-marqdo/_part/index"`) {
|
||||
t.Fatalf("missing resolved slot src: %s", html)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
)
|
||||
|
||||
func TestResolvePlaceholdersColumnarAndRows(t *testing.T) {
|
||||
params := map[string]any{"slug": "hello-marqdo"}
|
||||
|
||||
col := map[string]any{
|
||||
"字段": []any{"slug"},
|
||||
"操作": []any{"="},
|
||||
"值": []any{"{slug}"},
|
||||
}
|
||||
got := resolvePlaceholders(col, params).(map[string]any)
|
||||
vals := got["值"].([]any)
|
||||
if vals[0] != "hello-marqdo" {
|
||||
t.Fatalf("columnar 值=%v", vals)
|
||||
}
|
||||
|
||||
rows := []any{
|
||||
map[string]any{"字段": "slug", "操作": "=", "值": "{slug}"},
|
||||
}
|
||||
gotRows := resolvePlaceholders(rows, params).([]any)
|
||||
m := gotRows[0].(map[string]any)
|
||||
if m["值"] != "hello-marqdo" {
|
||||
t.Fatalf("row 值=%v", m["值"])
|
||||
}
|
||||
|
||||
top := map[string]any{"slug": "{slug}"}
|
||||
gotTop := resolvePlaceholders(top, params).(map[string]any)
|
||||
if gotTop["slug"] != "hello-marqdo" {
|
||||
t.Fatalf("map string=%v", gotTop["slug"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectPageDataResolvesSlug(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "t.db")
|
||||
url := "sqlite:" + dbPath
|
||||
t.Cleanup(db.ResetPool)
|
||||
|
||||
fields := []any{
|
||||
map[string]any{"name": "slug", "type": "text", "nullable": false},
|
||||
map[string]any{"name": "title", "type": "text", "nullable": false},
|
||||
map[string]any{"name": "body", "type": "text", "nullable": true},
|
||||
}
|
||||
if _, err := db.Init(url, "posts", fields); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Insert(url, "posts", []any{
|
||||
map[string]any{"slug": "hello-marqdo", "title": "Hello", "body": "## Hi\n\n*world*"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
page := map[string]any{
|
||||
"title": "post",
|
||||
"detail": true,
|
||||
"_route": "/post/{slug}",
|
||||
"params": map[string]any{"slug": "hello-marqdo"},
|
||||
"query": map[string]any{
|
||||
"字段": []any{"slug"},
|
||||
"操作": []any{"="},
|
||||
"值": []any{"{slug}"},
|
||||
},
|
||||
"main": []any{
|
||||
map[string]any{"front": "title", "back": "posts.title"},
|
||||
map[string]any{"front": "body", "back": "posts.body"},
|
||||
},
|
||||
"parts": map[string]any{
|
||||
"index": map[string]any{"slot": "main"},
|
||||
},
|
||||
}
|
||||
html := RenderPage(page, url, "")
|
||||
if !strings.Contains(html, `class="article-title"`) || !strings.Contains(html, "Hello") {
|
||||
t.Fatalf("missing article title: %s", html)
|
||||
}
|
||||
if !strings.Contains(html, `class="article-body md"`) {
|
||||
t.Fatalf("missing markdown body wrapper: %s", html)
|
||||
}
|
||||
if !strings.Contains(html, "world") {
|
||||
t.Fatalf("missing markdown content: %s", html)
|
||||
}
|
||||
if strings.Contains(html, "{slug}") {
|
||||
t.Fatalf("literal {slug} still present: %s", html)
|
||||
}
|
||||
if !strings.Contains(html, `/post/hello-marqdo/_part/`) {
|
||||
t.Fatalf("slot prefix not resolved: %s", html)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkdownToHTML(t *testing.T) {
|
||||
out := markdownToHTML("## Title\n\n**bold** and `code`")
|
||||
if !strings.Contains(out, `class="article-body md"`) {
|
||||
t.Fatalf("%s", out)
|
||||
}
|
||||
if !strings.Contains(out, "<h2") || !strings.Contains(out, "<strong>bold</strong>") {
|
||||
t.Fatalf("%s", out)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user