Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
draftjs-md-converter
Advanced tools
Converter for converting Draft.js state into Markdown and vice versa
Converts content from Draft.js blocks to Markdown and vice versa.
This exists because I needed a highly customisable rich text editor which posts to an external API in Markdown. Draft.js to the rescue! Alas, it doesn't ship with any sort of import or export to or from markdown so I've written my own.
npm install draftjs-md-converter
The following inline styles are supported:
The following block syles are supported:
Import mdToDraftjs
and draftjsToMd
into the React component. When instantiating the draft.js editor, use the mdToDraftjs
function to convert the default value (in markdown) to draft.js blocks and use the ContentState.createFromBlockArray()
function to create the immutable draft.js blocks.
Use the this.state.editorState.getCurrentContent()
to get the current content and draftjsToMd(convertToRaw(content).blocks)
to convert it back to markdown. That can be used with whatever onChange functionality used.
Below is a code example of the above in some context.
[---]
import { mdToDraftjs, draftjsToMd } from 'draftjs-md-converter';
import { EditorState, ContentState, convertToRaw, convertFromRaw } from 'draft-js';
[---]
constructor(props) {
super(props);
// some default falue in markdown
const defaultValue = this.props.defaultValue;
const rawData = mdToDraftjs(defaultValue);
const raw = convertFromRaw(rawData);
const contentState = ContentState.createFromBlockArray(raw);
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).blocks);
}
[---]
npm test
npm run test-dev
FAQs
Converter for converting Draft.js state into Markdown and vice versa
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
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.