MarkdownManager API
Import MarkdownManager from @tiptap/markdown to parse Markdown into Tiptap JSON or serialize Tiptap JSON back to Markdown without creating an editor. See the standalone parsing example.
Methods
Constructor
new MarkdownManager(options?: {
extensions: AnyExtension[],
marked?: typeof marked,
markedOptions?: MarkedOptions,
indentation?: {
style?: 'space' | 'tab',
size?: number,
},
})MarkdownManager.hasMarked()
Returns true or false depending on whether the marked library is available.
- returns:
boolean
const manager = new MarkdownManager()
manager.hasMarked() // true or falseMarkdownManager.registerExtension()
Registers a Tiptap extension to be used for parsing and serializing Markdown content.
- returns:
void - parameters:
extension: A Tiptap extension to register.
const manager = new MarkdownManager()
manager.registerExtension(MyCustomExtension)MarkdownManager.parse()
Parses a Markdown string using the registered extensions. Pass the extensions that define the nodes and marks your content uses.
- returns:
JSONContentwithtype: 'doc'and acontentarray of parsed nodes. - parameters:
markdown: A string containing the Markdown content to parse.
import { MarkdownManager } from '@tiptap/markdown'
import StarterKit from '@tiptap/starter-kit'
const manager = new MarkdownManager({ extensions: [StarterKit] })
const doc = manager.parse('# Hello World')parse() throws if no Marked instance is available or if tokenization or a registered parser throws.
Markdown parsing preserves consecutive empty paragraphs by combining normal blank-line separation with markers when needed. For example, the first empty paragraph in a run is represented by blank lines, while later empty paragraphs in the same run are represented by so they survive a roundtrip back to JSON.
MarkdownManager.serialize()
Serializes a Tiptap document or JSON content into a Markdown string.
- returns:
string- The serialized Markdown string. - parameters:
content: A Tiptap document or JSON content to serialize.
const manager = new MarkdownManager()
const markdown = manager.serialize(doc)MarkdownManager.renderNodeToMarkdown()
Renders a single ProseMirror node to its Markdown representation.
- returns:
string- The Markdown string for the given node. - parameters:
- node: A ProseMirror node to render.
- parentNode: (optional) The parent ProseMirror node.
- index: (optional) The index of the node within its parent.
- level: (optional) The nesting level of the node.
const manager = new MarkdownManager()
const markdown = manager.renderNodeToMarkdown(node)The renderer passes sibling context to extensions through RenderContext.previousNode, which is useful for decisions that depend on the previous node at the same level, such as preserving runs of empty paragraphs.
MarkdownManager.renderNodes()
Renders an array of ProseMirror nodes to their combined Markdown representation.
- returns:
string- The combined Markdown string for the given nodes. - parameters:
- nodes: An array of ProseMirror nodes to render.
- parentNode: (optional) The parent ProseMirror node.
- separator: (optional) A string to separate the rendered nodes. Defaults to
'' - level: (optional) The nesting level of the nodes. Defaults to
0. - level:
number- The nesting level of the nodes. Defaults to0.
Properties
Markdown.instance
The MarkedJS instance used for parsing Markdown content.
Markdown.indentCharacter
The character used for indentation in lists. Defaults to a space (' ').
Markdown.indentString
The string used for indentation in lists. Defaults to two spaces (' ').