require("slate/package.json"); // slate is a peer dependency.
require("immutable/package.json"); // immutable is a peer dependency of slate.
const MarkdownSerializer = require('slate-mdast-serializer')
const assert = require('assert')
const paragraph = {
match: node => node.kind === 'block' && node.type === 'PARAGRAPH',
matchMdast: node => node.type === 'paragraph',
fromMdast: (node, index, parent, {visitChildren}) => ({
kind: 'block',
type: 'PARAGRAPH',
nodes: visitChildren(node)
}),
toMdast: (object, index, parent, {visitChildren}) => ({
type: 'paragraph',
children: visitChildren(object)
})
}
const bold = {
match: node => node.kind === 'mark' && node.type === 'BOLD',
matchMdast: node => node.type === 'strong',
fromMdast: (node, index, parent, {visitChildren}) => ({
kind: 'mark',
type: 'BOLD',
nodes: visitChildren(node)
}),
toMdast: (mark, index, parent, {visitChildren}) => ({
type: 'strong',
children: visitChildren(mark)
})
}
const serializer = new MarkdownSerializer({
rules: [
paragraph,
bold
]
})
const md = 'Hello **World**'
const value = serializer.deserialize(md)
const node = value.document.nodes.first()
assert.equal(node.kind, 'block')
assert.equal(node.type, 'PARAGRAPH')
assert.equal(node.text, 'Hello World')
const textKey = node.getFirstText().key
const worldMarks = value.change().select({
anchorKey: textKey,
anchorOffset: 5,
focusKey: textKey,
focusOffset: 10
}).value.marks
assert.equal(worldMarks.size, 1)
assert.equal(worldMarks.first().type, 'BOLD')
assert.equal(serializer.serialize(value).trimRight(), md)
console.log('markdown', md)
console.log('mdast', serializer.parse(md))
console.log('slate', value.toJS())