Gathering detailed insights and metrics for draft-convert
Gathering detailed insights and metrics for draft-convert
Gathering detailed insights and metrics for draft-convert
Gathering detailed insights and metrics for draft-convert
Extensibly serialize & deserialize Draft.js ContentState with HTML.
npm install draft-convert
58.5
Supply Chain
91.5
Quality
82.4
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
483 Stars
234 Commits
94 Forks
143 Watching
27 Branches
49 Contributors
Updated on 25 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-25.1%
13,233
Compared to previous day
Last week
-6.7%
81,887
Compared to previous week
Last month
-5.2%
351,217
Compared to previous month
Last year
1.6%
3,867,535
Compared to previous year
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.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/20 approved changesets -- score normalized to 3
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
72 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