用于在浏览器中把结构化笔记数据导出为 PNG 图片的工具包。
这个包只做一件事:在浏览器里把“笔记形态的数据”渲染成一张可导出的 PNG 图片。
当前包含这些能力:
- 离屏渲染带独立样式的笔记卡片
- 基于
html-to-image输出 PNG - 优先复制到剪贴板,失败时自动降级为下载
- 支持清单、标签、表格、代码块和图片网格
- 通过注入函数适配你自己的富文本渲染和图片地址解析逻辑
- 已整理到可单独发布 npm 包的状态
- 当前包名为
@chenkunqing/note-image-export package.json的仓库信息已指向当前 monorepo 中的packages/note-image-export- 如果之后把这个目录拆到独立仓库,需要同步更新
repository、homepage和bugs
npm install @chenkunqing/note-image-export html-to-image为什么 html-to-image 需要单独安装:
- 这个包在运行时依赖它
- 它被声明为
peerDependencies,这样接入方可以自己控制版本
import { exportNoteAsImage } from '@chenkunqing/note-image-export';
await exportNoteAsImage(
{
title: '周复盘',
content: '<p>把包发出去。</p>',
type: 'text',
updatedAt: new Date().toISOString(),
labels: [{ name: '工作' }],
},
{
filename: 'weekly-review',
renderContent: (content) => content,
footerSource: 'My App',
},
);这个方法会先渲染笔记,然后:
- 优先复制为图片到剪贴板
- 如果当前环境不支持,就自动下载为
.png
常用选项:
filename:导出文件名renderContent:把你项目里的富文本内容渲染成 HTMLresolveImageSrc:把你自己的图片对象解析成浏览器可访问的地址updatedAtLabel:头部更新时间标签文案exportedAtLabel:底部导出时间标签文案footerSource:底部来源或产品名preferClipboard:传false时直接走下载
和上面的视觉渲染一致,但返回的是 PNG Blob,不直接复制也不直接下载。
适合这些场景:
- 你想自己上传导出的图片
- 你要接入自定义分享流程
- 你要做二次处理后再保存
type NoteExportData = {
title?: string;
content?: string;
type: 'text' | 'checklist';
color?: string;
updatedAt?: string;
checklistItems?: Array<{ content: string; isChecked: boolean }>;
labels?: Array<{ name: string }>;
images?: Array<{
filename?: string;
localUrl?: string;
src?: string;
alt?: string;
}>;
};说明:
content只在type: 'text'时使用checklistItems只在type: 'checklist'时使用images可以直接提供 URL,也可以通过resolveImageSrc从业务对象转换
type ExportNoteImageOptions = {
filename?: string;
width?: number;
baseFontSize?: number;
pixelRatio?: number;
backgroundColor?: string;
updatedAtLabel?: string;
exportedAtLabel?: string;
footerSource?: string;
preferClipboard?: boolean;
skipFonts?: boolean;
imagePlaceholder?: string;
formatDate?: (iso: string) => string;
isMobile?: () => boolean;
renderContent?: (content: string, note: NoteExportData) => string;
resolveImageSrc?: (image: NoteExportImage, note: NoteExportData) => string | null | undefined;
};- 这是一个纯浏览器侧包,运行时需要
window、document,如果要复制到剪贴板还需要可用的 Clipboard API。 - 富文本渲染没有写死在包里,应用可以通过
renderContent注入自己的 HTML 或 Markdown 渲染逻辑。 - 图片地址解析同样可以通过
resolveImageSrc自定义。 - 如果剪贴板复制不可用,会自动降级为下载。
- 这个包提供的是“笔记导出”场景下的默认视觉样式,但不会耦合 React、Vite、Zustand、Tiptap 或后端接口。
npm install
npm run build
npm run check
npm run pack:preview- 更新
package.json里的version。 - 确认
name、repository、homepage、bugs指向正确仓库。 - 运行
npm run prepublishOnly。 - 使用
npm publish --access public发布。
这个包目前不包含:
- React 组件
- 编辑器集成
- 状态管理
- 上传接口
- 笔记持久化
这些内容应该继续留在接入方应用里,通过 renderContent 和 resolveImageSrc 这类适配入口完成衔接。