Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/missing spaces #41

Merged
merged 2 commits into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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