Examples
Each example below uses real exports and option names. Import @powerduck/md-editor/dist/style.css once before these snippets run.
1. Basic editor with all options
A vanilla JS MarkdownEditor in complex mode with every configurable option wired up:
import { MarkdownEditor, type MarkdownEditorOptions } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
const options: MarkdownEditorOptions = {
value: '# Hello\n\nStart writing here...',
mode: 'complex',
theme: 'light',
math: true,
mindmap: true,
codeHighlight: true,
tips: true,
preview: true,
autoPreview: true,
renderDebounce: false, // render synchronously for this demo
onChange: (value) => console.log('changed:', value.length, 'chars'),
onImageUpload: async (file) => {
const formData = new FormData();
formData.append('file', file);
const res = await fetch('/api/upload', { method: 'POST', body: formData });
const data = await res.json();
return data.url as string;
},
toolbar: ['heading', 'bold', 'italic', 'link', 'image', 'code'],
};
const editor = new MarkdownEditor('#editor', options);
document.querySelector('#save')?.addEventListener('click', () => {
console.log(editor.getValue());
});
2. React component with controlled value
import { useRef, useState } from 'react';
import { MarkdownEditorReact, type MarkdownEditorHandle } from '@powerduck/md-editor/react';
import '@powerduck/md-editor/dist/style.css';
function NoteEditor() {
const [value, setValue] = useState('# Draft\n\nWrite something...');
const handleRef = useRef<MarkdownEditorHandle>(null);
return (
<div>
<button onClick={() => handleRef.current?.getHtml()}>Render HTML</button>
<MarkdownEditorReact
ref={handleRef}
value={value}
onChange={setValue}
mode="complex"
theme="light"
/>
</div>
);
}
export default NoteEditor;
value and onChange are excluded from the recreate path, so typing does not tear down the CodeMirror instance.
3. Custom toolbar (array and record forms)
import { MarkdownEditor } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
// Array form: an include-list. Only these actions appear, in this order.
new MarkdownEditor('#editor-array', {
mode: 'complex',
toolbar: ['bold', 'italic', 'link', 'code', 'math'],
});
// Record form: per-item overrides. Everything not listed stays visible.
new MarkdownEditor('#editor-record', {
mode: 'complex',
toolbar: {
bold: { label: 'Make bold' },
image: { show: false },
youtube: { show: false },
theme: { icon: '<svg>...</svg>' },
},
});
Valid action names are the 22 listed in Features.
4. Image upload integration
import { MarkdownEditor } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
const editor = new MarkdownEditor('#editor', {
mode: 'complex',
onImageUpload: async (file: File): Promise<string> => {
const form = new FormData();
form.append('file', file);
const res = await fetch('/api/upload', { method: 'POST', body: form });
if (!res.ok) throw new Error('Upload failed');
const { url } = await res.json();
return url; // inserted as 
},
});
Paste or drop an image file into the editor; the hook is invoked automatically. The resulting markdown is inserted at the cursor.
5. @mention integration
import { MarkdownEditor, type MentionItem } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
const USERS: MentionItem[] = [
{ id: '1', label: 'Alice', avatar: 'https://example.com/a.png', description: 'alice@example.com' },
{ id: '2', label: 'Bob', description: 'bob@example.com' },
];
new MarkdownEditor('#editor', {
mode: 'complex',
mention: {
onMentionSearch: async (query) =>
USERS.filter((u) => u.label.toLowerCase().includes(query.toLowerCase())),
onMentionSelect: (item) => `[@${item.label}](mention:${item.id})`,
minChars: 0,
maxItems: 8,
},
});
Typing @ opens the dropdown. Selecting an item inserts the [@label](mention:id) text, which renders as a styled badge in the preview.
6. Standalone renderMarkdown usage
import { renderMarkdown } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
const source = [
'# Hello',
'',
'Inline: $E=mc^2$.',
'',
'```ts',
'const x: number = 1;',
'```',
'',
':::tip',
'Callout block.',
':::',
].join('\n');
const html = renderMarkdown(source, { math: true, tips: true });
document.getElementById('output')!.innerHTML = html;
renderMarkdown returns a string and does not mount an editor. Mindmap placeholders are emitted as inert <div>s; call Renderer.hydrate yourself if you need live diagrams outside the editor.
7. Simple mode (no toolbar)
import { MarkdownEditor } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
const editor = new MarkdownEditor('#editor', {
value: '# Minimal editor\n\nNo toolbar, no status bar.',
mode: 'simple', // default; pure edit + preview
theme: 'dark',
preview: true,
});
In simple mode there is no toolbar, no status bar, and no line numbers. You can still switch to complex at runtime with editor.setMode('complex').
Next steps
- API reference — every method signature.
- Configuration — option defaults.