Skip to content

Insert Markdown formated text while in WYSIWYG mode. #3276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
geHuC opened this issue Oct 18, 2024 · 1 comment
Open

Insert Markdown formated text while in WYSIWYG mode. #3276

geHuC opened this issue Oct 18, 2024 · 1 comment
Labels

Comments

@geHuC
Copy link

geHuC commented Oct 18, 2024

I'm writing an extension that pulls blocks of text from an endpoint that are formatted in MD, and i want to be able insert them inside the editor with the formatting. I am using insertText() for this.

This is where the problem is if the user is in WYSIWYG mode the text gets escaped. I tried to see what the mode is and try and convert the text to html but it gets escaped again. I tried getting the mdEditor from the editor object but this returns an error saying insertText does not exist, and if use replaceSelection directly it only works when the editor is in markdown mode anyway. As a final resort I tried switching back and fort the mode which does work but causes jumps and is bad ux.

So my question is is there a way to insert MD text and retain formatting using JavaScript even when the current view is in WYSIWYG.

@geHuC geHuC added the Question label Oct 18, 2024
@jonmchan
Copy link

jonmchan commented Mar 21, 2025

NEVERMIND. This doesn't work for this general case for Markdown formatted text. The solution only works for trying to add unescaped text for a custom block.

Underscore escaping seems to be called regardless if this is first time or last time. It seems to always be happening.


It doesn't seem like this project is still maintained... but I ran into this problem as well and came up with a very hacky solution.

You'll have to create your own plugin and create you own command:


function insertUnescapedTextPlugin() {
  const wysiwygCommands = {
    unescapedInsertCommand: (payload, { tr, selection, schema }, dispatch) => {
      let node = schema.nodes.customBlock.create({info: 'myPlugin'},[schema.text(payload)]);
      tr.replaceSelectionWith(node);
      dispatch(tr);

      return true;
    },
  }

  return {wysiwygCommands }
}

editor = new Editor({ 
      ...
      plugins: [insertUnescapedTextPlugin]
    });

Then you can call your function where you want to insert code:

let isWysiwygMode = editor.isWysiwygMode();
if (isWysiwygMode) {
  let [ns, ne] = editor.getSelection()
  editor.exec("unescapedInsertCommand", "**This is markdown text.**");
  editor.changeMode('markdown', true)
  editor.changeMode('wysiwyg', true)
  editor.setSelection(ns, ne); // if you want the cursor to be where it was before swapping the modes...
} else {
  // use regular insertText in markdown mode
  editor.insertText("**This is markdown text.**");
}

Yes, you have to switch it back and forth from markdown to wysiwyg mode really fast for the markdown to be rendered correctly in WYSIWYG mode...

Don't ask me why or how... This is the only solution I was able to come up with after 6 hours of hacking.

If anyone can expand on this and make it less hacky, I would definitely welcome others to try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants