Documentation

HTML and JSON export

Save editable HTML and read structured JSON.

Try it

Click Export. The callback receives html containing <p><strong>Ship it</strong></p> and a JSON document whose text node carries a bold mark. Use onChange and onChangeJSON to observe updates between saves.

Live example
Example result

Add it to your editor

Use getHTML() when your app needs to save and reopen a document. Use getJSON() for a structured representation such as a content preview or indexing.

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>Ship it</strong></p>",
  });

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel="Editor" />
      <button type="button" onClick={() => onSave({
        html: editor.getHTML(),
        json: editor.getJSON(),
      })}>
        Export
      </button>
    </EditorProvider>
  );
}

Behavior to account for

Treat JSON results as immutable. The public editor loads HTML, so save the HTML if the document will reopen. See Save and restore HTML for loading a saved draft.

Features