# 导出 HTML 和 JSON {#html-and-json}

Source: [从 React 编辑器导出 HTML 和 JSON · Yurich](https://leeyujoo.com/yurich/zh-cn/features/html-and-json/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

保存可编辑的 HTML 并读取结构化 JSON。

## 试一试 {#try-it}

点击 **导出**。回调会收到 HTML 和带 `bold` 标记的 JSON。

## 添加到编辑器 {#setup}

通过 `getHTML()` 保存可重新打开的文档，用 `getJSON()` 读取结构化输出。

```tsx
import {
  Bold, EditorContent, EditorProvider, useEditor, type DocumentJSON,
} from 'yurich';

export function ExportEditor({ onSave }: {
  onSave: (draft: { html: string; json: DocumentJSON }) => void;
}) {
  const editor = useEditor({
    extensions: [Bold],
    initialValue: "<p><strong>可以发布了</strong></p>",
  });

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel="编辑器" />
      <button type="button" onClick={() => onSave({
        html: editor.getHTML(),
        json: editor.getJSON(),
      })}>
        导出
      </button>
    </EditorProvider>
  );
}
```

## 行为说明 {#behavior}

将 JSON 视为不可变。载入文档请用 HTML，见[保存和恢复](https://leeyujoo.com/yurich/zh-cn/#save-and-restore-html)。

[功能](https://leeyujoo.com/yurich/zh-cn/features/)
