Markdown shortcuts
The markdown shortcuts plugin converts a block when you type a prefix such as "# " or "- " at its start, and formats text typed as **bold**, *italic*, `code` or ~strike~, as in Notion.
createMarkdownShortcutsPlugin() brings Notion’s markdown as you type. A prefix at the start of a block converts it: # makes a heading, - a bulleted list item, ``` a code block. The prefixes come from the markdown field of each block kind’s presets, so the plugin has no list of its own. Inline, **bold**, *italic* and the others apply their mark when you type the closing marker.
<script lang="ts">
import { Edytor, createMarkdownShortcutsPlugin, createCodePlugin } from 'edytor';
</script>
<Edytor plugins={[createCodePlugin(), createMarkdownShortcutsPlugin()]} />
It takes no options. It is two input rules, one for the prefixes and one for the inline marks; a plugin of your own adds others the same way (:smile: → 😄).
Try it
In the empty line, type # , - , 1. , [] , > or ---, then try **bold** or `code` in a sentence. Mod+Z gives the typed text back. The editor is local to this page: a reload resets it.
Prefixes
With the rich text and code plugins, the prefixes are Notion’s:
| Type | Converts to |
|---|---|
# |
Heading 1 |
## |
Heading 2 |
### |
Heading 3 |
- , * or + |
Bulleted list item |
1. , a. or i. |
Numbered list item |
[] or [ ] |
To-do |
" |
Quote |
> |
Toggle |
> in a heading |
Toggle heading of the heading’s level |
# , ## , ### in a toggle or toggle heading |
Toggle heading 1, 2, 3 |
--- |
Divider |
``` |
Code block (code plugin) |
How it triggers
A conversion happens when the character you type completes a prefix, and all of these hold:
- the caret is collapsed in the block’s first text;
- the text from the block’s start to the caret plus the typed character equals the prefix exactly (so
#works at the start of a block, not after other words); - the block is convertible (not a void or island block, and not inside one); in a list’s item, the new kind takes the item out of the list (Containers);
- the typed input is one character.
Removing the prefix and converting the block are one operation (setBlock, with a deleteText step for the prefix): a plugin that refuses either refuses both. When the conversion is refused, the character is inserted as normal text instead. The conversion is its own undo step, separate from the typing before it: undo after ## leaves a paragraph ## , the typed space kept. After the conversion the caret is at the start of the converted block, or of its first child when the kind starts with one (a code block’s first line).
As in Notion, text after the caret is kept: typing - at the start of hello makes a bulleted item hello, with the caret before it.
A kind with an empty shape, such as the divider, replaces the block’s content and children, so its prefix converts only a block that holds nothing but the prefix: --- typed before hello stays text. Other kinds keep them.
A kind that renders no content, such as the divider, cannot hold the caret: the conversion also adds a fresh block of the default kind where the divider lands after it, in the same step, and the caret lands there. In a list’s empty item, both go out of the list, which splits around them (--- in an item gives a divider and a paragraph between the list’s two halves). Undo removes both.
Inline markdown
Typing the closing marker after marked-up text applies the mark and removes both markers:
| Type | Mark |
|---|---|
**bold** |
bold |
*italic* or _italic_ |
italic |
`code` |
code |
~strike~ or ~~strike~~ |
strike |
- The text between the markers must be non-empty and not start or end with a space:
* a*and** **stay text. - Removing the markers and applying the mark are one undo step, separate from the typing before it; undo brings the markers back as text, as typed (
say **b**undoes tosay **b**). The step runs as aninlineMarkdownoperation, visible toonBeforeOperation. A plugin that refuses it leaves the closing character typed as text. - Typing continues without the mark.
- It works anywhere in a text, at a collapsed caret, except in code lines, and only for marks the editor defines.
Adding a prefix
Declare markdown on a preset of your block kind:
blocks: {
note: {
snippet: note,
presets: [{ label: 'Note', icon: 'ℹ', data: { tone: 'info' }, markdown: ['!! '] }]
}
}
The last character of a prefix triggers it. A prefix without a trailing space, such as ---, converts as soon as it is complete.
A preset may also convert blocks of named kinds with prefixes of their own (markdownFrom, by kind). In a block of a named kind they come before every preset’s markdown, and when several presets answer the typed prefix, the one whose data shares the most values with the block’s wins. This is how a heading’s > keeps its level:
presets: (['h1', 'h2', 'h3'] as const).map((level, i) => ({
label: `Toggle heading ${i + 1}`,
data: { level },
// `> ` in any heading (the preset of its level wins), `## ` in a toggle.
markdownFrom: { heading: ['> '], toggle: [`${'#'.repeat(i + 1)} `] }
}))
``` See [Blocks](/docs/customization/blocks#presets) for presets and the [example plugin](/docs/plugins/example-plugin) for a full kind.