Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/draft-js-export-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"watch": "babel src --watch --out-dir lib --ignore \"_*\""
},
"dependencies": {
"draft-js-utils": "^1.3.3"
"draft-js-utils": "^1.3.3",
"markdown-escapes": "1.0.3"
},
"peerDependencies": {
"draft-js": ">=0.10.0",
Expand Down
18 changes: 11 additions & 7 deletions packages/draft-js-export-markdown/src/stateToMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ENTITY_TYPE,
INLINE_STYLE,
} from 'draft-js-utils';
import markdownEscapes from 'markdown-escapes';

import type {ContentState, ContentBlock} from 'draft-js';

Expand Down Expand Up @@ -194,6 +195,13 @@ class MarkupGenerator {
}
}

encodeContent(text) {
const markdownCharacters = markdownEscapes({gfm: this.options.gfm});

const regex = new RegExp(`[${'\\' + markdownCharacters.join('\\')}]`, 'g');
return text.replace(regex, '\\$&');
}

renderBlockContent(block: ContentBlock): string {
let {contentState} = this;
let blockType = block.getType();
Expand Down Expand Up @@ -224,19 +232,19 @@ class MarkupGenerator {
if (style.has(CODE)) {
return '`' + encodeCode(content) + '`';
}
content = encodeContent(text);

content = this.encodeContent(text);

if (style.has(BOLD)) {
content = `**${content}**`;
}
if (style.has(UNDERLINE)) {
// TODO: encode `+`?
content = `++${content}++`;
}
if (style.has(ITALIC)) {
content = `_${content}_`;
}
if (style.has(STRIKETHROUGH)) {
// TODO: encode `~`?
content = `~~${content}~~`;
}
return content;
Expand Down Expand Up @@ -273,10 +281,6 @@ function canHaveDepth(blockType: any): boolean {
}
}

function encodeContent(text) {
return text.replace(/[*_`]/g, '\\$&');
}

function encodeCode(text) {
return text.replace(/`/g, '\\`');
}
Expand Down