From c1e43935e13dc0b6451a417b077269c635cae60a Mon Sep 17 00:00:00 2001 From: Abner Soares Alves Junior Date: Thu, 28 Feb 2019 14:57:52 -0300 Subject: [PATCH 1/4] Add parse and fetch pasted markdown titles with url --- browser/components/CodeEditor.js | 31 +++++++++++++++++++++++-------- browser/lib/utils.js | 7 ++++++- tests/lib/utils.test.js | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tests/lib/utils.test.js diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 0ddfd5c93..19eec0122 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -14,6 +14,8 @@ import { import TextEditorInterface from 'browser/lib/TextEditorInterface' import eventEmitter from 'browser/main/lib/eventEmitter' import iconv from 'iconv-lite' + +import { isMarkdownTitleURL } from 'browser/lib/utils' import styles from '../components/CodeEditor.styl' const { ipcRenderer, remote, clipboard } = require('electron') import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily' @@ -806,9 +808,11 @@ export default class CodeEditor extends React.Component { } const pastedTxt = clipboard.readText() - + console.log(pastedTxt); if (isInFencedCodeBlock(editor)) { this.handlePasteText(editor, pastedTxt) + } else if (fetchUrlTitle && isMarkdownTitleURL(pastedTxt) && !isInLinkTag(editor)) { + this.handlePasteUrl(editor, pastedTxt) } else if (fetchUrlTitle && isURL(pastedTxt) && !isInLinkTag(editor)) { this.handlePasteUrl(editor, pastedTxt) } else if (attachmentManagement.isAttachmentLink(pastedTxt)) { @@ -849,8 +853,18 @@ export default class CodeEditor extends React.Component { } } - handlePasteUrl (editor, pastedTxt) { - const taggedUrl = `<${pastedTxt}>` + handlePasteUrl(editor, pastedTxt) { + let taggedUrl = `<${pastedTxt}>` + let urlToFetch = pastedTxt; + let titleMark = ''; + + if (isMarkdownTitleURL(pastedTxt)) { + const pastedTxtSplitted = pastedTxt.split(' ') + titleMark = `${pastedTxtSplitted[0]} `; + urlToFetch = pastedTxtSplitted[1]; + taggedUrl = `<${urlToFetch}>` + } + editor.replaceSelection(taggedUrl) const isImageReponse = response => { @@ -862,22 +876,23 @@ export default class CodeEditor extends React.Component { const replaceTaggedUrl = replacement => { const value = editor.getValue() const cursor = editor.getCursor() - const newValue = value.replace(taggedUrl, replacement) + const newValue = titleMark + value.replace(taggedUrl, replacement) const newCursor = Object.assign({}, cursor, { - ch: cursor.ch + newValue.length - value.length + ch: cursor.ch + newValue.length - (value.length - titleMark.length) }) + editor.setValue(newValue) editor.setCursor(newCursor) } - fetch(pastedTxt, { + fetch(urlToFetch, { method: 'get' }) .then(response => { if (isImageReponse(response)) { - return this.mapImageResponse(response, pastedTxt) + return this.mapImageResponse(response, urlToFetch) } else { - return this.mapNormalResponse(response, pastedTxt) + return this.mapNormalResponse(response, urlToFetch) } }) .then(replacement => { diff --git a/browser/lib/utils.js b/browser/lib/utils.js index 1d15b7221..4bcc96989 100644 --- a/browser/lib/utils.js +++ b/browser/lib/utils.js @@ -132,8 +132,13 @@ export function isObjectEqual (a, b) { return true } +export function isMarkdownTitleURL (str) { + return /(^#{1,6}\s)(?:\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str) +} + export default { lastFindInArray, escapeHtmlCharacters, - isObjectEqual + isObjectEqual, + isMarkdownTitleURL } diff --git a/tests/lib/utils.test.js b/tests/lib/utils.test.js new file mode 100644 index 000000000..d696eb29f --- /dev/null +++ b/tests/lib/utils.test.js @@ -0,0 +1,14 @@ +import test from 'ava' +import { isMarkdownTitleURL } from '../../browser/lib/utils' + +test('isMarkdownTitleURL returns true for valid Markdown title with url', (t) => { + t.true(isMarkdownTitleURL('# https://validurl.com')) + t.true(isMarkdownTitleURL('## https://validurl.com')) + t.true(isMarkdownTitleURL('###### https://validurl.com')) +}) + +test('isMarkdownTitleURL returns true for invalid Markdown title with url', (t) => { + t.false(isMarkdownTitleURL('1 https://validurl.com')) + t.false(isMarkdownTitleURL('24 https://validurl.com')) + t.false(isMarkdownTitleURL('####### https://validurl.com')) +}) From a351eef4fe1b1ccbf331c4bec86f0bb9e977ffe7 Mon Sep 17 00:00:00 2001 From: Abner Soares Alves Junior Date: Thu, 28 Feb 2019 15:07:50 -0300 Subject: [PATCH 2/4] Fix the position of title mark when replacing --- browser/components/CodeEditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 19eec0122..ec0063203 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -876,7 +876,7 @@ export default class CodeEditor extends React.Component { const replaceTaggedUrl = replacement => { const value = editor.getValue() const cursor = editor.getCursor() - const newValue = titleMark + value.replace(taggedUrl, replacement) + const newValue = value.replace(taggedUrl, titleMark + replacement) const newCursor = Object.assign({}, cursor, { ch: cursor.ch + newValue.length - (value.length - titleMark.length) }) From 01f5bd07e917b08e8dd4a318291c793a1267c63c Mon Sep 17 00:00:00 2001 From: Abner Soares Alves Junior Date: Thu, 28 Feb 2019 15:19:13 -0300 Subject: [PATCH 3/4] Fix some linter issues --- browser/components/CodeEditor.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index ec0063203..da16c5667 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -808,7 +808,7 @@ export default class CodeEditor extends React.Component { } const pastedTxt = clipboard.readText() - console.log(pastedTxt); + if (isInFencedCodeBlock(editor)) { this.handlePasteText(editor, pastedTxt) } else if (fetchUrlTitle && isMarkdownTitleURL(pastedTxt) && !isInLinkTag(editor)) { @@ -853,15 +853,15 @@ export default class CodeEditor extends React.Component { } } - handlePasteUrl(editor, pastedTxt) { + handlePasteUrl (editor, pastedTxt) { let taggedUrl = `<${pastedTxt}>` - let urlToFetch = pastedTxt; - let titleMark = ''; + let urlToFetch = pastedTxt + let titleMark = '' if (isMarkdownTitleURL(pastedTxt)) { const pastedTxtSplitted = pastedTxt.split(' ') - titleMark = `${pastedTxtSplitted[0]} `; - urlToFetch = pastedTxtSplitted[1]; + titleMark = `${pastedTxtSplitted[0]} ` + urlToFetch = pastedTxtSplitted[1] taggedUrl = `<${urlToFetch}>` } @@ -876,7 +876,7 @@ export default class CodeEditor extends React.Component { const replaceTaggedUrl = replacement => { const value = editor.getValue() const cursor = editor.getCursor() - const newValue = value.replace(taggedUrl, titleMark + replacement) + const newValue = value.replace(taggedUrl, titleMark + replacement) const newCursor = Object.assign({}, cursor, { ch: cursor.ch + newValue.length - (value.length - titleMark.length) }) From e14beab1a90060e24e058845bbea13e8bd354601 Mon Sep 17 00:00:00 2001 From: Abner Soares Alves Junior Date: Thu, 28 Feb 2019 15:59:15 -0300 Subject: [PATCH 4/4] Fix test moving from Ava to JEST --- tests/lib/utils.test.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/lib/utils.test.js b/tests/lib/utils.test.js index d696eb29f..a60cdfec9 100644 --- a/tests/lib/utils.test.js +++ b/tests/lib/utils.test.js @@ -1,14 +1,15 @@ -import test from 'ava' import { isMarkdownTitleURL } from '../../browser/lib/utils' -test('isMarkdownTitleURL returns true for valid Markdown title with url', (t) => { - t.true(isMarkdownTitleURL('# https://validurl.com')) - t.true(isMarkdownTitleURL('## https://validurl.com')) - t.true(isMarkdownTitleURL('###### https://validurl.com')) -}) +describe('isMarkdownTitleURL', () => { + it('returns true for valid Markdown title with url', () => { + expect(isMarkdownTitleURL('# https://validurl.com')).toBe(true) + expect(isMarkdownTitleURL('## https://validurl.com')).toBe(true) + expect(isMarkdownTitleURL('###### https://validurl.com')).toBe(true) + }) -test('isMarkdownTitleURL returns true for invalid Markdown title with url', (t) => { - t.false(isMarkdownTitleURL('1 https://validurl.com')) - t.false(isMarkdownTitleURL('24 https://validurl.com')) - t.false(isMarkdownTitleURL('####### https://validurl.com')) + it('returns true for invalid Markdown title with url', () => { + expect(isMarkdownTitleURL('1 https://validurl.com')).toBe(false) + expect(isMarkdownTitleURL('24 https://validurl.com')).toBe(false) + expect(isMarkdownTitleURL('####### https://validurl.com')).toBe(false) + }) })