yjs / y-prosemirror

ProseMirror editor binding for Yjs
https://demos.yjs.dev/prosemirror/prosemirror.html
MIT License
355 stars 124 forks source link

prosemirrorToYXmlFragment losing <a> link around <img> tags #165

Open bosschaert opened 3 months ago

bosschaert commented 3 months ago

Describe the bug When I pass prosemirrorToYXmlFragment a JSON representation of a HTML document that wraps an <img> with a <a> link tag, the link is dropped, which basically means that a clickable image that navigates elsewhere becomes just a static image.

To Reproduce Execute the following code:

import { JSDOM } from 'jsdom';
import { DOMParser } from 'prosemirror-model';
import { schema as baseSchema } from 'prosemirror-schema-basic';
import { prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror';

...

const html = `
<body>
  <main><a href="www.yeehaa.com" title="abc"><img src="http://www.foo.com/myimg.jpg"></a></main>
</body>
`;
console.log('Input', html);

const ydoc = new Y.Doc();
const jsdom = new JSDOM(html);
const json = DOMParser.fromSchema(baseSchema).parse(jsdom.window.document);
console.log('Before', json.toString());
prosemirrorToYXmlFragment(json, ydoc.getXmlFragment('prosemirror'));
console.log('YDoc', ydoc.getXmlFragment('prosemirror').toJSON());

const json2 = yXmlFragmentToProseMirrorRootNode(ydoc.getXmlFragment('prosemirror'), baseSchema);
console.log('After', json2.toString());

This logs the following output:

Input 
<body>
  <main><a href="www.yeehaa.com" title="abc"><img src="http://www.foo.com/myimg.jpg"></a></main>
</body>

Before doc(paragraph(link(image)))

YDoc <paragraph><image src="http://www.foo.com/myimg.jpg"></image></paragraph>

After doc(paragraph(image))

We can see that the Before log message contains a link component, and the After log message doesn't, which is wrong. It can also be seen that the YDoc serialization is missing the link.

As a sanity check, if I change the HTML to the following, things work as expected:

Input 
<body>
  <main><a href="www.yeehaa.com" title="abc">blahblah</a></main>
</body>

Before doc(paragraph(link("blahblah")))

YDoc <paragraph><link href="www.yeehaa.com" title="abc">blahblah</link></paragraph>

After doc(paragraph(link("blahblah")))

Expected behavior The link defined in the tag should not be disappearing.

Environment Information

  • Node.js - 21.7.1
  • y-prosemirror - 1.2.12
  • prose-mirror-model - 1.22.3
  • prosemirror-schema-basic - 1.2.3
  • yjs 13.6.18

This issue is possibly related to https://github.com/yjs/y-prosemirror/issues/138