markdown-draft-js
Advanced tools
Comparing version 0.4.1 to 0.4.2
'use strict'; | ||
var SINGLE_SPACE_CHARACTER = /\u0020*$/; | ||
var TRAILING_WHITESPACE = /[ |\u0020|\t]*$/; | ||
@@ -200,7 +200,2 @@ // A map of draftjs block types -> markdown open and close characters | ||
Array.from(block.text).some(function (character, characterIndex) { | ||
var initialMarkdownStringLength = markdownString.length; | ||
markdownString = markdownString.replace(SINGLE_SPACE_CHARACTER, ''); | ||
var newMarkdownStringLength = markdownString.length; | ||
var totalSpacesToReadd = initialMarkdownStringLength - newMarkdownStringLength; | ||
// Close any entity tags that need closing | ||
@@ -226,3 +221,6 @@ block.entityRanges.forEach(function (range, rangeIndex) { | ||
if (styleItem) { | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += styleItem.close(); | ||
markdownString += trailingWhitespace[0]; | ||
} | ||
@@ -233,3 +231,8 @@ } | ||
// Close the actual inline style being closed | ||
// Have to trim whitespace first and then re-add after because markdown can't handle leading/trailing whitespace | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += (customStyleItems[style.style] || StyleItems[style.style]).close(); | ||
markdownString += trailingWhitespace[0]; | ||
@@ -251,7 +254,2 @@ // Handle nested case - reopen any inline styles after closing the parent | ||
// We removed trailing whitespace before closing markdown tags because | ||
// markdown doesn't play nice with trailing whitespace. | ||
// But we want to preserve it, so we re-add it after closing all tags. | ||
markdownString += ' '.repeat(totalSpacesToReadd); | ||
// Open any inline tags that need opening | ||
@@ -292,3 +290,6 @@ block.inlineStyleRanges.forEach(function (style, styleIndex) { | ||
openInlineStyles.reverse().forEach(function (style) { | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += (customStyleItems[style.style] || StyleItems[style.style]).close(); | ||
markdownString += trailingWhitespace[0]; | ||
}); | ||
@@ -295,0 +296,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Rose Robertson", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"license": "MIT", | ||
@@ -46,2 +46,3 @@ "repository": { | ||
"karma-jasmine": "1.0.2", | ||
"jasmine-core": "2.8.0", | ||
"watchify": "3.7.0" | ||
@@ -48,0 +49,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
const SINGLE_SPACE_CHARACTER = /\u0020*$/; | ||
const TRAILING_WHITESPACE = /[ |\u0020|\t]*$/; | ||
@@ -202,7 +202,2 @@ // A map of draftjs block types -> markdown open and close characters | ||
Array.from(block.text).some(function (character, characterIndex) { | ||
var initialMarkdownStringLength = markdownString.length; | ||
markdownString = markdownString.replace(SINGLE_SPACE_CHARACTER, ''); | ||
var newMarkdownStringLength = markdownString.length; | ||
var totalSpacesToReadd = initialMarkdownStringLength - newMarkdownStringLength; | ||
// Close any entity tags that need closing | ||
@@ -228,3 +223,6 @@ block.entityRanges.forEach(function (range, rangeIndex) { | ||
if (styleItem) { | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += styleItem.close(); | ||
markdownString += trailingWhitespace[0]; | ||
} | ||
@@ -235,3 +233,8 @@ } | ||
// Close the actual inline style being closed | ||
// Have to trim whitespace first and then re-add after because markdown can't handle leading/trailing whitespace | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += (customStyleItems[style.style] || StyleItems[style.style]).close(); | ||
markdownString += trailingWhitespace[0]; | ||
@@ -253,7 +256,2 @@ // Handle nested case - reopen any inline styles after closing the parent | ||
// We removed trailing whitespace before closing markdown tags because | ||
// markdown doesn't play nice with trailing whitespace. | ||
// But we want to preserve it, so we re-add it after closing all tags. | ||
markdownString += ' '.repeat(totalSpacesToReadd); | ||
// Open any inline tags that need opening | ||
@@ -294,3 +292,6 @@ block.inlineStyleRanges.forEach(function (style, styleIndex) { | ||
openInlineStyles.reverse().forEach(function (style) { | ||
var trailingWhitespace = TRAILING_WHITESPACE.exec(markdownString); | ||
markdownString = markdownString.slice(0, markdownString.length - trailingWhitespace[0].length); | ||
markdownString += (customStyleItems[style.style] || StyleItems[style.style]).close(); | ||
markdownString += trailingWhitespace[0]; | ||
}); | ||
@@ -297,0 +298,0 @@ |
import { markdownToDraft, draftToMarkdown } from '../src/index'; | ||
describe('draftToMarkdown', function () { | ||
it('renders inline styled text with trailing whitespace correctly', function () { | ||
/* eslint-disable */ | ||
var rawObject = {"entityMap":{},"blocks":[{"key":"dvfr1","text":"Test Bold Text Test","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":5,"length":10,"style":"BOLD"}],"entityRanges":[],"data":{}}]}; | ||
/* eslint-enable */ | ||
var markdown = draftToMarkdown(rawObject); | ||
expect(markdown).toEqual('Test **Bold Text** Test'); | ||
}); | ||
it('renders inline styled text with trailing whitespace correctly when trailing whitespace is the last character', function () { | ||
/* eslint-disable */ | ||
var rawObject = {"entityMap":{},"blocks":[{"key":"dvfr1","text":"Test Bold Text ","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":5,"length":10,"style":"BOLD"}],"entityRanges":[],"data":{}}]}; | ||
/* eslint-enable */ | ||
var markdown = draftToMarkdown(rawObject); | ||
expect(markdown).toEqual('Test **Bold Text** '); | ||
}); | ||
it('renders nested inline styled text with trailing whitespace correctly', function () { | ||
/* eslint-disable */ | ||
var rawObject = {"entityMap":{},"blocks":[{"key":"dvfr1","text":"Test Bold Text Test","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":10,"style":"ITALIC"},{"offset":5,"length":9,"style":"BOLD"}],"entityRanges":[],"data":{}}]}; | ||
/* eslint-enable */ | ||
var markdown = draftToMarkdown(rawObject); | ||
expect(markdown).toEqual('_Test **Bold**_ **Text** Test'); | ||
}); | ||
it('renders links correctly', function () { | ||
@@ -5,0 +32,0 @@ /* eslint-disable */ |
66832
1383
15