Parse to markdown during a copy paste

Hi, I am trying to use prosemirror-markdown logic to transform Slice during copy-paste, but I can’t wrap my head around creating a new slice.

When I paste some content, nothing happens.

Here is the code:

transformPasted(slice, view) {
        const parsed = defaultMarkdownParser.parse(slice.content.toString())
        if (parsed) {
          return new Slice(Fragment.from(parsed), slice.openStart, slice.openEnd)
        return slice

Here are the logs for each value.

I’ve tried also using firstChild to remove the “doc” Node, no effect. I’ve noticed that the string seems escaped when I print the parsed version.

Any hint about what I’m doing wrong?

Edit: ok so first mistake is using toString(), this seems a debug utility. But it should still paste some content.

transformPasted, as the types show, works in terms of ProseMirror slices, so I’m not sure what you want to do with a Markdown parser there. Maybe you’re looking for transformPastedText instead?

I might misunderstand the functions intent but transformPastedText seems to be applied only to text/plain content type (resp. for transformPastedHTML and text/html)? Edit: I confirm those are for string manipulation but not for creating nodes from the pasted content.

This is the first step of implementing some more advanced parsing logic during paste/drop, so I used the more generating transformPasted.

My goal is to detect markdown syntax in the pasted/dropped text and turn it into the right prosemirror nodes

I now get the slice content with slice.content.textBetween(0, slice.size) but the final slice still seems wrong and no text is added on paste.

I’ve tried with clipboardTextParser, which seems more suited for my need as it gets the pasted text as a string, instead of an already built slice:

      clipboardTextParser(text) {
        // default parser uses CommonMark syntax
        const parsed = defaultMarkdownParser.parse(text)
        return new Slice(Fragment.from(parsed), 1, 1)

But still no luck with the slice, it pastes nothing.

This will create a slice holding a top-level document node, which as such cannot be inserted anywhere. new Slice(parsed.content, 0, 0) is probably closer to what you want.

1 Like

The parse.content part (instead of manually creating a Fragment) is better indeed, the log show the right content for the slice, with markdown marks being parsed:

But the slice still doesn’t display with new Slice(parsed.content, 0, 0). I’ve tried 0,0 and slice.openStart, slice.openEnd, same result. I didn’t really figure what they mean so my issue is probably there.