Slate Mdast Serializer
Convert Slate trees to MDAST trees.
See @orbiting/remark-preset
for a good preset to parse markdown and stringify mdast to markdown.
API
rule
object
match: : Boolean matchMdast: : Boolean fromMdast: : SlateJson toMdast: : Mdast
match
Return true if the rule should serialize the slate object.matchMdast
Return true if the rule should deserialize the mdast node.fromMdast
Return slate objects for a given mdast node.toMdast
Return mdast nodes for a given slate object.
See most common rules below.
constructor(options): instance
options.rules
array of rule objects
instance.deserialize(mdast, options): Slate.Value
mdast
: Mdast
options
: Object
instance.deserialize(value, options): Mdast
value
: Slate.Value
options
: Object
instance.fromMdast(mdast, index = 0, parent = null, options)
instance.toMdast(slateJson, index = 0, parent = null, options)
instance.parse(markdown): Mdast
instance.stringify(mdast): String
Most Common Rules
Arbitrary Children
Delegate children processing back to the serializer.
match: nodetype === 'heading' && nodedepth === 1 kind: 'block' type: TYPE nodes: type: 'heading' depth: 1 children:
Block with Specific Children
Delegate to a childSerializer
. Another instance with just the rules you want.
Make sure to forward parent information and rest (e.g. context) to the childSerializer
.
match: matchMdast: rulematchMdast kind: 'block' type: TYPE nodes: childSerializer type: 'zone' identifier: TYPE children: childSerializer
Void Node
Skip processing children further.
match: nodetype === 'image' { return kind: 'block' type: TYPE data: alt: nodealt src: nodeurl isVoid: true nodes: } type: 'image' alt: objectdataalt url: objectdatasrc
Example
const MarkdownSerializer = const parse stringify = const assert = const paragraph = nodekind === 'block' && nodetype === 'PARAGRAPH' nodetype === 'paragraph' kind: 'block' type: 'PARAGRAPH' nodes: type: 'paragraph' children: const bold = nodekind === 'mark' && nodetype === 'BOLD' nodetype === 'strong' kind: 'mark' type: 'BOLD' nodes: type: 'strong' children: const serializer = rules: paragraph bold const md = 'Hello **World**\n' const value = serializerconst node = valuedocumentnodes assertassert assert const textKey = nodekeyconst worldMarks = valuevaluemarks assertassert assert