Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

draftjs-to-markdown

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draftjs-to-markdown - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

2

lib/draftjs-to-markdown.js

@@ -1,1 +0,1 @@

module.exports=function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=2)}([function(t,e,n){"use strict";function r(t){return t.type&&C[t.type]}function o(t){return!!(t.entityRanges.length>0&&(0,v.isEmptyString)(t.text))}function u(t,e,n){var r=t[e];return"LINK"===r.type||"MENTION"===r.type?"["+n+"]("+r.data.url+")":"IMAGE"===r.type?"!("+r.data.src+")":"EMBEDDED_LINK"===r.type?'<iframe width="'+r.data.width+'" height="'+r.data.height+'" src="'+r.data.src+'" frameBorder="0" allowFullScreen />':n}function a(t,e){var n=[],r=0;return t.forEach(function(t){t.offset>r&&n.push({start:r,end:t.offset-1}),n.push({start:t.offset,end:t.offset+t.length,entityKey:t.key}),r=t.offset+t.length}),r<e&&n.push({start:r,end:e}),n}function i(t){var e=t.text,n=t.inlineStyleRanges,r={COLOR:new Array(e.length),BGCOLOR:new Array(e.length),FONTSIZE:new Array(e.length),FONTFAMILY:new Array(e.length),SUBSCRIPT:new Array(e.length),SUPERSCRIPT:new Array(e.length),CODE:new Array(e.length),STRIKETHROUGH:new Array(e.length),UNDERLINE:new Array(e.length),ITALIC:new Array(e.length),BOLD:new Array(e.length),length:e.length};return n&&n.length>0&&n.forEach(function(t){for(var e=t.offset,n=e+t.length,o=e;o<n;o+=1)t.style.startsWith("color-")?r.COLOR[o]=t.style.substring(6):t.style.startsWith("bgcolor-")?r.BGCOLOR[o]=t.style.substring(8):t.style.startsWith("fontsize-")?r.FONTSIZE[o]=t.style.substring(9):t.style.startsWith("fontfamily-")?r.FONTFAMILY[o]=t.style.substring(11):r[t.style]&&(r[t.style][o]=!0)}),r}function s(t,e,n){var r=!0;return n>0&&n<t.length?e.forEach(function(e){r=r&&t[e][n]===t[e][n-1]}):r=!1,r}function f(t,e){var n={};return t.COLOR[e]&&(n.COLOR=t.COLOR[e]),t.BGCOLOR[e]&&(n.BGCOLOR=t.BGCOLOR[e]),t.FONTSIZE[e]&&(n.FONTSIZE=t.FONTSIZE[e]),t.FONTFAMILY[e]&&(n.FONTFAMILY=t.FONTFAMILY[e]),t.SUBSCRIPT[e]&&(n.SUBSCRIPT=!0),t.SUPERSCRIPT[e]&&(n.SUPERSCRIPT=!0),t.CODE[e]&&(n.CODE=!0),t.STRIKETHROUGH[e]&&(n.STRIKETHROUGH=!0),t.UNDERLINE[e]&&(n.UNDERLINE=!0),t.ITALIC[e]&&(n.ITALIC=!0),t.BOLD[e]&&(n.BOLD=!0),n}function l(t,e,n,r){var o=[],u=t.text;if(u.length>0)for(var a=i(t),l=void 0,c=n;c<r;c+=1)c!==n&&s(a,e,c)?(l.text.push(u[c]),l.end=c+1):(l={styles:f(a,c),text:[u[c]],start:c,end:c+1},o.push(l));return o}function c(t){if(t&&t.length>0){var e=t.map(function(t){switch(t){case"\n":return"\\s\\s\n";case"&":return"&amp;";case"<":return"&lt;";case">":return"&gt;";default:return t}});return e.join("")}return""}function O(t,e){return"BOLD"===t?"**"+e+"**":"ITALIC"===t?"*"+e+"*":"UNDERLINE"===t?"__"+e+"__":"STRIKETHROUGH"===t?"~~"+e+"~~":"CODE"===t?"`"+e+"`":"SUPERSCRIPT"===t?"<sup>"+e+"</sup>":"SUBSCRIPT"===t?"<sub>"+e+"</sub>":e}function h(t,e){var n=e;return(0,v.forEach)(t,function(t,e){n=O(t,n,e)}),n}function y(t){var e=t.styles,n=t.text,r=c(n);if(e&&(e.COLOR||e.BGCOLOR||e.FONTSIZE||e.FONTFAMILY)){var o='style="';return e.COLOR&&(o+="color: "+e.COLOR+";"),e.BGCOLOR&&(o+="background-color: "+e.BGCOLOR+";"),e.FONTSIZE&&(o+="font-size: "+e.FONTSIZE+"px;"),e.FONTFAMILY&&(o+="font-family: "+e.FONTFAMILY+";"),o+='"',"<span "+o+">"+r+"</span>"}return r}function d(t,e,n){var r=[],o=l(t,["BOLD","ITALIC","UNDERLINE","STRIKETHROUGH","CODE","SUPERSCRIPT","SUBSCRIPT"],n.start,n.end),a="";o.forEach(function(e){var n=l(t,["COLOR","BGCOLOR","FONTSIZE","FONTFAMILY"],e.start,e.end),r="";n.forEach(function(t){r+=y(t)}),a+=h(e.styles,r)}),r.push(a);var i=r.join("");return void 0!==n.entityKey&&null!==n.entityKey&&(i=u(e,n.entityKey,i)),i}function g(t){if(t){for(var e=t,n=0;n<e.length&&" "===t[n];n+=1)e=e.replace(" ","&nbsp;");return e}return t}function I(t){if(t){for(var e=t,n=e.length-1;n>=0&&" "===e[n];n-=1)e=e.substring(0,n)+"&nbsp;"+e.substring(n+1);return e}return t}function R(t,e){if(o(t))return u(e,t.entityRanges[0].key,"");var n=[],r=a(t.entityRanges,t.text.length);return r.forEach(function(o,u){var a=d(t,e,o);0===u&&(a=g(a)),u===r.length-1&&(a=I(a)),n.push(a)}),n.join("")}function p(t){var e="";return(0,v.forEach)(t,function(t,n){e+=t+":"+n+";"}),e}function E(t,e){var n=p(t);return n?'<span style="'+n+'">'+e+"</span>":e}function L(t,e){var n=[];n.push(r(t));var o=R(t,e);return t.data&&(o=E(t.data,o)),n.push(o),n.push("\n"),n.join("")}function T(t){for(var e="",n=0;n<4*t;n+=1)e+=" ";return e}function S(t){var e=[];return t&&!function(){var n=t.blocks,r=t.entityMap;n&&n.length>0&&n.forEach(function(t){var n=L(t,r);(0,v.isList)(t.type)&&(n=T(t.depth)+n),e.push(n)})}(),e.join("")}Object.defineProperty(e,"__esModule",{value:!0}),e.getBlockTagSymbol=r,e.sameStyleAsPrevious=s,e.getStylesAtOffset=f,e.addInlineStyleMarkup=O,e.addStylePropertyMarkdown=y,e.trimLeadingZeros=g,e.trimTrailingZeros=I,e.getBlockContentMarkdown=R,e.getBlockStyle=p,e.default=S;var v=n(1),C={unstyled:"","header-one":"# ","header-two":"## ","header-three":"### ","header-four":"#### ","header-five":"##### ","header-six":"###### ","unordered-list-item":"- ","ordered-list-item":"1. ",blockquote:"> "}},function(t,e,n){"use strict";function r(t,e){if(t)for(var n in t)({}).hasOwnProperty.call(t,n)&&e(n,t[n])}function o(t){return void 0===t||null===t||0===t.length||0===t.trim().length}function u(t){return"unordered-list-item"===t||"ordered-list-item"===t}Object.defineProperty(e,"__esModule",{value:!0}),e.forEach=r,e.isEmptyString=o,e.isList=u},function(t,e,n){t.exports=n(0)}]);
module.exports=function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=2)}([function(t,e,n){"use strict";function r(t){return t.type&&N[t.type]}function o(t){return!!(t.entityRanges.length>0&&(0,C.isEmptyString)(t.text))}function u(t,e,n,r){var o=t[e];if("function"==typeof r){var u=r(o,n);if(u)return u}return"MENTION"===o.type?"["+n+"]("+o.data.url+")":"LINK"===o.type?"["+o.data.title+"]("+o.data.url+")":"IMAGE"===o.type?"!("+o.data.src+")":"EMBEDDED_LINK"===o.type?'<iframe width="'+o.data.width+'" height="'+o.data.height+'" src="'+o.data.src+'" frameBorder="0" allowFullScreen />':n}function i(t,e){var n=[];if(e)for(var r=0,o=0,u=t,i=e.trigger||"#",a=e.separator||" ";u.length>0&&o>=0;)if(u[0]===i?(o=0,r=0,u=u.substr(i.length)):(o=u.indexOf(a+i),o>=0&&(u=u.substr(o+(a+i).length),r+=o+a.length)),o>=0){var s=u.indexOf(a)>=0?u.indexOf(a):u.length,f=u.substr(0,s);f&&f.length>0&&n.push({offset:r,length:f.length+i.length,type:"HASHTAG"}),r+=i.length}return n}function a(t,e){var n=[],r=0,o=t.entityRanges.map(function(t){var e=t.offset,n=t.length,r=t.key;return{offset:e,length:n,key:r,type:"ENTITY"}});return o=o.concat(i(t.text,e)),o=o.sort(function(t,e){return t.offset-e.offset}),o.forEach(function(t){t.offset>r&&n.push({start:r,end:t.offset-1}),n.push({start:t.offset,end:t.offset+t.length,entityKey:t.key,type:t.type}),r=t.offset+t.length}),r<t.text.length&&n.push({start:r,end:t.text.length}),n}function s(t){var e=t.text,n=t.inlineStyleRanges,r={COLOR:new Array(e.length),BGCOLOR:new Array(e.length),FONTSIZE:new Array(e.length),FONTFAMILY:new Array(e.length),SUBSCRIPT:new Array(e.length),SUPERSCRIPT:new Array(e.length),CODE:new Array(e.length),STRIKETHROUGH:new Array(e.length),UNDERLINE:new Array(e.length),ITALIC:new Array(e.length),BOLD:new Array(e.length),length:e.length};return n&&n.length>0&&n.forEach(function(t){for(var e=t.offset,n=e+t.length,o=e;o<n;o+=1)t.style.startsWith("color-")?r.COLOR[o]=t.style.substring(6):t.style.startsWith("bgcolor-")?r.BGCOLOR[o]=t.style.substring(8):t.style.startsWith("fontsize-")?r.FONTSIZE[o]=t.style.substring(9):t.style.startsWith("fontfamily-")?r.FONTFAMILY[o]=t.style.substring(11):r[t.style]&&(r[t.style][o]=!0)}),r}function f(t,e,n){var r=!0;return n>0&&n<t.length?e.forEach(function(e){r=r&&t[e][n]===t[e][n-1]}):r=!1,r}function l(t,e){var n={};return t.COLOR[e]&&(n.COLOR=t.COLOR[e]),t.BGCOLOR[e]&&(n.BGCOLOR=t.BGCOLOR[e]),t.FONTSIZE[e]&&(n.FONTSIZE=t.FONTSIZE[e]),t.FONTFAMILY[e]&&(n.FONTFAMILY=t.FONTFAMILY[e]),t.SUBSCRIPT[e]&&(n.SUBSCRIPT=!0),t.SUPERSCRIPT[e]&&(n.SUPERSCRIPT=!0),t.CODE[e]&&(n.CODE=!0),t.STRIKETHROUGH[e]&&(n.STRIKETHROUGH=!0),t.UNDERLINE[e]&&(n.UNDERLINE=!0),t.ITALIC[e]&&(n.ITALIC=!0),t.BOLD[e]&&(n.BOLD=!0),n}function c(t,e,n,r){var o=[],u=t.text;if(u.length>0)for(var i=s(t),a=void 0,c=n;c<r;c+=1)c!==n&&f(i,e,c)?(a.text.push(u[c]),a.end=c+1):(a={styles:l(i,c),text:[u[c]],start:c,end:c+1},o.push(a));return o}function h(t){if(t&&t.length>0){var e=t.map(function(t){switch(t){case"\n":return"\\s\\s\n";case"&":return"&amp;";case"<":return"&lt;";case">":return"&gt;";default:return t}});return e.join("")}return""}function O(t,e){return"BOLD"===t?"**"+e+"**":"ITALIC"===t?"*"+e+"*":"UNDERLINE"===t?"__"+e+"__":"STRIKETHROUGH"===t?"~~"+e+"~~":"CODE"===t?"`"+e+"`":"SUPERSCRIPT"===t?"<sup>"+e+"</sup>":"SUBSCRIPT"===t?"<sub>"+e+"</sub>":e}function y(t,e){var n=e;return(0,C.forEach)(t,function(t,e){n=O(t,n,e)}),n}function g(t){var e=t.styles,n=t.text,r=h(n);if(e&&(e.COLOR||e.BGCOLOR||e.FONTSIZE||e.FONTFAMILY)){var o='style="';return e.COLOR&&(o+="color: "+e.COLOR+";"),e.BGCOLOR&&(o+="background-color: "+e.BGCOLOR+";"),e.FONTSIZE&&(o+="font-size: "+e.FONTSIZE+"px;"),e.FONTFAMILY&&(o+="font-family: "+e.FONTFAMILY+";"),o+='"',"<span "+o+">"+r+"</span>"}return r}function d(t,e,n,r){var o=[],i=c(t,["BOLD","ITALIC","UNDERLINE","STRIKETHROUGH","CODE","SUPERSCRIPT","SUBSCRIPT"],n.start,n.end),a="";i.forEach(function(e){var n=c(t,["COLOR","BGCOLOR","FONTSIZE","FONTFAMILY"],e.start,e.end),r="";n.forEach(function(t){r+=g(t)}),a+=y(e.styles,r)}),o.push(a);var s=o.join("");return"ENTITY"===n.type?void 0!==n.entityKey&&null!==n.entityKey&&(s=u(e,n.entityKey,s,r)):"HASHTAG"===n.type&&(s="["+s+"]("+s+")"),s}function p(t){if(t){for(var e=t,n=0;n<e.length&&" "===t[n];n+=1)e=e.replace(" ","&nbsp;");return e}return t}function I(t){if(t){for(var e=t,n=e.length-1;n>=0&&" "===e[n];n-=1)e=e.substring(0,n)+"&nbsp;"+e.substring(n+1);return e}return t}function E(t,e,n,r){if(o(t))return u(e,t.entityRanges[0].key,void 0,r);var i=[],s=a(t,n);return s.forEach(function(n,r){var o=d(t,e,n);0===r&&(o=p(o)),r===s.length-1&&(o=I(o)),i.push(o)}),i.join("")}function R(t){var e="";return(0,C.forEach)(t,function(t,n){e+=t+":"+n+";"}),e}function T(t,e){var n=R(t);return n?'<span style="'+n+'">'+e+"</span>":e}function L(t,e,n,o){var u=[];u.push(r(t));var i=E(t,e,n,o);return t.data&&(i=T(t.data,i)),u.push(i),u.push("\n"),u.join("")}function S(t){for(var e="",n=0;n<4*t;n+=1)e+=" ";return e}function v(t,e,n){var r=[];return t&&!function(){var o=t.blocks,u=t.entityMap;o&&o.length>0&&o.forEach(function(t){var o=L(t,u,e,n);(0,C.isList)(t.type)&&(o=S(t.depth)+o),r.push(o)})}(),r.join("")}Object.defineProperty(e,"__esModule",{value:!0}),e.getBlockTagSymbol=r,e.sameStyleAsPrevious=f,e.getStylesAtOffset=l,e.addInlineStyleMarkdown=O,e.addStylePropertyMarkdown=g,e.trimLeadingZeros=p,e.trimTrailingZeros=I,e.getBlockContentMarkdown=E,e.getBlockStyle=R,e.default=v;var C=n(1),N={unstyled:"","header-one":"# ","header-two":"## ","header-three":"### ","header-four":"#### ","header-five":"##### ","header-six":"###### ","unordered-list-item":"- ","ordered-list-item":"1. ",blockquote:"> "}},function(t,e,n){"use strict";function r(t,e){if(t)for(var n in t)({}).hasOwnProperty.call(t,n)&&e(n,t[n])}function o(t){return void 0===t||null===t||0===t.length||0===t.trim().length}function u(t){return"unordered-list-item"===t||"ordered-list-item"===t}Object.defineProperty(e,"__esModule",{value:!0}),e.forEach=r,e.isEmptyString=o,e.isList=u},function(t,e,n){t.exports=n(0)}]);
{
"name": "draftjs-to-markdown",
"version": "0.3.0",
"version": "0.3.1",
"description": "A library for draftjs to markdown conversion.",

@@ -5,0 +5,0 @@ "main": "lib/draftjs-to-markdown.js",

@@ -17,5 +17,17 @@ # DraftJS TO Markdown

const rawContentState = convertToRaw(editorState.getCurrentContent());
const markup = draftToMarkdown(contentState);
const markup = draftToMarkdown(contentState, hashConfig, customEntityTransform);
```
The function parameters are:
1. **contentState**: Its instance of [RawDraftContentState](https://facebook.github.io/draft-js/docs/api-reference-data-conversion.html#content)
2. **hashConfig**: Its configuration object for hashtag, its required only if hashtags are used. If the object is not defined hashtags will be output as simple text in the markdown.
```
hashConfig = {
trigger: '#',
separator: ' ',
}
```
Here trigger is character that marks starting of hashtag (default '#') and separator is character that separates characters (default ' ').
3. **customEntityTransform**: Its function to render custom defined entities by user, its also optional.
## Supported conversions

@@ -54,9 +66,13 @@ Following is the list of conversions it supports:

6. Converts atomic entity image to image tag using entity data src for image source: `!(Image Source)`.
6. Converts hashtags to :`[hashtag](hashtag)`.
7. Converts embedded links to HTML iframe tags <iframe ... />.
7. Converts atomic entity image to image tag using entity data src for image source: `!(Image Source)`.
8. For block level styles like text-alignment add `<span>` with `style` property around block content.
8. Converts embedded links to HTML iframe tags <iframe ... />.
9. For block level styles like text-alignment add `<span>` with `style` property around block content.
10. Supports using function `customEntityTransform` for custom draftjs entities.
## License
MIT.
MIT.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc