Plugin `sendHtml` will always insert the content as a block
- Dominant language
- JavaScript
- Stars
- 34
- Forks
- 71
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 31
Description
When plugins use `sendHtml` and attempt to insert inline content, it will always get wrapped in `
` tags. The reason:
> When you use DOMParser().parseFromString(e.data.details, 'text/html'), the browser creates a full HTML document structure, and then when ProseMirror parses it, it sees inline content ( with ) but the document root expects block content.
> ProseMirror's schema (in schema.js line 60) defines doc: { content: 'block+' }, so when you try to insert inline content at the document level or at a position that requires blocks, ProseMirror automatically wraps it in a paragraph.
The soln is to parse the HTML into a fragment and extract just the inline content, then insert it differently.
AI soln that I have not vetted:
```
if (e.data.action === 'sendHTML') {
const dom = new DOMParser().parseFromString(e.data.details, 'text/html');
const fragment = proseDOMParser.fromSchema(window.view.state.schema).parse(dom.body);
// If the fragment contains only inline content, insert it as inline content
if (fragment.childCount === 1 && fragment.firstChild.type.name === 'paragraph') {
// Extract the inline content from the paragraph
const inlineContent = fragment.firstChild.content;
window.view.dispatch(
window.view.state.tr.replaceSelection(
new Slice(inlineContent, 0, 0)
)
);
} else {
// For block content, use the original approach
window.view.dispatch(window.view.state.tr.replaceSelectionWith(fragment));
}
}
```
Contributor guide
Research direction
Start by locating the plugin handler for the `sendHTML` action and read `schema.js` around line 60 to understand the block-content requirement. Reproduce an inline insertion such as an `` with `` and compare it with block HTML. Done means inline content is inserted without an unwanted ` ` wrapper while block content still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100