Core plugin system and editor functionality for the Plate rich text editor framework
—
Support for multiple serialization formats including HTML, AST fragments, and custom format parsing with extensible parser system.
Handle Slate AST fragment format for clipboard and drag-and-drop operations.
/**
* AST plugin for Slate fragment handling
*/
const AstPlugin: SlatePlugin;
/**
* Serialize nodes to AST fragment format
* @param editor - Editor instance
* @param nodes - Nodes to serialize
* @returns AST fragment string
*/
function serializeAst(
editor: SlateEditor,
nodes: any[]
): string;
/**
* Deserialize AST fragment to nodes
* @param editor - Editor instance
* @param fragment - AST fragment string
* @returns Array of deserialized nodes
*/
function deserializeAst(
editor: SlateEditor,
fragment: string
): any[];Usage Examples:
import { createSlateEditor, AstPlugin, serializeAst, deserializeAst } from "@platejs/core";
const editor = createSlateEditor({
plugins: [AstPlugin]
});
// Serialize current selection to AST
const selectedNodes = editor.getFragment();
const astFragment = serializeAst(editor, selectedNodes);
// Store fragment for later use
localStorage.setItem('clipboard', astFragment);
// Restore from AST fragment
const storedFragment = localStorage.getItem('clipboard');
const nodes = deserializeAst(editor, storedFragment);
editor.insertFragment(nodes);Extensible parser system for handling various data formats.
/**
* Parser plugin for custom format handling
*/
const ParserPlugin: SlatePlugin;
/**
* Parser configuration interface
*/
interface ParserOptions {
/** Format identifier */
format: string;
/** MIME type for format */
mimeType: string;
/** Deserialization function */
deserialize: (data: string) => any[];
/** Serialization function */
serialize?: (nodes: any[]) => string;
/** Format priority */
priority?: number;
}
/**
* Register a custom parser
* @param options - Parser configuration
*/
function registerParser(options: ParserOptions): void;
/**
* Get registered parser by format
* @param format - Format identifier
* @returns Parser configuration
*/
function getParser(format: string): ParserOptions | null;
/**
* Parse data using registered parsers
* @param editor - Editor instance
* @param data - Data to parse
* @param format - Format hint
* @returns Parsed nodes
*/
function parseData(
editor: SlateEditor,
data: string,
format?: string
): any[];Usage Examples:
import { registerParser, parseData } from "@platejs/core";
// Register custom Markdown parser
registerParser({
format: 'markdown',
mimeType: 'text/markdown',
deserialize: (markdown: string) => {
// Custom markdown parsing logic
return parseMarkdownToSlate(markdown);
},
serialize: (nodes: any[]) => {
// Custom Slate to markdown conversion
return convertSlateToMarkdown(nodes);
}
});
// Register CSV parser
registerParser({
format: 'csv',
mimeType: 'text/csv',
deserialize: (csv: string) => {
const rows = csv.split('\n').map(row => row.split(','));
return [{
type: 'table',
children: rows.map(row => ({
type: 'tr',
children: row.map(cell => ({
type: 'td',
children: [{ text: cell }]
}))
}))
}];
}
});
// Use parsers
const markdownData = "# Heading\n\nParagraph with **bold** text.";
const nodes = parseData(editor, markdownData, 'markdown');
editor.insertFragment(nodes);Handle clipboard operations with multiple format support.
/**
* Clipboard data interface
*/
interface ClipboardData {
/** Plain text representation */
text: string;
/** HTML representation */
html?: string;
/** AST fragment */
fragment?: string;
/** Custom format data */
[format: string]: string;
}
/**
* Write data to clipboard with multiple formats
* @param editor - Editor instance
* @param data - Data to write
*/
function writeClipboard(
editor: SlateEditor,
data: ClipboardData
): Promise<void>;
/**
* Read data from clipboard with format detection
* @param editor - Editor instance
* @returns Clipboard data with detected formats
*/
function readClipboard(
editor: SlateEditor
): Promise<ClipboardData>;Multi-format support for drag and drop operations.
/**
* Drag data interface
*/
interface DragData {
/** Data transfer object */
dataTransfer: DataTransfer;
/** Supported formats */
formats: string[];
/** Drag source information */
source?: {
editor: SlateEditor;
nodes: any[];
};
}
/**
* Handle drop event with format detection
* @param editor - Editor instance
* @param event - Drop event
* @returns Processing result
*/
function handleDrop(
editor: SlateEditor,
event: DragEvent
): Promise<boolean>;
/**
* Prepare drag data with multiple formats
* @param editor - Editor instance
* @param nodes - Nodes to drag
* @returns Prepared drag data
*/
function prepareDragData(
editor: SlateEditor,
nodes: any[]
): DragData;Automatic format detection for unknown data.
/**
* Detect data format from content
* @param data - Data to analyze
* @param mimeType - Optional MIME type hint
* @returns Detected format or null
*/
function detectFormat(
data: string,
mimeType?: string
): string | null;
/**
* Format detection rules
*/
interface FormatDetectionRule {
/** Format identifier */
format: string;
/** Detection function */
detect: (data: string, mimeType?: string) => boolean;
/** Detection priority */
priority: number;
}
/**
* Register format detection rule
* @param rule - Detection rule
*/
function registerFormatDetection(rule: FormatDetectionRule): void;Comprehensive export and import system with format conversion.
/**
* Export editor content to specified format
* @param editor - Editor instance
* @param format - Target format
* @param options - Export options
* @returns Exported data
*/
function exportContent(
editor: SlateEditor,
format: string,
options?: {
nodes?: any[];
pretty?: boolean;
metadata?: Record<string, any>;
}
): string;
/**
* Import content from specified format
* @param editor - Editor instance
* @param data - Data to import
* @param format - Source format
* @param options - Import options
* @returns Imported nodes
*/
function importContent(
editor: SlateEditor,
data: string,
format: string,
options?: {
replace?: boolean;
position?: number[];
normalize?: boolean;
}
): any[];Usage Examples:
import { exportContent, importContent, detectFormat } from "@platejs/core";
// Export to different formats
const htmlExport = exportContent(editor, 'html', { pretty: true });
const markdownExport = exportContent(editor, 'markdown');
const astExport = exportContent(editor, 'ast');
// Import with format detection
const unknownData = `# Title\n\nSome content...`;
const detectedFormat = detectFormat(unknownData);
if (detectedFormat) {
const importedNodes = importContent(editor, unknownData, detectedFormat);
editor.insertFragment(importedNodes);
}
// Batch export for multiple formats
const exports = {
html: exportContent(editor, 'html'),
markdown: exportContent(editor, 'markdown'),
plain: exportContent(editor, 'text')
};Built-in format support includes:
/** Built-in formats */
const SUPPORTED_FORMATS = {
HTML: 'html',
AST: 'ast',
TEXT: 'text',
JSON: 'json'
} as const;
/** Format MIME types */
const FORMAT_MIME_TYPES = {
'html': 'text/html',
'ast': 'application/x-slate-fragment',
'text': 'text/plain',
'json': 'application/json'
} as const;Install with Tessl CLI
npx tessl i tessl/npm-platejs--core