Skip to content

Commit

Permalink
Merge pull request #123 from Rosey/trailing_link_bug
Browse files Browse the repository at this point in the history
Trailing link bug
  • Loading branch information
Rose authored Jun 14, 2020
2 parents 9be3aa7 + b5a7d15 commit 8d8b931
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/draft-to-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions test/draft-to-markdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":{}}]};
Expand Down

0 comments on commit 8d8b931

Please sign in to comment.