I created a node that containes meta Image and description of a link.
const CustomNode = {
inline: true,
attrs: {
src: {},
width: { default: '100%' },
alt: { default: null },
title: { default: null },
textContent: { default: '' },
imgUrl: { default: '' },
},
group: 'inline',
parseDOM: [
{
priority: 51,
tag: 'div[src][title][textContent][imgUrl]',
getAttrs(dom) {
return {
src: dom.getAttribute('src'),
title: dom.getAttribute('title'),
textContent: dom.getAttribute('textContent'),
imgUrl: dom.getAttribute('imgUrl'),
};
},
},
],
toDOM(node) {
const attrs = {
style: `
display: flex;
justify-content: flex-start;
padding: 1.5rem;
border-radius: 15px;
background-color: #f1f1f1;
overflow: hidden;
margin: 1rem 0;
`,
};
const imgAttrs = {
src: node.attrs.imgUrl,
width: '100px',
height: '100px',
style:
'margin: 1rem; object-fit: cover; margin-right: 2rem; width: 100px; height: 100px; ',
};
const aAttrs = {
href: node.attrs.src,
target: '_blank',
rel: 'noopener',
};
return [
'div',
[
'a',
{ ...aAttrs },
[
'div',
{ ...node.attrs, ...attrs },
['img', { ...imgAttrs }],
['span', node.attrs.title + '\n' + node.attrs.textContent],
],
],
];
},
};
I save the content from the editor as HTML by using DOMSerializer.
Every time I parse the HTML in the editor, new empty nodes are created above and below this CustomNode.
Is this the right way to create a node? What can I do to avoid those extra empty nodes?
Also currently after serializing, I’m replacing empty p tags with <p> </p>
which might not be the right way. to save those spaces, what can I do to preserve those breaks?
Thanks!