npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mixxt/markdown-draft-js

v1.4.0

Published

Convert draftjs blocks to markdown using the marked library, and vice versa.

Downloads

12

Readme

⭐️ A quick maintenance note: I (Rosey) will be on maternity leave starting in December 2017 until December 2018 and will likely have way less time to devote to issues, bug fixes, etc. (And once I get back I’ll be working full time + have a toddler, so I’m sure it won’t be much better 😇) Therefore, it’s much appreciated if people who encounter bugs and are able to do so, they open a PR with a proposed fix (vs opening an issue and waiting for me to fix it). OR if you happen to be using this library and see an open issue you’re able to fix, I would love it if you opened a PR with that fix! Of course, feel free to continue to open issues if you don’t have the time or knowledge to fix a bug you notice, I just want to set the expectation that response time will be slower 🙃 I’ll do my best to continue to review and merge any PRs that do get opened, although for the first few months after giving birth there may be total radio silence on my end. Thank you! ❤️ And thank you to everyone who has helped out and contributed to this project in its early days!!

Markdown draft js

A tool for converting Draft.js raw object to markdown, and vice-versa.

Looking for an example? There is a running example here

Basic Usage

Please note: We recommend using a polyfill (like babel-polyfill) since we're using a bunch of modern array methods.

draftToMarkdown expects a RAW Draft.js JS object.

It returns a string of markdown.

// First, import `draftToMarkdown`
import { draftToMarkdown } from 'markdown-draft-js';

var markdownString = draftToMarkdown(rawObject);

markdownToDraft expects a string containing markdown.

It returns a RAW Draft.js JS object.

// First, import `draftToMarkdown`
import { markdownToDraft } from 'markdown-draft-js';

var rawObject = markdownToDraft(markdownString);

Custom Values

In case you want to extend markdown’s functionality, you can. draftToMarkdown accepts an (optional) second options argument.

It takes two values: styleItems and entityItems. This is because of a distinction in draftjs between styles and entities. You can read more about them on Draft’s documentation.

Say I wanted to convert red text from my Draft.js editor to a span with a red colour style. Unless I write a custom method for it, the markdown parser will ignore this special style, since it’s not a normal, pre-defined style. (An example of this style item is defined in one of the Draft.js custom colours examples.)

However, I can pass in a custom renderer for the red style type, and then decide how I want it to be depicted in markdown. Since markdown parsers usually also accept HTML, in this example I’ll just have my custom renderer do a span with a red style. Here it is:

var markdownString = draftToMarkdown(rawObject, {
  styleItems: {
    red: {
      open: function () {
        return '<span style="color: red">';
      },

      close: function () {
        return '</span>';
      }
    }
  }
});

red is the value of the style key in the raw object. The open method is what precedes the actual text, and close is what succeeds it.

Here’s another example, with a mention entity type -

var markdownString = draftToMarkdown(rawObject, {
  entityItems: {
    mention: {
      open: function (entity) {
        return '<span class="mention-item" data-user-id="' + entity.data.id + '">';
      },

      close: function (entity) {
        return '</span>';
      }
    }
  }
});

Since entities can also contain additional custom information - in this case, the user’s id, an entity object is passed to the open and close methods so that you can use that information in your open/close text if you need to.

What if you wanted to go the opposite direction? markdownToDraft uses Remarkable for defining custom markdown types.

In this case, you need to write a remarkable plugin first and pass it in to markdownToDraft -

var rawDraftJSObject = markdownToDraft(markdownString, {
  remarkablePlugins: [remarkableMentionPlugin],
  blockEntities: {
    mention_open: function (item) {
      return {
        type: "mention",
        mutability: "IMMUTABLE",
        data: {
          mention: {
            id: item.id,
            name: item.name
          }
        }
      };
    }
  }
});

Additional options

Remarkable options

Since this module uses remarkable under the hood, you can also pass down options for the remarkable parser, simply add the property remarkableOptions to your options object. For example, let's say you wanted to parse html as well:

var rawDraftJSObject = markdownToDraft(markdownString, {
  remarkableOptions: {
    html: true
  }
});

More options

preserveNewlines can be passed in to preserve empty whitespace newlines. By default, markdown rules specify that blank whitespace is collapsed, but in the interest in maintaining 1:1 parity with draft appearance-wise, this option can be turned on if you like :)

NOTE: If you plan on passing the markdown to a 3rd party markdown parser, markdown default behaviour IS to strip additional newlines, so the HTML it generates will likely strip those newlines at that point.... Which is why this is an option disabled by default.

FAQ

How do I get images to work?

For now, check out this pull request and the discussion below. this comment outlines how to get images working by writing a basic plugin for markdown-draft-js. The reason it’s not built into the library itself is because draftjs doesn’t support images out of the box, so there’s no standardized way of supporting them in the library that will work for everyone. In the future I hope to publish a plugin for people to quickly add image support if they need to, but I haven’t quite gotten there yet 🙂