Skip to content

Commit

Permalink
Merge pull request #41 from kadikraman/bugfix/missing-spaces
Browse files Browse the repository at this point in the history
Bugfix/missing spaces
  • Loading branch information
kadikraman authored Jul 5, 2018
2 parents f5bd85f + f0481dc commit c80cc7f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 102 deletions.
1 change: 1 addition & 0 deletions src/draftjsToMd.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ function draftjsToMd(raw, extraMarkdownDict) {
newText += endingStyle.symbol;

newText = fixWhitespacesInsideStyle(newText, endingStyle);
totalOffset += endingStyle.symbol.length;
}

return newText;
Expand Down
225 changes: 123 additions & 102 deletions test/draftjsToMd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,45 @@ describe('draftjsToMd', () => {
draftjsToMd(raw).should.equal(expectedMarkdown);
});

it('converts block quotes to markdown correctly', () => {
const raw = {
entityMap: {},
blocks: [
{
text: 'Here is a block quote.',
type: 'blockquote',
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
data: {}
}
]
};
const expectedMarkdown = '> Here is a block quote.';
draftjsToMd(raw).should.equal(expectedMarkdown);
});

it('inline styles do not remove spaces', () => {
const raw = {
entityMap: {},
blocks: [
{
data: {},
depth: 0,
entityRanges: [],
inlineStyleRanges: [
{ offset: 0, length: 4, style: 'BOLD' },
{ offset: 12, length: 4, style: 'ITALIC' }
],
text: 'This is not fine',
type: 'unstyled'
}
]
};
const expectedMarkdown = '__This__ is not *fine*';
draftjsToMd(raw).should.equal(expectedMarkdown);
});

describe('custom markdownDict', () => {
const customMarkdownDict = {
BOLD: '**',
Expand Down Expand Up @@ -685,124 +724,106 @@ describe('draftjsToMd', () => {
const expectedMarkdown = 'No style ~~strike-through~~ no style.';
draftjsToMd(raw, customMarkdownDict).should.equal(expectedMarkdown);
});
});

describe('Images', () => {
it('converts image media to markdown correctly with url/filename', () => {
const raw = {
entityMap: {
1: {
type: 'image',
mutability: 'IMMUTABLE',
data: {
url: '//images.mine.com/myImage.jpg',
fileName: 'My Image Name'
}
}
},
blocks: [
{
key: 'fag2v',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
]
describe('Images', () => {
it('converts image media to markdown correctly with url/filename', () => {
const raw = {
entityMap: {
1: {
type: 'image',
mutability: 'IMMUTABLE',
data: {
url: '//images.mine.com/myImage.jpg',
fileName: 'My Image Name'
}
]
};
const expectedMarkdown = '![My Image Name](//images.mine.com/myImage.jpg)';
draftjsToMd(raw).should.equal(expectedMarkdown);
});

it('converts image media to markdown correctly with src format', () => {
const raw = {
entityMap: {
1: {
type: 'image',
mutability: 'IMMUTABLE',
data: {
src: '//images.mine.com/myImage.jpg'
}
},
blocks: [
{
key: 'fag2v',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
}
},
blocks: [
{
key: 'fag2v',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
]
}
]
};
const expectedMarkdown = '![](//images.mine.com/myImage.jpg)';
draftjsToMd(raw).should.equal(expectedMarkdown);
});
]
}
]
};
const expectedMarkdown = '![My Image Name](//images.mine.com/myImage.jpg)';
draftjsToMd(raw).should.equal(expectedMarkdown);
});

describe('Videos', () => {
it('converts video media created by draft-js-video-plugin to markdown correctly with src format', () => {
// eslint-disable-line max-len
const raw = {
entityMap: {
1: {
type: 'draft-js-video-plugin-video',
mutability: 'IMMUTABLE',
data: {
src: '//youtu.be/wfWIs2gFTAM'
}
}
},
blocks: [
{
key: 'ov7r',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
]
it('converts image media to markdown correctly with src format', () => {
const raw = {
entityMap: {
1: {
type: 'image',
mutability: 'IMMUTABLE',
data: {
src: '//images.mine.com/myImage.jpg'
}
]
};
const expectedMarkdown = '[[ embed url=//youtu.be/wfWIs2gFTAM ]]';
draftjsToMd(raw).should.equal(expectedMarkdown);
});
}
},
blocks: [
{
key: 'fag2v',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
]
}
]
};
const expectedMarkdown = '![](//images.mine.com/myImage.jpg)';
draftjsToMd(raw).should.equal(expectedMarkdown);
});
});

it('converts block quotes to markdown correctly', () => {
describe('Videos', () => {
it('converts video media created by draft-js-video-plugin to markdown correctly with src format', () => {
// eslint-disable-line max-len
const raw = {
entityMap: {},
entityMap: {
1: {
type: 'draft-js-video-plugin-video',
mutability: 'IMMUTABLE',
data: {
src: '//youtu.be/wfWIs2gFTAM'
}
}
},
blocks: [
{
text: 'Here is a block quote.',
type: 'blockquote',
key: 'ov7r',
text: ' ',
type: 'atomic',
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
data: {}
entityRanges: [
{
offset: 0,
length: 1,
key: 1
}
]
}
]
};
const expectedMarkdown = '> Here is a block quote.';
const expectedMarkdown = '[[ embed url=//youtu.be/wfWIs2gFTAM ]]';
draftjsToMd(raw).should.equal(expectedMarkdown);
});
});
Expand Down

0 comments on commit c80cc7f

Please sign in to comment.