You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're currently using Tiptap as the text editor for a new app we're building, and need it to support the ability for a user to either paste or enter some markdown.
Typing works (for instance, I can do ## Some text, and that will convert to a heading – ditto bold marks etc).
However, I can't seem to get pasting to work – e.g. if user pastes in some content from another editor – Notion for instance.
I had a look around, and found a couple of threads – namely this one: #337.
I couldn't get the example to work, but in any case, I'm not sure it's the right path to go down as I want to support this behaviour for different marks / nodes (images being another).
And I've had some success with this approach – my code looks something like this:
exportconstEventHandler=Extension.create({name: "eventHandler",addProseMirrorPlugins(){return[newPlugin({key: newPluginKey("eventHandler"),props: {handlePaste(this,view,event,slice){constregexMdLinks=/\[([^\[]+)\](\(.*\))/gm;constmdContents=` Lorem ipsum dolor sit amet, consectetur adipiscing elit.. [hello link](/admin/table_edit/table_edit.cfm?action=edit&table_name=organizationsXcategories) Lorem ipsum dolor sit amet, consectetur adipiscing elit.. [otherLink](https://google.com) Lorem ipsum dolor sit amet, consectetur adipiscing elit.. [third link](https://google.com) `;consttext=mdContents;const{ state, dispatch }=view;constmatches=mdContents.match(regexMdLinks);constsingleMatch=/\[([^\[]+)\]\((.*)\)/;letupdated=text;// Remove all markdown syntaxfor(vari=0;i<matches.length;i++){lettext=singleMatch.exec(matches[i]);updated=updated.substring(0,updated.indexOf(matches[i]))+text[1]+updated.substring(updated.indexOf(matches[i])+matches[i].length);}consttransaction=view.state.tr.insertText(updated,state.selection.from,state.selection.to);// Apply the link markfor(vari=0;i<matches.length;i++){lettext=singleMatch.exec(matches[i]);transaction.addMark(state.selection.from+updated.indexOf(matches[i]),state.selection.to+(updated.indexOf(matches[i])+text[i].length),state.schema.marks.link.create({href: text}));}view.dispatch(transaction);returntrue;},},}),];},});
The issue I'm now running into is the line breaks in the example markdown aren't preserved. So I assume I would need to do something like the above to convert paragraphs into nodes.
But I'm also wondering if I'm going about this the right way, and whether there's perhaps, a simpler way?
I also tried using MarkdownIt to convert the input into HTML, and then converting that HTML into JSON using the generateJSON helper, but that didn't seem to work either.
Any guidance would be greatly appreciated!
What’s the solution you would like to see?
I imagine there's a reason why the above hasn't been implemented as default, so not sure what to suggest here – I guess if the above is already doable, then updating the documentation to make this clearer would help.
What alternatives did you consider?
Nothing to add here.
Anything to add? (optional)
No response
Are you sponsoring us?
Yes, I’m a sponsor. 💖
The text was updated successfully, but these errors were encountered:
Turns out the solution for this was remarkably simple – the editor instance is available within the plugin, here's what my code looks like (and it works perfectly). Closing, but sharing this here in case it helps anyone.
exportconstEventHandler=Extension.create({name: "eventHandler",addProseMirrorPlugins(){const{ editor }=this;return[newPlugin({key: newPluginKey("eventHandler"),props: {handlePaste(){constmd=newMarkdownIt();constmdContents=`Lorem ipsum dolor sit amet, consectetur adipiscing elit..[hello link](/admin/table_edit/table_edit.cfm?action=edit&table_name=organizationsXcategories)Lorem ipsum dolor sit amet, consectetur adipiscing elit..[otherLink](https://google.com)Lorem ipsum dolor sit amet, consectetur adipiscing elit..[third link](https://google.com) `;editor.commands.insertContent(generateJSON(md.render(mdContents),[EventHandler,StarterKit,Link.configure({openOnClick: false,}),]),{parseOptions: {preserveWhitespace: false,},});returntrue;},},}),];},});
What problem are you facing?
We're currently using Tiptap as the text editor for a new app we're building, and need it to support the ability for a user to either paste or enter some markdown.
Typing works (for instance, I can do ## Some text, and that will convert to a heading – ditto bold marks etc).
However, I can't seem to get pasting to work – e.g. if user pastes in some content from another editor – Notion for instance.
I had a look around, and found a couple of threads – namely this one: #337.
I couldn't get the example to work, but in any case, I'm not sure it's the right path to go down as I want to support this behaviour for different marks / nodes (images being another).
So, I eventually came across this example (not TipTap), where they've written their own ProseMirror plugin, and it would appear, something similar could be done in TipTap using the
addProseMirrorPlugin
property: https://github.com/outline/rich-markdown-editor/blob/main/src/plugins/PasteHandler.tsAnd I've had some success with this approach – my code looks something like this:
The issue I'm now running into is the line breaks in the example markdown aren't preserved. So I assume I would need to do something like the above to convert paragraphs into nodes.
But I'm also wondering if I'm going about this the right way, and whether there's perhaps, a simpler way?
I also tried using MarkdownIt to convert the input into HTML, and then converting that HTML into JSON using the generateJSON helper, but that didn't seem to work either.
Any guidance would be greatly appreciated!
What’s the solution you would like to see?
I imagine there's a reason why the above hasn't been implemented as default, so not sure what to suggest here – I guess if the above is already doable, then updating the documentation to make this clearer would help.
What alternatives did you consider?
Nothing to add here.
Anything to add? (optional)
No response
Are you sponsoring us?
The text was updated successfully, but these errors were encountered: