Gathering detailed insights and metrics for draft-convert-greger
Gathering detailed insights and metrics for draft-convert-greger
Gathering detailed insights and metrics for draft-convert-greger
Gathering detailed insights and metrics for draft-convert-greger
Extensibly serialize & deserialize Draft.js ContentState with HTML.
npm install draft-convert-greger
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
237 Commits
1 Branches
1 Contributors
Updated on Dec 02, 2023
Latest Version
1.0.6
Package Id
draft-convert-greger@1.0.6
Unpacked Size
216.72 kB
Size
40.82 kB
File Count
43
NPM Version
8.11.0
Node Version
16.15.1
Published on
Feb 15, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
3
Extensibly serialize & deserialize Draft.js content with HTML
See draft-extend for more on how to use draft-convert with plugins
npm install draft-convert --save
or yarn add draft-convert
Jump to:
Extensibly serialize Draft.js ContentState
to HTML.
Basic usage:
1const html = convertToHTML(editorState.getCurrentContent());
Advanced usage:
1// convert to HTML with blue text, paragraphs, and links 2const html = convertToHTML({ 3 styleToHTML: (style) => { 4 if (style === 'BOLD') { 5 return <span style={{color: 'blue'}} />; 6 } 7 }, 8 blockToHTML: (block) => { 9 if (block.type === 'PARAGRAPH') { 10 return <p />; 11 } 12 }, 13 entityToHTML: (entity, originalText) => { 14 if (entity.type === 'LINK') { 15 return <a href={entity.data.url}>{originalText}</a>; 16 } 17 return originalText; 18 } 19})(editorState.getCurrentContent()); 20 21// convert content state to HTML with functionality defined in the plugins applied 22const html = compose( 23 FirstPlugin, 24 SecondPlugin, 25 ThirdPlugin 26)(convertToHTML)(editorState.getCurrentContent());
styleToHTML
, blockToHtml
, and entityToHTML
are functions that take Draft content data and may return a ReactElement
or an object of shape {start, end}
defining strings for the beginning and end tags of the style, block, or entity. entityToHTML
may return either a string with or without HTML if the use case demands it. blockToHTML
also may return an optional empty
property to handle alternative behavior for empty blocks. To use this along with a ReactElement
return value an object of shape {element: ReactElement, empty: ReactElement}
may be returned. If no additional functionality is necessary convertToHTML
can be invoked with just a ContentState
to serialize using just the default Draft functionality. convertToHTML
can be passed as an argument to a plugin to modularly augment its functionality.
Legacy alternative conversion options
styleToHTML
and blockToHTML
options may be plain objects keyed by style or block type with values of ReactElement
s or {start, end}
objects. These objects will eventually be removed in favor of the functions described above.
Type info:
1type ContentStateConverter = (contentState: ContentState) => string 2 3type Tag = 4 ReactElement | 5 {start: string, end: string, empty?: string} | 6 {element: ReactElement, empty?: ReactElement} 7 8type RawEntity = { 9 type: string, 10 mutability: DraftEntityMutability, 11 data: Object 12} 13 14type RawBlock = { 15 type: string, 16 depth: number, 17 data?: object, 18 text: string 19} 20 21type convertToHTML = ContentStateConverter | ({ 22 styleToHTML?: (style: string) => Tag, 23 blockToHTML?: (block: RawBlock) => Tag), 24 entityToHTML?: (entity: RawEntity, originalText: string) => Tag | string 25}) => ContentStateConverter
Extensibly deserialize HTML to Draft.js ContentState
.
Basic usage:
1const editorState = EditorState.createWithContent(convertFromHTML(html));
Advanced usage:
1// convert HTML to ContentState with blue text, links, and at-mentions 2const contentState = convertFromHTML({ 3 htmlToStyle: (nodeName, node, currentStyle) => { 4 if (nodeName === 'span' && node.style.color === 'blue') { 5 return currentStyle.add('BLUE'); 6 } else { 7 return currentStyle; 8 } 9 }, 10 htmlToEntity: (nodeName, node, createEntity) => { 11 if (nodeName === 'a') { 12 return createEntity( 13 'LINK', 14 'MUTABLE', 15 {url: node.href} 16 ) 17 } 18 }, 19 textToEntity: (text, createEntity) => { 20 const result = []; 21 text.replace(/\@(\w+)/g, (match, name, offset) => { 22 const entityKey = createEntity( 23 'AT-MENTION', 24 'IMMUTABLE', 25 {name} 26 ); 27 result.push({ 28 entity: entityKey, 29 offset, 30 length: match.length, 31 result: match 32 }); 33 }); 34 return result; 35 }, 36 htmlToBlock: (nodeName, node) => { 37 if (nodeName === 'blockquote') { 38 return { 39 type: 'blockquote', 40 data: {} 41 }; 42 } 43 } 44})(html); 45 46// convert HTML to ContentState with functionality defined in the draft-extend plugins applied 47const fromHTML = compose( 48 FirstPlugin, 49 SecondPlugin, 50 ThirdPlugin 51)(convertFromHTML); 52const contentState = fromHTML(html);
If no additional functionality is necessary convertToHTML
can be invoked with just an HTML string to deserialize using just the default Draft functionality. Any convertFromHTML
can be passed as an argument to a plugin to modularly augment its functionality. A flat
option may be provided to force nested block elements to split into flat, separate blocks. For example, the HTML input <p>line one<br />linetwo</p>
will produce two unstyled
blocks in flat
mode.
Type info:
1type HTMLConverter = (html: string, {flat: ?boolean}, DOMBuilder: ?Function, generateKey: ?Function) => ContentState 2 3type EntityKey = string 4 5type convertFromHTML = HTMLConverter | ({ 6 htmlToStyle: ?(nodeName: string, node: Node) => DraftInlineStyle, 7 htmlToBlock: ?(nodeName: string, node: Node) => ?(DraftBlockType | {type: DraftBlockType, data: object} | false), 8 htmlToEntity: ?( 9 nodeName: string, 10 node: Node, 11 createEntity: (type: string, mutability: string, data: object) => EntityKey, 12 getEntity: (key: EntityKey) => Entity, 13 mergeEntityData: (key: string, data: object) => void, 14 replaceEntityData: (key: string, data: object) => void 15 ): ?EntityKey, 16 textToEntity: ?( 17 text: string, 18 createEntity: (type: string, mutability: string, data: object) => EntityKey, 19 getEntity: (key: EntityKey) => Entity, 20 mergeEntityData: (key: string, data: object) => void, 21 replaceEntityData: (key: string, data: object) => void 22 ) => Array<{entity: EntityKey, offset: number, length: number, result: ?string}> 23}) => HTMLConverter
Any conversion option for convertToHTML
or convertFromHTML
may also accept a middleware function of shape (next) => (…args) => result
, where …args
are the normal configuration function paramaters and result
is the expected return type for that function. These functions can transform results of the default conversion included in convertToHTML
or convertFromHTML
by leveraging the result of next(...args)
. These middleware functions are most useful when passed as the result of composition of draft-extend
plugins. If you choose to use them independently, a __isMiddleware
property must be set to true
on the function for draft-convert
to properly handle it.
No vulnerabilities found.
No security vulnerabilities found.