关于 json-render-image
这是一个使用 Satori 将 json-render 规格转换为 SVG 或 PNG 图片的渲染器。它包含标准布局和内容组件,也支持添加自定义目录组件。
主要功能
- 01将规格渲染为 SVG 字符串
- 02将规格渲染为 PNG 缓冲区
- 03提供标准布局和内容组件
- 04支持添加自定义目录组件
- 05支持服务端安全的模式和目录导入
适用场景
- 01从 JSON 规格生成 SVG 图片
- 02将声明式布局输出为 PNG
- 03用自定义组件扩展图片目录
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ vercel-labs/ json-render --skill json-render-image安装后在 Claude Code 中输入/json-render-image确认已加载。
详细介绍
@json-render/image
使用 Satori 将 JSON 规范转换为 SVG 和 PNG 图片的渲染器。
快速开始
import { renderToPng } from "@json-render/image/render";
import type { Spec } from "@json-render/core";
const spec: Spec = {
root: "frame",
elements: {
frame: {
type: "Frame",
props: { width: 1200, height: 630, backgroundColor: "#1a1a2e" },
children: ["heading"],
},
heading: {
type: "Heading",
props: { text: "Hello World", level: "h1", color: "#ffffff" },
children: [],
},
},
};
const png = await renderToPng(spec, {
fonts: [{ name: "Inter", data: fontData, weight: 400, style: "normal" }],
});
使用标准组件
import { defineCatalog } from "@json-render/core";
import { schema, standardComponentDefinitions } from "@json-render/image";
export const imageCatalog = defineCatalog(schema, {
components: standardComponentDefinitions,
});
添加自定义组件
import { z } from "zod";
const catalog = defineCatalog(schema, {
components: {
...standardComponentDefinitions,
Badge: {
props: z.object({ label: z.string(), color: z.string().nullable() }),
slots: [],
description: "A colored badge label",
},
},
});
标准组件
| 组件 | 类别 | 描述 |
|---|---|---|
Frame | 根组件 | 根容器。定义宽度、高度和背景。必须位于根节点。 |
Box | 布局 | 带有内边距、外边距、边框和绝对定位的容器 |
Row | 布局 | 水平弹性布局 |
Column | 布局 | 垂直弹性布局 |
Heading | 内容 | h1-h4 标题文本 |
Text | 内容 | 带完整样式的正文文本 |
Image | 内容 | 来自 URL 的图片 |
Divider | 装饰 | 水平分隔线 |
Spacer | 装饰 | 空白垂直间距 |
主要导出项
| 导出项 | 用途 |
|---|---|
renderToSvg | 将规范渲染为 SVG 字符串 |
renderToPng | 将规范渲染为 PNG 缓冲区(需要 @resvg/resvg-js) |
schema | 图片元素的架构 |
standardComponents | 预构建的组件注册表 |
standardComponentDefinitions | 用于 AI 提示词的目录定义 |
子路径导出
| 导出项 | 描述 |
|---|---|
@json-render/image | 完整包:架构、组件和渲染函数 |
@json-render/image/server | 仅包含架构和目录定义(不包含 React/Satori) |
@json-render/image/catalog | 标准组件定义和类型 |
@json-render/image/render | 仅包含渲染函数 |
评论
支持 Markdown