- rbac/: Role type, constants, HasPermission, RoleFromString - blocks/: BlockFunc, BlockMeta, BlockContext, context accessors, BlockRegistry interface - templates/: TemplateFunc, meta types, TemplateRegistry interface - auth/: Claims, PublicClaims, context extractors - content/: Content interface, AuthorProfile/PageInfo/PostInfo types - settings/: Settings interface, map accessor helpers - gating/: AccessRule, AccessResult, EvaluateAccess, Gating interface - crypto/: Crypto interface (Encrypt/Decrypt) - render/: BlockNoteToHTML standalone renderer - video/: ParseEmbedURL, EmbedIframeURL - ai/: ToolDefinition, ToolResult, ToolRegistry interface Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
331 lines
8.2 KiB
Go
331 lines
8.2 KiB
Go
package render
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html"
|
|
"strings"
|
|
)
|
|
|
|
// BlockNoteToHTML converts a BlockNote document (map with "blocks" key) to HTML.
|
|
func BlockNoteToHTML(doc map[string]any) string {
|
|
blocks := blocksFromRaw(doc["blocks"])
|
|
if len(blocks) == 0 {
|
|
return ""
|
|
}
|
|
return renderBlocks(blocks)
|
|
}
|
|
|
|
func renderBlocks(blocks []any) string {
|
|
var sb strings.Builder
|
|
var currentListType string
|
|
var listItems []map[string]any
|
|
|
|
flushList := func() {
|
|
if len(listItems) == 0 {
|
|
return
|
|
}
|
|
tag := "ul"
|
|
listStyle := "list-disc"
|
|
if currentListType == "numberedListItem" {
|
|
tag = "ol"
|
|
listStyle = "list-decimal"
|
|
}
|
|
fmt.Fprintf(&sb, "<%s class=\"my-4 pl-6 space-y-2 %s\">\n", tag, listStyle)
|
|
for _, item := range listItems {
|
|
content := inlineContentFromRaw(item["content"])
|
|
childrenHTML := renderChildren(item["children"])
|
|
sb.WriteString("<li>")
|
|
sb.WriteString(renderInlineContent(content))
|
|
if childrenHTML != "" {
|
|
sb.WriteString(childrenHTML)
|
|
}
|
|
sb.WriteString("</li>\n")
|
|
}
|
|
fmt.Fprintf(&sb, "</%s>\n", tag)
|
|
listItems = nil
|
|
currentListType = ""
|
|
}
|
|
|
|
for _, block := range blocks {
|
|
blockMap, ok := block.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
blockType, _ := blockMap["type"].(string)
|
|
|
|
if blockType == "bulletListItem" || blockType == "numberedListItem" {
|
|
if currentListType != "" && currentListType != blockType {
|
|
flushList()
|
|
}
|
|
currentListType = blockType
|
|
listItems = append(listItems, blockMap)
|
|
continue
|
|
}
|
|
|
|
flushList()
|
|
sb.WriteString(renderBlock(blockMap))
|
|
}
|
|
|
|
flushList()
|
|
return sb.String()
|
|
}
|
|
|
|
func inlineContentFromRaw(raw any) []any {
|
|
switch v := raw.(type) {
|
|
case []any:
|
|
return v
|
|
case []map[string]any:
|
|
items := make([]any, 0, len(v))
|
|
for _, item := range v {
|
|
items = append(items, item)
|
|
}
|
|
return items
|
|
case string:
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
return []any{map[string]any{"type": "text", "text": v}}
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func blocksFromRaw(raw any) []any {
|
|
switch v := raw.(type) {
|
|
case []any:
|
|
return v
|
|
case []map[string]any:
|
|
items := make([]any, 0, len(v))
|
|
for _, item := range v {
|
|
items = append(items, item)
|
|
}
|
|
return items
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func renderBlock(block map[string]any) string {
|
|
blockType, _ := block["type"].(string)
|
|
props, _ := block["props"].(map[string]any)
|
|
content := inlineContentFromRaw(block["content"])
|
|
childrenHTML := renderChildren(block["children"])
|
|
|
|
var sb strings.Builder
|
|
|
|
switch blockType {
|
|
case "paragraph":
|
|
sb.WriteString("<p class=\"my-4\">")
|
|
sb.WriteString(renderInlineContent(content))
|
|
if childrenHTML != "" {
|
|
sb.WriteString(childrenHTML)
|
|
}
|
|
sb.WriteString("</p>\n")
|
|
|
|
case "heading":
|
|
level := 2
|
|
if l, ok := props["level"].(float64); ok {
|
|
level = int(l)
|
|
} else if l, ok := props["level"].(int); ok {
|
|
level = l
|
|
}
|
|
if level < 1 {
|
|
level = 1
|
|
}
|
|
if level > 6 {
|
|
level = 6
|
|
}
|
|
fmt.Fprintf(&sb, "<h%d class=\"mt-8 mb-4 font-bold\">", level)
|
|
sb.WriteString(renderInlineContent(content))
|
|
if childrenHTML != "" {
|
|
sb.WriteString(childrenHTML)
|
|
}
|
|
fmt.Fprintf(&sb, "</h%d>\n", level)
|
|
|
|
case "quote":
|
|
sb.WriteString("<blockquote class=\"my-4 border-l-4 border-border pl-4 text-muted-foreground\">")
|
|
sb.WriteString(renderInlineContent(content))
|
|
if childrenHTML != "" {
|
|
sb.WriteString(childrenHTML)
|
|
}
|
|
sb.WriteString("</blockquote>\n")
|
|
|
|
case "codeBlock":
|
|
lang := ""
|
|
if l, ok := props["language"].(string); ok {
|
|
lang = l
|
|
}
|
|
fmt.Fprintf(&sb, `<pre class="my-4"><code class="language-%s">`, html.EscapeString(lang))
|
|
sb.WriteString(renderInlineContent(content))
|
|
sb.WriteString("</code></pre>\n")
|
|
|
|
case "image":
|
|
url := ""
|
|
caption := ""
|
|
alt := ""
|
|
if u, ok := props["url"].(string); ok {
|
|
url = u
|
|
}
|
|
if c, ok := props["caption"].(string); ok {
|
|
caption = c
|
|
}
|
|
if a, ok := props["alt"].(string); ok {
|
|
alt = a
|
|
}
|
|
if alt == "" {
|
|
alt = caption
|
|
}
|
|
sb.WriteString(`<figure class="my-6">`)
|
|
fmt.Fprintf(&sb, `<img src="%s" alt="%s" />`, html.EscapeString(url), html.EscapeString(alt))
|
|
if caption != "" {
|
|
fmt.Fprintf(&sb, "<figcaption>%s</figcaption>", html.EscapeString(caption))
|
|
}
|
|
sb.WriteString("</figure>\n")
|
|
|
|
case "table":
|
|
sb.WriteString(`<div class="overflow-x-auto my-6"><table class="min-w-full border-collapse border border-border">`)
|
|
if tableContent, ok := block["content"].(map[string]any); ok {
|
|
if rows, ok := tableContent["rows"].([]any); ok && len(rows) > 0 {
|
|
renderTableRow := func(row any, isHeader bool) {
|
|
if rowMap, ok := row.(map[string]any); ok {
|
|
if cells, ok := rowMap["cells"].([]any); ok {
|
|
for _, cell := range cells {
|
|
cellTag := "td"
|
|
cellClass := "px-4 py-2 text-sm"
|
|
if isHeader {
|
|
cellTag = "th"
|
|
cellClass = "px-4 py-3 text-left text-sm font-semibold"
|
|
}
|
|
fmt.Fprintf(&sb, "<%s class=\"%s\">", cellTag, cellClass)
|
|
sb.WriteString(renderInlineContent(inlineContentFromRaw(cell)))
|
|
fmt.Fprintf(&sb, "</%s>", cellTag)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
sb.WriteString("<thead><tr class=\"bg-muted\">")
|
|
renderTableRow(rows[0], true)
|
|
sb.WriteString("</tr></thead>")
|
|
if len(rows) > 1 {
|
|
sb.WriteString("<tbody>")
|
|
for _, row := range rows[1:] {
|
|
sb.WriteString("<tr class=\"border-b border-border\">")
|
|
renderTableRow(row, false)
|
|
sb.WriteString("</tr>")
|
|
}
|
|
sb.WriteString("</tbody>")
|
|
}
|
|
}
|
|
}
|
|
sb.WriteString("</table></div>\n")
|
|
|
|
case "references":
|
|
sb.WriteString("<div class=\"bn-references\">\n")
|
|
sb.WriteString("<div class=\"bn-references-label\">References</div>\n")
|
|
sb.WriteString("<ol>\n")
|
|
if itemsJSON, ok := props["items"].(string); ok && itemsJSON != "" {
|
|
var items []map[string]string
|
|
if err := json.Unmarshal([]byte(itemsJSON), &items); err == nil {
|
|
for _, item := range items {
|
|
text := html.EscapeString(item["text"])
|
|
url := item["url"]
|
|
if text == "" {
|
|
continue
|
|
}
|
|
if url != "" {
|
|
fmt.Fprintf(&sb, "<li><a href=\"%s\">%s</a></li>\n", html.EscapeString(url), text)
|
|
} else {
|
|
fmt.Fprintf(&sb, "<li>%s</li>\n", text)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
sb.WriteString("</ol>\n</div>\n")
|
|
|
|
default:
|
|
if len(content) > 0 || childrenHTML != "" {
|
|
sb.WriteString("<div>")
|
|
sb.WriteString(renderInlineContent(content))
|
|
if childrenHTML != "" {
|
|
sb.WriteString(childrenHTML)
|
|
}
|
|
sb.WriteString("</div>\n")
|
|
}
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
func renderChildren(children any) string {
|
|
blocks := blocksFromRaw(children)
|
|
if len(blocks) == 0 {
|
|
return ""
|
|
}
|
|
return renderBlocks(blocks)
|
|
}
|
|
|
|
func renderInlineContent(content []any) string {
|
|
var sb strings.Builder
|
|
for _, item := range content {
|
|
itemMap, ok := item.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
itemType, _ := itemMap["type"].(string)
|
|
text, _ := itemMap["text"].(string)
|
|
styles, _ := itemMap["styles"].(map[string]any)
|
|
|
|
switch itemType {
|
|
case "text":
|
|
rendered := html.EscapeString(text)
|
|
if styles != nil {
|
|
if bold, ok := styles["bold"].(bool); ok && bold {
|
|
rendered = "<strong>" + rendered + "</strong>"
|
|
}
|
|
if italic, ok := styles["italic"].(bool); ok && italic {
|
|
rendered = "<em>" + rendered + "</em>"
|
|
}
|
|
if underline, ok := styles["underline"].(bool); ok && underline {
|
|
rendered = "<u>" + rendered + "</u>"
|
|
}
|
|
if strike, ok := styles["strike"].(bool); ok && strike {
|
|
rendered = "<s>" + rendered + "</s>"
|
|
}
|
|
if strike, ok := styles["strikethrough"].(bool); ok && strike {
|
|
rendered = "<s>" + rendered + "</s>"
|
|
}
|
|
if code, ok := styles["code"].(bool); ok && code {
|
|
rendered = "<code>" + rendered + "</code>"
|
|
}
|
|
}
|
|
sb.WriteString(rendered)
|
|
|
|
case "link":
|
|
href, _ := itemMap["href"].(string)
|
|
linkContent := inlineContentFromRaw(itemMap["content"])
|
|
if href == "" {
|
|
sb.WriteString(renderInlineContent(linkContent))
|
|
break
|
|
}
|
|
fmt.Fprintf(&sb, `<a href="%s">`, html.EscapeString(href))
|
|
sb.WriteString(renderInlineContent(linkContent))
|
|
sb.WriteString("</a>")
|
|
|
|
case "hardBreak":
|
|
sb.WriteString("<br />")
|
|
|
|
default:
|
|
if text != "" {
|
|
sb.WriteString(html.EscapeString(text))
|
|
break
|
|
}
|
|
if rawContent, ok := itemMap["content"]; ok {
|
|
sb.WriteString(renderInlineContent(inlineContentFromRaw(rawContent)))
|
|
}
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|