DefinitelyTyped/types/markdown-draft-js/markdown-draft-js-tests.ts
Irina Shasheva 1e0f80d01a
Update typings and test for markdown-draft-js (#43980)
Co-authored-by: irina.shasheva <irina.shasheva@jetbrains.com>
2020-04-17 14:10:30 -07:00

120 lines
3.0 KiB
TypeScript

import { EditorState, convertToRaw, convertFromRaw } from 'draft-js';
import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js';
const rawContent = convertToRaw(EditorState.createEmpty().getCurrentContent());
draftToMarkdown(rawContent);
// Handle options.styleItems
draftToMarkdown(rawContent, {
styleItems: {
RED: {
open() {
return '<span style="color: red">';
},
close() {
return '</span>';
},
},
},
});
// Handle options.entityItems
draftToMarkdown(rawContent, {
entityItems: {
mention: {
open: () => {
return '<span class="mention-item">';
},
close: () => {
return '</span>';
},
},
},
});
// Handle markdown param
const rawDraft = markdownToDraft('# Test');
convertFromRaw(rawDraft);
// Handle options.blockEntities without arguments and options.remarkablePlugins
const rawDraftWithBlockEntities = markdownToDraft('# Test', {
blockEntities: {
link_open: () => {
return {
type: 'LINK',
mutability: 'MUTABLE',
};
},
},
remarkablePlugins: [{}],
});
convertFromRaw(rawDraftWithBlockEntities);
// Handle options.blockEntities with argument
const rawDraftWithBlockEntitiesArguments = markdownToDraft('# Test', {
blockEntities: {
link_open: item => {
const data = item && {
url: item.href,
href: item.href,
};
return {
type: 'LINK',
mutability: 'MUTABLE',
data,
};
},
},
});
convertFromRaw(rawDraftWithBlockEntitiesArguments);
// Handle options.blockStyles
const rawDraftWithBlockStyles = markdownToDraft('# Test', {
blockStyles: {
code: 'CODE',
},
});
convertFromRaw(rawDraftWithBlockStyles);
// Handle options.blockTypes without arguments
const rawDraftWithBlockTypes = markdownToDraft('# Test', {
blockTypes: {
paragraph_open: () => {
return {
type: 'unstyled',
text: '',
entityRanges: [],
inlineStyleRanges: [],
};
},
},
});
convertFromRaw(rawDraftWithBlockTypes);
// Handle options.blockTypes with argument
const rawDraftWithBlockTypesArguments = markdownToDraft('# Test', {
blockTypes: {
paragraph_open: item => {
const text = item ? item.text : '';
return {
type: 'unstyled',
text,
entityRanges: [],
inlineStyleRanges: [],
};
},
},
});
convertFromRaw(rawDraftWithBlockTypesArguments);
// Handle options.remarkablePreset and options.remarkableOptions
const rawDraftWithRemarkablePreset = markdownToDraft('# Test', {
remarkablePreset: 'commonmark',
remarkableOptions: {
html: true,
},
});
convertFromRaw(rawDraftWithRemarkablePreset);