From 93e0d3644a9ce0a3e928a04bdc94138e6e5005e7 Mon Sep 17 00:00:00 2001 From: Rose Robertson Date: Wed, 10 Jun 2020 09:49:41 -0700 Subject: [PATCH 1/2] Add test for surrogate pair bug with trailing links As outlined in this issue: https://github.com/Rosey/markdown-draft-js/issues/122 --- test/draft-to-markdown.spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/draft-to-markdown.spec.js b/test/draft-to-markdown.spec.js index b8b01cd..b13c0d7 100644 --- a/test/draft-to-markdown.spec.js +++ b/test/draft-to-markdown.spec.js @@ -218,6 +218,31 @@ describe('draftToMarkdown', function () { expect(markdown).toEqual('This is a test of [a link](https://google.com)\n\nAnd [perhaps](https://facebook.github.io/draft-js/) we should test once more.'); }); + it('renders links with surrogate pairs (e.g. some emoji) correctly', function () { + /* eslint-disable */ + var rawObject = { + "blocks": [{ + "key": "eubc2", + "text": "🙋 link link", + "type": "unstyled", + "depth": 0, + "inlineStyleRanges": [], + "entityRanges": [{"offset": 2, "length": 4, "key": 0}, {"offset": 7, "length": 4, "key": 1}], + "data": {} + }], + "entityMap": { + "0": { + "type": "LINK", + "mutability": "MUTABLE", + "data": {"url": "https://link.com", "href": "https://link.com"} + }, "1": {"type": "LINK", "mutability": "MUTABLE", "data": {"url": "https://link.com"}} + } + } + /* eslint-enable */ + var markdown = draftToMarkdown(rawObject); + expect(markdown).toEqual('🙋 [link](https://link.com) [link](https://link.com)'); + }); + it('renders links with a HREF correctly', function () { /* eslint-disable */ var rawObject = {"entityMap":{"0":{"type":"LINK","mutability":"MUTABLE","data":{"href":"https://google.com"}},"1":{"type":"LINK","mutability":"MUTABLE","data":{"href":"https://facebook.github.io/draft-js/"}}},"blocks":[{"key":"58spd","text":"This is a test of a link","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":18,"length":6,"key":0}],"data":{}},{"key":"9ln6g","text":"","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"3euar","text":"And perhaps we should test once more.","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":4,"length":7,"key":1}],"data":{}}]}; From b5a7d15ff694a6233a44e8e515261f66fc30134a Mon Sep 17 00:00:00 2001 From: Rose Robertson Date: Wed, 10 Jun 2020 09:52:03 -0700 Subject: [PATCH 2/2] Fix issue with surrogate pairs messing up links at EOL The closing code for entity items was sometimes not run at the end of a block if the block text included emoji made up of multiple characters. In cases such as this. string.length !== Array.from(string).length, the latter being a more accurate representation of how many characters there actually are. --- src/draft-to-markdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/draft-to-markdown.js b/src/draft-to-markdown.js index a15f1bd..586488e 100644 --- a/src/draft-to-markdown.js +++ b/src/draft-to-markdown.js @@ -436,7 +436,7 @@ function renderBlock(block, index, rawDraftObject, options) { // Close any remaining entity tags block.entityRanges.forEach(function (range, rangeIndex) { - if (range.offset + range.length === block.text.length) { + if (range.offset + range.length === Array.from(block.text).length) { var entity = rawDraftObject.entityMap[range.key]; if (customEntityItems[entity.type] || EntityItems[entity.type]) { markdownString += (customEntityItems[entity.type] || EntityItems[entity.type]).close(entity);