Skip to content

Commit 8d8b931

Browse files
author
Rose
authored
Merge pull request Rosey#123 from Rosey/trailing_link_bug
Trailing link bug
2 parents 9be3aa7 + b5a7d15 commit 8d8b931

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/draft-to-markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ function renderBlock(block, index, rawDraftObject, options) {
436436

437437
// Close any remaining entity tags
438438
block.entityRanges.forEach(function (range, rangeIndex) {
439-
if (range.offset + range.length === block.text.length) {
439+
if (range.offset + range.length === Array.from(block.text).length) {
440440
var entity = rawDraftObject.entityMap[range.key];
441441
if (customEntityItems[entity.type] || EntityItems[entity.type]) {
442442
markdownString += (customEntityItems[entity.type] || EntityItems[entity.type]).close(entity);

test/draft-to-markdown.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,31 @@ describe('draftToMarkdown', function () {
218218
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.');
219219
});
220220

221+
it('renders links with surrogate pairs (e.g. some emoji) correctly', function () {
222+
/* eslint-disable */
223+
var rawObject = {
224+
"blocks": [{
225+
"key": "eubc2",
226+
"text": "🙋 link link",
227+
"type": "unstyled",
228+
"depth": 0,
229+
"inlineStyleRanges": [],
230+
"entityRanges": [{"offset": 2, "length": 4, "key": 0}, {"offset": 7, "length": 4, "key": 1}],
231+
"data": {}
232+
}],
233+
"entityMap": {
234+
"0": {
235+
"type": "LINK",
236+
"mutability": "MUTABLE",
237+
"data": {"url": "https://link.com", "href": "https://link.com"}
238+
}, "1": {"type": "LINK", "mutability": "MUTABLE", "data": {"url": "https://link.com"}}
239+
}
240+
}
241+
/* eslint-enable */
242+
var markdown = draftToMarkdown(rawObject);
243+
expect(markdown).toEqual('🙋 [link](https://link.com) [link](https://link.com)');
244+
});
245+
221246
it('renders links with a HREF correctly', function () {
222247
/* eslint-disable */
223248
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":{}}]};

0 commit comments

Comments
 (0)