draft-js-utils icon indicating copy to clipboard operation
draft-js-utils copied to clipboard

[draft-js-export-markdown] Add option for custom entity render function

Open PierreGUI opened this issue 5 years ago • 0 comments

I want to support mentions in my app, using the plugin: https://www.draft-js-plugins.com/plugin/mention

It adds a type of entities that are currently not handled, thus not rendered in markdown. That's why I added the possibility to pass a function as an option to render an entity.

Here is an example how it can be used:

// Used in editor to export mention entity as markdown-compatible string "@id"
export const renderMentionMarkdown = (_content: string, e: EntityInstance): string | undefined => {
  if (e.getType() === 'mention') {
    return `@${e.getData().mention.id}`
  }

  return undefined
}

stateToMarkdown(editorState.getCurrentContent(), { renderEntity: renderMentionMarkdown })

PierreGUI avatar Mar 16 '21 20:03 PierreGUI