Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (node.children) {
data = {
type: getComponentName(node.children[0].value),
props: {}
}
// Remove open and closing jsx blocks
node.children = node.children.slice(1, node.children.length - 1)
return {
object: 'block',
type: 'jsx',
nodes: visitChildren(node),
data: {
type: data.type,
props: Data.create(data.props)
}
}
} else {
data = parseJSXBlock(node.value)
return {
object: 'block',
type: 'jsx-void',
data: {
type: data.type,
props: Data.create(data.props)
}
}
}
},
toMdast: (object, index, parent, { visitChildren }) => {
function addMarks(characters, tokens, offset = 0) {
for (const token of tokens) {
if (typeof token == 'string') {
offset += token.length;
continue;
}
const { content, length, type } = token;
let level;
if (type === 'title') {
const hashes = content.find(
innerToken => innerToken.type === 'punctuation'
);
level = hashes.length;
}
const mark = Mark.create({ type, data: Data.create({ level }) });
for (let i = offset; i < offset + length; i++) {
let char = characters.get(i);
let { marks } = char;
marks = marks.add(mark);
char = char.set('marks', marks);
characters.set(i, char);
}
if (Array.isArray(content)) {
addMarks(characters, content, offset);
}
offset += length;
}
}
deserialize = (el, next) => {
switch (el.tagName.toLowerCase()) {
case 'a':
return {
object: 'inline',
type: A,
nodes: next(el.childNodes),
data: Data.create({
href: (el.attrs.find(({ name }) => name === 'href') || {
value: ''
}).value
})
}
}
}
const setJSXProps = (editor, dataObject) => {
const {
value: { startBlock, document }
} = editor
const data = Data.create(dataObject)
if (startBlock.data.get('type') !== dataObject.type) {
const parent = document.getParent(startBlock.key)
if (parent) {
editor.setNodeByKey(parent.key, { type: 'jsx', data })
}
} else {
editor.setBlocks({ data })
}
}
deserialize = (el, next) => {
switch (el.tagName.toLowerCase()) {
case 'code':
return {
object: 'mark',
type: CODE,
data: Data.create({}),
nodes: next(el.childNodes),
};
case 'pre':
return {
object: 'block',
type: CODE,
nodes: next(el.childNodes),
};
default:
return;
}
}
const newKey = blockTools.randomKey(12)
const SlateType = node.constructor
const newData = node.get('data') ? node.get('data').toObject() : {}
newData._key = newKey
if (newData.value && newData.value._key) {
newData.value._key = newKey
}
if (newData.annotations) {
Object.keys(newData.annotations).forEach(key => {
newData.annotations[key]._key = blockTools.randomKey(12)
})
}
return new SlateType({
data: Data.create(newData),
isVoid: editor.query('isVoid', node),
key: newKey,
nodes: node.get('nodes').map(childNode => processNode(childNode, editor)),
type: node.get('type')
})
}
const NOOP = () => {}
object: 'block',
type: 'jsx',
nodes: visitChildren(node),
data: {
type: data.type,
props: Data.create(data.props)
}
}
} else {
data = parseJSXBlock(node.value)
return {
object: 'block',
type: 'jsx-void',
data: {
type: data.type,
props: Data.create(data.props)
}
}
}
},
toMdast: (object, index, parent, { visitChildren }) => {
const items = this.textStyles.map((style, index) => {
return {
key: `blockFormat-${index}`,
style: style,
preview: this.slateSchema.nodes.contentBlock({
children: [
{style.title}
]
}),
title: ` ${style.title}`,
active: this.hasStyle(style.value)
}
})
let value = items.filter(item => item.active)
const insertJSXBlock = (editor, type, props, component) => {
const { isVoid } = component.propertyControls
if (isVoid) {
editor.insertBlock({
type: 'jsx-void',
data: {
type,
props: Data.create(props)
}
})
} else {
editor.wrapBlock({
type: 'jsx',
data: {
type,
props: Data.create(props)
}
})
}
}
deserialize(el, next) {
if (el.tagName.toLowerCase() === 'a') {
const href = el.getAttribute('href')
return {
object: 'inline',
type: linkNode,
nodes: next(el.childNodes),
data: Data.create({
href: href ? href : ''
})
}
}
}
},