diff --git a/packages/draft-js-export-markdown/src/stateToMarkdown.js b/packages/draft-js-export-markdown/src/stateToMarkdown.js index eef9de4f..697a4cdb 100644 --- a/packages/draft-js-export-markdown/src/stateToMarkdown.js +++ b/packages/draft-js-export-markdown/src/stateToMarkdown.js @@ -12,6 +12,7 @@ import type {ContentState, ContentBlock} from 'draft-js'; const {BOLD, CODE, ITALIC, STRIKETHROUGH, UNDERLINE} = INLINE_STYLE; const CODE_INDENT = ' '; +const URLRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?(\?([-a-zA-Z0-9@:%_\+.~#?&//=]+)|)/gi; class MarkupGenerator { blocks: Array; @@ -192,6 +193,35 @@ class MarkupGenerator { return ''; } let content = encodeContent(text); + let match; + let output = []; + let lastIndex = 0; + while (match = URLRegex.exec(content)) { //eslint-disable-line + let plainText = ''; + if (match.index !== lastIndex) { + plainText += content.substring(lastIndex, match.index); + } + if (plainText.length) { + output.push(plainText); + } + + let url = match[0]; + if (url.endsWith('.')) { + url = url.substring(0, url.length - 1); + output.push(`[${url}](${url})`); + output.push('.'); + } else { + output.push(`[${url}](${url})`); + } + + lastIndex = URLRegex.lastIndex; + } + if (output.length) { + if (lastIndex < content.length) { + output.push(content.substring(lastIndex, content.length)); + } + content = output.join(''); + } if (style.has(BOLD)) { content = `**${content}**`; } @@ -219,6 +249,11 @@ class MarkupGenerator { let url = data.url || ''; let title = data.title ? ` "${escapeTitle(data.title)}"` : ''; return `[${content}](${encodeURL(url)}${title})`; + } else if (entity != null && entity.getType() === ENTITY_TYPE.MENTION) { + let data = entity.getData(); + let url = data.url || ''; + let title = data.title ? ` "${escapeTitle(data.title)}"` : ''; + return `[${content}](${encodeURL(url)}${title})`; } else if (entity != null && entity.getType() === ENTITY_TYPE.IMAGE) { let data = entity.getData(); let src = data.src || ''; diff --git a/packages/draft-js-export-markdown/test/test-cases.txt b/packages/draft-js-export-markdown/test/test-cases.txt index b19e54fe..d9031940 100644 --- a/packages/draft-js-export-markdown/test/test-cases.txt +++ b/packages/draft-js-export-markdown/test/test-cases.txt @@ -2,6 +2,14 @@ {"entityMap":{},"blocks":[{"key":"33nh8","text":"a","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[]}]} a +>> Autolink +{"entityMap":{},"blocks":[{"key":"33nh8","text":"https://foo.com","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[]}]} +[https://foo.com](https://foo.com) + +>> Autolink with other text +{"entityMap":{},"blocks":[{"key":"33nh8","text":"this is a test https://foo.com links","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[]}]} +this is a test [https://foo.com](https://foo.com) links + >> Single Inline Style {"entityMap":{},"blocks":[{"key":"99n0j","text":"asdf","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":3,"length":1,"style":"BOLD"}],"entityRanges":[]}]} asd**f** diff --git a/packages/draft-js-utils/src/Constants.js b/packages/draft-js-utils/src/Constants.js index d78f33df..45520dfa 100644 --- a/packages/draft-js-utils/src/Constants.js +++ b/packages/draft-js-utils/src/Constants.js @@ -20,6 +20,7 @@ export const BLOCK_TYPE = { export const ENTITY_TYPE = { LINK: 'LINK', IMAGE: 'IMAGE', + MENTION: 'MENTION', }; export const INLINE_STYLE = {