@editkit/export
v2.0.4
Published
Document import and export adapters for EditKit with Word and Google Docs paste normalization — Markdown, clean/email HTML, plain text, JSON, DOCX, and PDF.
Maintainers
Readme
@editkit/export
Import and export helpers for EditKit Text Editor. Convert editor HTML to Markdown, clean HTML, email-safe HTML, plain text, JSON, DOCX, or PDF, and normalize HTML pasted from Microsoft Word and Google Docs.
Install
pnpm add @editkit/export
# or
npm install @editkit/exportThe package has no required runtime dependency. It expects a DOM Window when used outside a browser or in SSR/tests.
Quick start
import { toMarkdown, toCleanHtml, toPlainText } from '@editkit/export';
const html = editor.getHTML();
const markdown = toMarkdown(html);
const cleanHtml = toCleanHtml(html);
const plainText = toPlainText(html);API
ExportOptions
All HTML conversion functions accept an optional options object:
interface ExportOptions {
/** DOM window used to parse HTML. Required in SSR/Node. */
window?: Window;
/** Sanitize HTML before parsing or before returning generated HTML. */
sanitize?: (html: string) => string;
/** Keep inline styles for clean HTML output. */
inlineStyles?: boolean;
}With EditKit, pass the editor sanitizer at the export boundary:
const options = { sanitize: editor.sanitize };
const markdown = toMarkdown(editor.getHTML(), options);
const cleanHtml = toCleanHtml(editor.getHTML(), options);Markdown
import { toMarkdown, fromMarkdown } from '@editkit/export';
const markdown = toMarkdown(html, options);
const htmlAgain = fromMarkdown(markdown, options);The converters support headings, paragraphs, emphasis, links, images, code blocks, blockquotes, horizontal rules, ordered/unordered/task lists, tables, and EditKit image/panel blocks.
HTML and plain text
import { toCleanHtml, toEmailHtml, toPlainText } from '@editkit/export';
const htmlForStorage = toCleanHtml(html, { inlineStyles: false });
const htmlForEmail = toEmailHtml(html);
const text = toPlainText(html);toCleanHtml() removes editor-only controls, transient attributes, editing classes, and (unless enabled) inline styles. toEmailHtml() removes SVG and converts EditKit layout/button blocks to email-compatible tables.
JSON editor documents
import { editorToJSON, editorFromJSON } from '@editkit/export';
const document = editorToJSON(editor);
editorFromJSON(editor, document);These helpers delegate to the editor's existing getJSON() and setJSON() methods.
Word and Google Docs HTML
import { normalizeWordHtml } from '@editkit/export';
const normalized = normalizeWordHtml(pastedHtml, {
sanitize: editor.sanitize,
});This removes common Microsoft Office mso-* styles and Google Docs internal wrapper elements while preserving content.
DOCX export
DOCX support is optional:
pnpm add docximport { exportDocx } from '@editkit/export';
const result = await exportDocx(editor.getHTML(), {
sanitize: editor.sanitize,
filename: 'article.docx',
});
// result.blob is the generated file; result.filename is its name.If docx is not installed, exportDocx() throws an actionable error when called.
PDF export
Use the browser print dialog for a zero-dependency PDF flow:
import { printPdf } from '@editkit/export';
printPdf(editor.getHTML(), { sanitize: editor.sanitize });For a PDF library or server-side renderer, provide an adapter returning a Blob:
import { exportPdfViaAdapter } from '@editkit/export';
const result = await exportPdfViaAdapter(
editor.getHTML(),
safeHtml => myPdfAdapter.render(safeHtml),
{ sanitize: editor.sanitize, filename: 'article.pdf' },
);SSR and tests
Pass a DOM window explicitly when no global browser window exists:
const markdown = toMarkdown(html, { window: jsdomWindow });Do not pass unsanitized user-controlled HTML to a DOM sink. Prefer editor.sanitize or another trusted sanitizer before exporting or rendering generated HTML.
Related packages
editkit-text-editor— umbrella package@editkit/core— framework-independent editor engine@editkit/react— React and Next.js integration@editkit/vue— Vue and Nuxt integration@editkit/svelte— Svelte and SvelteKit integration
License
MIT © Ashikur Rahman
