feat: tighten types — MasterPageBlock JSON tags + *string, BlockNote []map[string]any

- Add JSON struct tags to MasterPageBlock and MasterPageDefinition
- Change MasterPageBlock.HtmlContent from string to *string (nullable)
- Change BlockNote renderer signatures from []any to []map[string]any
- Move type assertions to JSON boundary in blocksFromRaw/inlineContentFromRaw

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Alex Dunmow 2026-05-01 11:37:41 +08:00
parent 13d741979a
commit 43deff21f7
2 changed files with 29 additions and 34 deletions

View File

@ -47,20 +47,20 @@ type AIAction struct {
// MasterPageBlock defines a block to add to a master page during provisioning.
type MasterPageBlock struct {
BlockKey string
Title string
Content map[string]any
HtmlContent string
Slot string
SortOrder int32
BlockKey string `json:"block_key"`
Title string `json:"title"`
Content map[string]any `json:"content"`
HtmlContent *string `json:"html_content"`
Slot string `json:"slot"`
SortOrder int32 `json:"sort_order"`
}
// MasterPageDefinition defines a default master page that a plugin provisions.
type MasterPageDefinition struct {
Key string
Title string
PageTemplates []string
Blocks []MasterPageBlock
Key string `json:"key"`
Title string `json:"title"`
PageTemplates []string `json:"page_templates"`
Blocks []MasterPageBlock `json:"blocks"`
}
// Plugin status constants.

View File

@ -16,7 +16,7 @@ func BlockNoteToHTML(doc map[string]any) string {
return renderBlocks(blocks)
}
func renderBlocks(blocks []any) string {
func renderBlocks(blocks []map[string]any) string {
var sb strings.Builder
var currentListType string
var listItems []map[string]any
@ -47,11 +47,7 @@ func renderBlocks(blocks []any) string {
currentListType = ""
}
for _, block := range blocks {
blockMap, ok := block.(map[string]any)
if !ok {
continue
}
for _, blockMap := range blocks {
blockType, _ := blockMap["type"].(string)
if blockType == "bulletListItem" || blockType == "numberedListItem" {
@ -71,36 +67,40 @@ func renderBlocks(blocks []any) string {
return sb.String()
}
func inlineContentFromRaw(raw any) []any {
func inlineContentFromRaw(raw any) []map[string]any {
switch v := raw.(type) {
case []any:
return v
case []map[string]any:
items := make([]any, 0, len(v))
items := make([]map[string]any, 0, len(v))
for _, item := range v {
items = append(items, item)
if m, ok := item.(map[string]any); ok {
items = append(items, m)
}
}
return items
case []map[string]any:
return v
case string:
if v == "" {
return nil
}
return []any{map[string]any{"type": "text", "text": v}}
return []map[string]any{{"type": "text", "text": v}}
default:
return nil
}
}
func blocksFromRaw(raw any) []any {
func blocksFromRaw(raw any) []map[string]any {
switch v := raw.(type) {
case []any:
return v
case []map[string]any:
items := make([]any, 0, len(v))
items := make([]map[string]any, 0, len(v))
for _, item := range v {
items = append(items, item)
if m, ok := item.(map[string]any); ok {
items = append(items, m)
}
}
return items
case []map[string]any:
return v
default:
return nil
}
@ -265,14 +265,9 @@ func renderChildren(children any) string {
return renderBlocks(blocks)
}
func renderInlineContent(content []any) string {
func renderInlineContent(content []map[string]any) string {
var sb strings.Builder
for _, item := range content {
itemMap, ok := item.(map[string]any)
if !ok {
continue
}
for _, itemMap := range content {
itemType, _ := itemMap["type"].(string)
text, _ := itemMap["text"].(string)
styles, _ := itemMap["styles"].(map[string]any)