From 54ffc222619ad9ab54f5a9f6b22362dba246e245 Mon Sep 17 00:00:00 2001 From: Rose Robertson Date: Fri, 15 Jul 2016 23:11:57 -0700 Subject: [PATCH] Do not include whitespace when applying inline markdown styles If you test out markdown on a gist, markdown like this: ```markdown **Test ** ``` (with trailing or leading whitespace) is not properly parsed. This change only surrounds the text with markdown inline styles and preserves the whitespace, but doesn't attempt to style it, fixing the issue of incompatibility with markdown parsers. --- src/stateToMarkdown.js | 11 ++++++----- test/test-cases.txt | 8 ++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/stateToMarkdown.js b/src/stateToMarkdown.js index c23f3ea..fbb1072 100644 --- a/src/stateToMarkdown.js +++ b/src/stateToMarkdown.js @@ -19,6 +19,7 @@ const { } = INLINE_STYLE; const CODE_INDENT = ' '; +const STRIP_WHITESPACE = /([\s]*)([\W\w]*[^\s]+)([\s]*)/; class MarkupGenerator { blocks: Array; @@ -201,21 +202,21 @@ class MarkupGenerator { } let content = encodeContent(text); if (style.has(BOLD)) { - content = `**${content}**`; + content = content.replace(STRIP_WHITESPACE, '$1**$2**$3'); } if (style.has(UNDERLINE)) { // TODO: encode `+`? - content = `++${content}++`; + content = content.replace(STRIP_WHITESPACE, '$1++$2++$3'); } if (style.has(ITALIC)) { - content = `_${content}_`; + content = content.replace(STRIP_WHITESPACE, '$1_$2_$3'); } if (style.has(STRIKETHROUGH)) { // TODO: encode `~`? - content = `~~${content}~~`; + content = content.replace(STRIP_WHITESPACE, '$1~~$2~~$3'); } if (style.has(CODE)) { - content = (blockType === BLOCK_TYPE.CODE) ? content : '`' + content + '`'; + content = (blockType === BLOCK_TYPE.CODE) ? content : content.replace(STRIP_WHITESPACE, '$1`$2`$3'); } return content; }).join(''); diff --git a/test/test-cases.txt b/test/test-cases.txt index 799d13a..1ab30a4 100644 --- a/test/test-cases.txt +++ b/test/test-cases.txt @@ -6,10 +6,18 @@ a {"entityMap":{},"blocks":[{"key":"99n0j","text":"asdf","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":3,"length":1,"style":"BOLD"}],"entityRanges":[]}]} asd**f** +>> Inline style with whitespace +{"entityMap":{},"blocks":[{"key":"99n0j","text":"asdf ","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":6,"style":"BOLD"}],"entityRanges":[]}]} +**asdf** + >> Nested Inline Style {"entityMap":{},"blocks":[{"key":"9nc73","text":"BoldItalic","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":10,"style":"BOLD"},{"offset":0,"length":10,"style":"ITALIC"}],"entityRanges":[]}]} _**BoldItalic**_ +>> Nested Inline Style with whitespace +{"entityMap":{},"blocks":[{"key":"9nc73","text":"BoldItalic ","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":14,"style":"BOLD"},{"offset":0,"length":10,"style":"ITALIC"}],"entityRanges":[]}]} +_**BoldItalic**_ + >> Adjacent Inline Style {"entityMap":{},"blocks":[{"key":"9nc73","text":"BoldItalic","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":4,"length":6,"style":"BOLD"},{"offset":0,"length":4,"style":"ITALIC"}],"entityRanges":[]}]} _Bold_**Italic**