首页/文档/块类型

块类型与数据结构

13 种块的字段定义、JSON 形态,以及在不同端上如何渲染。

更新于 2026-11-26 适用版本 v1.0.0 约 5 分钟读完

所有内容都是块。这一页把 13 种块的字段、约束和 JSON 形态一次讲清楚——照着这份约定,你在任何语言里都能渲染同一篇文章。

文档结构

{
  "id": "d1a2b3c",
  "title": "文章标题",
  "blocks": [ { "id": "b1", "type": "paragraph", "text": "…" } ],
  "createdAt": "2026-11-25T09:12:00.000Z",
  "updatedAt": "2026-11-25T09:40:31.000Z"
}

id 在文档内唯一,可用作前端列表的 key 或页面锚点;updatedAt 是 ISO 时间字符串。

13 种块

type名称字段说明
paragraph段落text正文,允许受控内联 HTML
heading1大标题text对应 H1,一般一篇文章一个
heading2中标题text对应 H2
heading3小标题text对应 H3
quote引用text引述、金句,可含 <br>
ul无序列表items[]每项 text + depth
ol有序列表items[]编号按顺序自动生成,不存序号
todo待办清单items[]额外带 checked 布尔值
code代码块lang code纯文本,不做 HTML 解析
image图片src caption width altsrc 可为 URL 或 data URL
callout提示框emoji text强调说明,带图标
table表格rows[][]二维字符串数组,首行视为表头
divider分割线无纯视觉分隔

各类型 JSON 示例

文本类

{ "type": "paragraph", "text": "这是一段加粗与 inline code 混排的正文。" }
{ "type": "heading2", "text": "二级标题" }
{ "type": "quote",    "text": "好的编辑器不该让人感觉到编辑器的存在。" }
{ "type": "code",     "lang": "javascript", "code": "const a = 1;\nconsole.log(a);" }

列表与待办

{
  "type": "ul",
  "items": [
    { "text": "第一项", "depth": 0 },
    { "text": "缩进一层", "depth": 1 },
    { "text": "再缩一层", "depth": 2 }
  ]
}
{
  "type": "todo",
  "items": [
    { "text": "写完初稿", "depth": 0, "checked": true },
    { "text": "配三张图", "depth": 0, "checked": false }
  ]
}

depth 取值 0 / 1 / 2,对应 0 / 24 / 48 像素缩进。有序列表不保存序号,按数组顺序渲染即可。

图片、提示框、表格

{
  "type": "image",
  "src": "https://editor.zancms.cn/uploads/a.webp",
  "caption": "编辑器工作台",
  "alt": "ZanCMS Editor 截图",
  "width": 75
}
{ "type": "callout", "emoji": "💡", "text": "上传的图片会在本地压缩后存进草稿。" }
{
  "type": "table",
  "rows": [
    ["能力", "说明", "状态"],
    ["块编辑", "13 种块类型", "已上线"],
    ["导出", "HTML / Markdown / JSON", "已上线"]
  ]
}

受控内联 HTML

所有文本字段里的 text 都是一段受控的内联 HTML:编辑器只会写入下面这些标签,渲染时可以直接插入 innerHTML,不需要再转义。

<b> <strong> <i> <em> <u> <s> <code> <mark> <a href="…"> <br>
安全提醒:如果内容来自多用户投稿,入库前请在你的服务端再做一次白名单过滤(例如 PHP 的 strip_tags($text, '<b><strong><i><em><u><s><code><mark><a><br>')),编辑器本身不做可信边界。

前端渲染建议

window.ZanEditor.toHTML() 已经能把块转成语义化 HTML;如果你在自己的框架里渲染,按类型分支即可:

function renderBlock(b) {
  switch (b.type) {
    case 'paragraph': return '<p>' + b.text + '</p>';
    case 'heading2':  return '<h2>' + b.text + '</h2>';
    case 'ul':        return '<ul>' + b.items.map(i =>
                        '<li style="margin-left:' + i.depth * 24 + 'px">' + i.text + '</li>').join('') + '</ul>';
    case 'code':      return '<pre><code>' + escapeHtml(b.code) + '</code></pre>';
    case 'image':     return '<figure><img src="' + b.src + '"><figcaption>' + (b.caption || '') + '</figcaption></figure>';
    case 'divider':   return '<hr>';
    default:          return '';
  }
}

注意 code 字段要转义后输出,table 的单元格同样按纯文本处理。