Gathering detailed insights and metrics for markdown-draft-js
Gathering detailed insights and metrics for markdown-draft-js
Gathering detailed insights and metrics for markdown-draft-js
Gathering detailed insights and metrics for markdown-draft-js
A tool to convert content created in DraftJS to markdown and vice versa.
npm install markdown-draft-js
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
317 Stars
245 Commits
70 Forks
5 Watching
20 Branches
21 Contributors
Updated on 01 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.3%
12,069
Compared to previous day
Last week
3.7%
60,729
Compared to previous week
Last month
2.7%
266,695
Compared to previous month
Last year
4.8%
2,949,343
Compared to previous year
😷 COVID update I am now stuck at home full time with two very young children and get zero breaks all day from the moment I wake up until the moment I go to sleep. Anything that requires deep thought, like reviewing and writing code, as well as taking deep dives into open issues, is basically impossible for me right now! So sorry if things fall behind.
⭐️ A quick maintenance note: I (Rosey) work full time and am the mom to a toddler, a soon-to-be newborn (coming Dec 2019), and a high–energy dog, and this is just a side–project 😇 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 not be super speedy 🙃 I’ll do my best to review and merge any PRs that do get opened. Thank you! ❤️ And thank you to everyone who has helped out and contributed to this project, it has been a real delight 🥰
A tool for converting Draft.js raw object to markdown, and vice-versa.
Looking for an example? There is a running example here
I started this project in 2016 because I was in need of a draft/markdown conversion tool that could handle custom entities, such as mentions, and the existing conversion tools out there didn’t support these slightly complex needs. I was also finding various bugs with the existing conversion tools and none of them seemed to be maintained, so I decided to write my own.
It’s now 2019 and the landscape has potentially changed! I don’t spend a ton of time keeping tabs on other draftjs markdown conversion tools out there, but I believe there are a few that are actively maintained and significantly more popular than this one, such as draft-js-export-markdown. Before choosing this project, I encourage you to do your research! This may still be the best tool for what you need, but it’s always worth being critical and looking at all your options 😃 Stability wise, I use markdown-draft-js in a production environment with over 10k monthly active users and it has served very well so far.
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.
1// First, import `draftToMarkdown` 2import { draftToMarkdown } from 'markdown-draft-js'; 3 4var markdownString = draftToMarkdown(rawObject);
markdownToDraft
expects a string containing markdown.
It returns a RAW Draft.js JS object.
1// First, import `draftToMarkdown` 2import { markdownToDraft } from 'markdown-draft-js'; 3 4var rawObject = markdownToDraft(markdownString);
1[---] 2 3import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js'; 4import { EditorState, convertToRaw, convertFromRaw } from 'draft-js'; 5 6[---] 7 8constructor(props) { 9 super(props); 10 11 // Convert input from markdown to draftjs state 12 const markdownString = this.props.markdownString; 13 const rawData = markdownToDraft(markdownString); 14 const contentState = convertFromRaw(rawData); 15 const newEditorState = EditorState.createWithContent(contentState); 16 this.state = { 17 editorState: newEditorState, 18 }; 19 20 this.onChange = (editorState) => { 21 this.setState({ editorState }); 22 23 // Convert draftjs state to markdown 24 const content = editorState.getCurrentContent(); 25 const rawObject = convertToRaw(content); 26 const markdownString = draftToMarkdown(rawObject); 27 28 // Do something with the markdown 29 }; 30} 31 32[---]
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:
1var markdownString = draftToMarkdown(rawObject, { 2 styleItems: { 3 red: { 4 open: function () { 5 return '<span style="color: red">'; 6 }, 7 8 close: function () { 9 return '</span>'; 10 } 11 } 12 } 13});
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 -
1var markdownString = draftToMarkdown(rawObject, { 2 entityItems: { 3 mention: { 4 open: function (entity, block) { 5 return '<span class="mention-item" data-user-id="' + entity.data.id + '" data-block-type="'+ block.type + '">'; 6 }, 7 8 close: function (entity) { 9 return '</span>'; 10 } 11 } 12 } 13});
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.
In case you need more information about the block the entity belongs to, it is available as the second parameter of the open/close methods.
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
-
1var rawDraftJSObject = markdownToDraft(markdownString, { 2 remarkablePlugins: [remarkableMentionPlugin], 3 blockEntities: { 4 mention_open: function (item) { 5 return { 6 type: "mention", 7 mutability: "IMMUTABLE", 8 data: { 9 mention: { 10 id: item.id, 11 name: item.name 12 } 13 } 14 }; 15 } 16 } 17});
Since this module uses Remarkable under the hood, you can also pass down preset and options for the Remarkable parser. Simply add the remarkablePreset
or remarkableOptions
property (or both of them) to your options object. For example, let's say you wanted to use the commonmark
preset and parse html as well:
1var rawDraftJSObject = markdownToDraft(markdownString, { 2 remarkablePreset: 'commonmark', 3 remarkableOptions: { 4 html: true 5 } 6});
It's possible to enable or disable specific rules. Remarkable categorizes them into three groups, every file represents a possible rule:
1var rawDraftJSObject = markdownToDraft(markdownString, { 2 remarkablePreset: 'commonmark', 3 remarkableOptions: { 4 disable: { 5 inline: ['links', 'emphasis'], 6 block: ['heading'] 7 }, 8 enable: { 9 block: 'table', 10 core: ['abbr'] 11 } 12 } 13});
The table
rule is disabled by default but could be enabled like in the example above.
preserveNewlines
can be passed in to preserve multiple sequential empty lines. By default, markdown rules specify that blank whitespace is collapsed, the result being that more than one empty line will be reduced to a single empty line, 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.
escapeMarkdownCharacters
– by default this value is true and markdown characters (e.g. *~_`) typed directly into the editor by a user are escaped when converting from draft to markdown.
Setting this option to false will prevent those special characters from being escaped, so the markdown string it generates will remain in its “raw” form. What this means is that when the markdown is later converted back to draftjs or parsed by a different markdown tool, any user-entered markdown will be rendered AS markdown, and will not "match" what the user initially entered. (So if the user explicitly typed in **hello**
, converting to markdown and back to draft it will be restored as hello instead of the original **hello**
)
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 🙂
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 10/14 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
60 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More