Skip to content

Commit 46709e9

Browse files
committed
Removed two +1s that cancel eachother out
Also removed if (previousBlockEndingLine) because this caused it to miss empty lines at the start of text. Added test for this case.
1 parent 97f30c2 commit 46709e9

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/markdown-to-draft.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function markdownToDraft(string, options = {}) {
223223
var entityMap = {}; // entitymap will be returned as part of the final draftjs raw object
224224
var parsedData = md.parse(string, {}); // remarkable js takes markdown and makes it an array of style objects for us to easily parse
225225
var currentListType = null; // Because of how remarkable's data is formatted, we need to cache what kind of list we're currently dealing with
226-
var previousBlockEndingLine = 1;
226+
var previousBlockEndingLine = 0;
227227

228228
// Allow user to define custom BlockTypes and Entities if they so wish
229229
const BlockTypes = Object.assign({}, DefaultBlockTypes, options.blockTypes || {});
@@ -277,20 +277,18 @@ function markdownToDraft(string, options = {}) {
277277
if (block && options.preserveNewlines) {
278278
// Re: previousBlockEndingLine.... omg.
279279
// So remarkable strips out empty newlines and doesn't make any entities to parse to restore them
280-
// the only solution I could find is that there's a 2-value array on each block item called "lines" which is the start end line of the block element.
280+
// the only solution I could find is that there's a 2-value array on each block item called "lines" which is the start and end line of the block element.
281281
// by keeping track of the PREVIOUS block element ending line and the NEXT block element starting line, we can find the difference between the new lines and insert
282282
// an appropriate number of extra paragraphs to re-create those newlines in draftjs.
283283
// This is probably my least favourite thing in this file, but not sure what could be better.
284-
if (previousBlockEndingLine) {
285-
var totalEmptyParagraphsToCreate = item.lines[0] - previousBlockEndingLine + 1;
286-
for (var i = 0; i < totalEmptyParagraphsToCreate; i++) {
287-
blocks.push(DefaultBlockTypes.paragraph_open());
288-
}
284+
var totalEmptyParagraphsToCreate = item.lines[0] - previousBlockEndingLine;
285+
for (var i = 0; i < totalEmptyParagraphsToCreate; i++) {
286+
blocks.push(DefaultBlockTypes.paragraph_open());
289287
}
290288
}
291289

292290
if (block) {
293-
previousBlockEndingLine = item.lines[1] + 1;
291+
previousBlockEndingLine = item.lines[1];
294292
blocks.push(block);
295293
}
296294
}

test/idempotency.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ describe('idempotency', function () {
2828

2929
expect(markdownFromDraft).toEqual(markdownString);
3030

31+
markdownString = '\n\na';
32+
draftJSObject = markdownToDraft(markdownString, {preserveNewlines: true});
33+
markdownFromDraft = draftToMarkdown(draftJSObject, {preserveNewlines: true});
34+
expect(markdownFromDraft).toEqual(markdownString);
35+
3136
});
3237

3338
it('renders new lines text correctly with styled blocks', function () {

0 commit comments

Comments
 (0)