From ccc93da8e34e8c1c7d6d029e02f806910a96d24a Mon Sep 17 00:00:00 2001 From: Walisson Rodrigo Date: Wed, 3 Feb 2021 10:20:06 -0300 Subject: [PATCH 01/11] include customStyleTransform to replace inline syntax of standard styles. --- js/index.js | 66 ++++++++++++++++++++++++-------------- lib/draftjs-to-markdown.js | 2 +- package.json | 4 +-- readme.md | 20 +++++++++++- 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/js/index.js b/js/index.js index c99ace7..37f43c6 100644 --- a/js/index.js +++ b/js/index.js @@ -78,7 +78,7 @@ function getHashtagRanges(blockText, hashConfig) { } } if (startIndex >= 0) { - const endIndex = text.indexOf(separator) >= 0 ? text.indexOf(separator) : text.length; + const endIndex = text.indexOf(separator) >= 0 ? text.indexOf(separator) : text.length; const hashtagText = text.substr(0, endIndex); if (hashtagText && hashtagText.length > 0) { sections.push({ @@ -187,7 +187,7 @@ export function sameStyleAsPrevious(inlineStyles, styles, index) { let sameStyled = true; if (index > 0 && index < inlineStyles.length) { styles.forEach((style) => { - sameStyled = sameStyled + sameStyled = sameStyled && inlineStyles[style][index] === inlineStyles[style][index - 1]; }); } else { @@ -295,26 +295,32 @@ function getSectionText(text) { * Function returns markdown for inline style symbols. */ export function addInlineStyleMarkdown(style, content) { - if (style === 'BOLD') { - return `**${content}**`; + if (style.BOLD) { + return `${style.BOLD}${content}${style.BOLD}`; } - if (style === 'ITALIC') { - return `*${content}*`; + if (style.ITALIC) { + return `${style.ITALIC}${content}${style.ITALIC}`; } - if (style === 'UNDERLINE') { - return `__${content}__`; + if (style.UNDERLINE) { + return `${style.UNDERLINE}${content}${style.UNDERLINE}`; } - if (style === 'STRIKETHROUGH') { - return `~~${content}~~`; + if (style.STRIKETHROUGH) { + return `${style.STRIKETHROUGH}${content}${style.STRIKETHROUGH}`; } - if (style === 'CODE') { - return `\`${content}\``; + if (style.CODE) { + return `${style.CODE}${content}${style.CODE}`; } - if (style === 'SUPERSCRIPT') { - return `${content}`; + if (style['CODE-BLOCK']) { + return `${style['CODE-BLOCK']}${content}${style['CODE-BLOCK']}`; } - if (style === 'SUBSCRIPT') { - return `${content}`; + if (style.BLOCKQUOTE) { + return `${style.BLOCKQUOTE}${content}${style.BLOCKQUOTE}`; + } + if (style.SUPERSCRIPT) { + return `${style.SUPERSCRIPT}${content}${style.SUPERSCRIPT}`; + } + if (style.SUBSCRIPT) { + return `${style.SUBSCRIPT}${content}${style.SUBSCRIPT}`; } return content; } @@ -365,18 +371,27 @@ export function addStylePropertyMarkdown(styleSection) { * An entity section is a continuous section in a block * to which same entity or no entity is applicable. */ -function getSectionMarkdown(block, entityMap, section, customEntityTransform) { +function getSectionMarkdown( + block, + entityMap, + section, + customEntityTransform, + customStyleTransform, +) { const entitySectionMarkdown = []; const styleSections = getStyleSections( block, [ - 'BOLD', - 'ITALIC', - 'UNDERLINE', - 'STRIKETHROUGH', - 'CODE', - 'SUPERSCRIPT', - 'SUBSCRIPT', + { BOLD: '**' }, + { ITALIC: '*' }, + { UNDERLINE: '__' }, + { STRIKETHROUGH: '~~' }, + { CODE: '`' }, + { 'CODE-BLOCK': '```' }, + { BLOCKQUOTE: '>' }, + { SUPERSCRIPT: '' }, + { SUBSCRIPT: '' }, + ...customStyleTransform, ], section.start, section.end, @@ -464,6 +479,7 @@ export function getBlockContentMarkdown( entityMap, hashConfig, customEntityTransform, + customStyleTransform, ) { if (isAtomicBlock(block)) { return getEntityMarkdown( @@ -481,6 +497,7 @@ export function getBlockContentMarkdown( entityMap, section, customEntityTransform, + customStyleTransform, ); if (index === 0) { sectionText = trimLeadingZeros(sectionText); @@ -533,6 +550,7 @@ function getBlockMarkdown( entityMap, hashConfig, customEntityTransform, + config.customStyleTransform, ); if (block.data) { blockContentMarkdown = getBlockStyleProperty( diff --git a/lib/draftjs-to-markdown.js b/lib/draftjs-to-markdown.js index 6f12e63..b66fa74 100644 --- a/lib/draftjs-to-markdown.js +++ b/lib/draftjs-to-markdown.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.draftjsToMarkdown=e():t.draftjsToMarkdown=e()}(window,function(){return r={},o.m=n=[function(t,e,n){t.exports=n(1)},function(t,e,n){"use strict";function i(t,e){if(t)for(var n in t)!{}.hasOwnProperty.call(t,n)||e(n,t[n])}function f(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function s(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}n.r(e),n.d(e,"sameStyleAsPrevious",function(){return d}),n.d(e,"getStylesAtOffset",function(){return p}),n.d(e,"addInlineStyleMarkdown",function(){return g}),n.d(e,"addStylePropertyMarkdown",function(){return T}),n.d(e,"trimLeadingZeros",function(){return b}),n.d(e,"trimTrailingZeros",function(){return R}),n.d(e,"getBlockContentMarkdown",function(){return E}),n.d(e,"getBlockStyle",function(){return S}),n.d(e,"default",function(){return r});var l={unstyled:"","header-one":"# ","header-two":"## ","header-three":"### ","header-four":"#### ","header-five":"##### ","header-six":"###### ","unordered-list-item":"- ","ordered-list-item":"1. ",blockquote:"> ",code:" "};function O(t,e,n,r){var o=t[e];if("function"==typeof r){var c=r(o,n);if(c)return c}return"MENTION"===o.type?"[".concat(n,"](").concat(o.data.url,")"):"LINK"===o.type?"[".concat(n,"](").concat(o.data.url,")"):"IMAGE"===o.type?"![".concat(o.data.alt||"","](").concat(o.data.src,")"):"EMBEDDED_LINK"===o.type?'