Hi there! I’m trying to leverage a ProseMirror schema to convert HTML into JSON, but I’ve got stuck on getting ProseMirror to recognise an <a>
tag as a top-level Node element.
Here’s my input HTML:
<a class="link-preview-container" href="https://example.com" rel="noopener noreferrer nofollow" target="_blank">
<img class="link-preview-image" src="https://example.com/image.jpg">
<div class="link-preview-text-container">
<div class="link-preview-title">Link Title</div>
<div class="link-preview-description">A link description</div>
<div class="link-preview-domain">example.com</div>
</div>
</a>
Here’s my schema for this node (note I haven’t written the getAttrs
function so I’m aware this won’t fill in the resultant JSON, but it should at least generate it):
sweet_link_preview: {
group: "block",
attrs: {
"url": { default: null },
"title": { default: null },
"description": { default: null },
"image": { default: null },
"domain": { default: null }
},
parseDOM: [{ tag: "a.link-preview-container" }],
toDOM: node =>
["a", { class: "link-preview-container", href: node.attrs.url, rel: "noopener noreferrer nofollow", target: '_blank', },
["img", { class: "link-preview-image", src: node.attrs.image }],
["div", { class: "link-preview-text-container"},
["div", {class: "link-preview-title"}, node.attrs.title],
["div", {class: "link-preview-description"}, node.attrs.description],
["div", {class: "link-preview-domain"}, node.attrs.domain],
]
]
},
Here’s the actual output:
{
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [
{
"type": "link",
"attrs": {
"href": "https://example.com"
}
}
],
"text": "Link Title"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [
{
"type": "link",
"attrs": {
"href": "https://example.com"
}
}
],
"text": "A link description"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [
{
"type": "link",
"attrs": {
"href": "https://example.com"
}
}
],
"text": "example.com"
}
]
}
]
}
And here’s the expected output I would like:
{
"type": "doc",
"content": [
{
"type": "sweet_link_preview",
"attrs": {
"url": "https://example.com",
"title": "Link Title",
"description": "A link description",
"image": "https://example.com/image.jpg",
"domain": "example.com"
}
}
]
}
Any help or pointers would be gladly appreciated! And let me know if you need more of my schema.