使用例
以下の例では、実際に公開されているエクスポートとオプション名を使っています。実行前に @powerduck/md-editor/dist/style.css を一度インポートしてください。参照先のコンテナー要素をページに用意し、アップロードの例には独自のアップロードエンドポイントを実装してください。
1. よく使うオプションを設定した基本エディター
素の JS で complex モードの MarkdownEditor を作成し、よく使うオプションを設定します。
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 });
if (!res.ok) throw new Error('Upload failed');
const data = await res.json();
return data.url as string;
},
toolbar: ['heading', 'bold', 'italic', 'link', 'image', 'code'],
};
const editor = new MarkdownEditor('#editor', options);
// Call editor.destroy() when removing this editor from the page.
document.querySelector('#save')?.addEventListener('click', () => {
console.log(editor.getValue());
});