範例
以下範例使用實際的匯出與選項名稱。執行前請統一匯入一次 @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());
});
2. 受控值 React 元件
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 與 onChange 不會觸發實例重建,因此輸入時不會銷毀 CodeMirror 實例。
3. 自訂工具列(陣列與對應表形式)
import { MarkdownEditor } from '@powerduck/md-editor';
import '@powerduck/md-editor/dist/style.css';
// Array form: an include-list. Actions retain the built-in toolbar 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>' },
},
});
有效的操作名稱為功能中列出的 22 項。
4. 整合圖片上傳
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 
},
});
將圖片檔案貼上或拖入編輯器時,會自動呼叫上傳掛鉤,產生的 Markdown 將插入游標位置。
5. 整合 @mention
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,
},
});
輸入 @ 會開啟下拉清單。選取項目後插入 [@label](mention:id) 文字,並在預覽中顯示為帶有樣式的徽章。
6. 單獨使用 renderMarkdown
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 回傳字串,不會掛載編輯器。心智圖以尚未啟用的 <div> 預留位置輸出;若要在編輯器外顯示互動圖形,請自行呼叫 Renderer.hydrate。
7. 簡潔模式(無工具列)
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,
});
simple 模式沒有工具列、狀態列與行號。仍可在執行階段透過 editor.setMode('complex') 切換為 complex 模式。