Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
draftjs-md-converter
Advanced tools
Converter for converting Draft.js state into Markdown and vice versa
Converts rich text content between Draft.js blocks and Markdown.
This library exists because I needed a highly customisable rich text editor which posts to an external API in Markdown. Draft.js to the rescue! It provides the editor state but, alas, doesn't ship with any sort of conversion to or from markdown. So, I've written my own.
npm install draftjs-md-converter
The following inline styles are supported:
The following block styles are supported:
The following media types are supported:
mdToDraftjs(markdown: String): RawDraftContentState
Use convertToRaw from the draft-js library
to convert the resulting RawDraftContentState into a draft-js ContentState.
The default supported inline styles:
{
Strong: {
type: 'BOLD',
symbol: '__'
},
Emphasis: {
type: 'ITALIC',
symbol: '*'
}
}
The default supported block styles:
{
List: 'unordered-list-item',
Header1: 'header-one',
Header2: 'header-two',
Header3: 'header-three',
Header4: 'header-four',
Header5: 'header-five',
Header6: 'header-six',
CodeBlock: 'code-block',
BlockQuote: 'blockquote'
}
Inline styles and block styles can be extended or overridden by passing a custom styles object as a second optional argument to mdToDraftjs
, e.g.
const markdown = "Some `markdown` with ~~deleted~~ text";
const myCustomStyles = {
inlineStyles: {
Delete: {
type: "STRIKETHROUGH",
symbol: "~~",
},
Code: {
type: "CODE",
symbol: "`",
},
},
blockStyles: {
CustomBlock: "custom-block",
},
};
const content = mdToDraftjs(markdown, myCustomStyles);
The keys to the inlineStyles
object should be valid AST node types.
draftjsToMd(rawData: RawDraftContentState): String
Use convertFromRaw from the draft-js library
to get the raw RawDraftContentState to then pass into the converter.
The default Markdown dictionary is
{
BOLD: '__',
ITALIC: '*'
};
The inline styles can be extended or overridden by passing a custom dictionary object as a second optional argument to draftjsToMd
, e.g.
const myMdDict = {
BOLD: "**",
STRIKETHROUGH: "~~",
};
const markdown = draftjsToMd(blocks, myMdDict);
NOTE: at this point you cannot override block styles!
[---]
import { mdToDraftjs, draftjsToMd } from 'draftjs-md-converter';
import { EditorState, ContentState, convertToRaw, convertFromRaw } from 'draft-js';
[---]
constructor(props) {
super(props);
// some default value in markdown
const defaultValue = this.props.defaultValue;
const rawData = mdToDraftjs(defaultValue);
const contentState = convertFromRaw(rawData);
const newEditorState = EditorState.createWithContent(contentState);
this.state = {
editorState: newEditorState,
};
this.onChange = (editorState) => {
this.props.onChange(this.getMarkdown());
this.setState({ editorState });
};
}
[---]
getMarkdown() {
const content = this.state.editorState.getCurrentContent();
return draftjsToMd(convertToRaw(content));
}
[---]
npm test
npm run test-dev
npm run lint
FAQs
Converter for converting Draft.js state into Markdown and vice versa
The npm package draftjs-md-converter receives a total of 6,430 weekly downloads. As such, draftjs-md-converter popularity was classified as popular.
We found that draftjs-md-converter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.