Skip to main content

Usage

This page covers the basic API for embedding ToastWrite in a web page.

Minimal example

HTML:

<div id="editor-root"></div>
<div id="viewer-root"></div>

JavaScript:

import { Editor, Viewer } from '@toastwrite/editor';
import '@toastwrite/editor/style.css';

const editorRoot = document.querySelector('#editor-root') as HTMLElement;
const viewerRoot = document.querySelector('#viewer-root') as HTMLElement;

const viewer = new Viewer({
el: viewerRoot,
initialValue: '# Hello\n\nStart writing **Markdown**.',
height: '400px',
});

const editor = new Editor({
el: editorRoot,
initialValue: '# Hello\n\nStart writing **Markdown**.',
height: '400px',
previewStyle: 'vertical',
previewSplitter: true,
events: {
change: () => {
viewer.setMarkdown(editor.getMarkdown());
},
},
});

Factory helper

Use Editor.factory to create either an editor or viewer from one options object:

import Editor from '@toastwrite/editor';

const editor = Editor.factory({
el: document.querySelector('#root')!,
viewer: false, // set to true for Viewer
});

Editor options

OptionTypeDefaultDescription
elHTMLElementRequired. Container element
initialValuestring''Initial Markdown content
heightstring'300px'Editor height
minHeightstring'200px'Minimum height
placeholderstring''Placeholder text
previewStyle'tab' | 'vertical''vertical'Preview layout
previewSplitterbooleanfalseDraggable split between editor and preview
scrollSyncbooleantrueSync scroll between editor and preview
hideToolbarbooleanfalseHide the formatting toolbar
toolbarItemsCommandId[]all built-insCustomize toolbar buttons
theme'light' | 'dark''light'Color theme
frontMatterbooleanfalseParse YAML/TOML/JSON front matter
referenceDefinitionbooleanfalseEnable reference-style links
pluginsEditorPlugin[][]Editor plugins
eventsobjectEvent handlers (see below)
beforePreviewRender(html) => htmlHook to transform preview HTML

Viewer options

The viewer accepts the same base options as the editor, except preview-related settings (previewStyle, hideToolbar, etc.) are not used.

Public API

Editor

MethodDescription
getMarkdown()Get current Markdown source
setMarkdown(markdown, cursorToEnd?)Replace content
getHTML()Get rendered preview HTML
getSelection() / setSelection()Get or set cursor/selection range
executeCommand(commandId)Run a toolbar command programmatically
on(event, handler) / off(event, handler)Subscribe to events
destroy()Tear down the instance

Viewer

MethodDescription
getMarkdown()Get current Markdown source
setMarkdown(markdown)Replace content and re-render
on(event, handler) / off(event, handler)Subscribe to events
destroy()Tear down the instance

Events

EventPayloadDescription
loadFired when the instance is ready
changeContent changed
focus / blurFocus state changed
caretChangeSelection/caret moved
beforePreviewRenderstring (HTML)Before preview HTML is applied

Toolbar commands

Built-in toolbar commands include bold, italic, strikethrough, headings, lists, task lists, blockquotes, horizontal rules, links, tables, inline code, and code blocks. Keyboard shortcuts are available for most commands (for example Mod-b for bold, Mod-Shift-k for links).

Customize the toolbar with toolbarItems:

new Editor({
el: root,
toolbarItems: ['bold', 'italic', 'link', 'codeBlock'],
});
new Editor({
el: root,
frontMatter: true,
referenceDefinition: true,
initialValue: `---
title: My Document
---

[link][ref]

[ref]: https://example.com
`,
});

See Front Matter and Reference Definitions for syntax details.

Cleanup

Always call destroy() when removing the editor from the DOM (for example in a SPA route change):

editor.destroy();
viewer.destroy();

Learn more

Browse Markdown Syntax for every supported construct and how to write it.