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

Fetch page title when paste valid markdown title with one valid URL #2908

Merged
merged 4 commits into from
Apr 15, 2019
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
27 changes: 21 additions & 6 deletions browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -809,6 +811,8 @@ export default class CodeEditor extends React.Component {

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)) {
Expand Down Expand Up @@ -850,7 +854,17 @@ export default class CodeEditor extends React.Component {
}

handlePasteUrl (editor, pastedTxt) {
const taggedUrl = `<${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 => {
Expand All @@ -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 = value.replace(taggedUrl, titleMark + 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 => {
Expand Down
7 changes: 6 additions & 1 deletion browser/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions tests/lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isMarkdownTitleURL } from '../../browser/lib/utils'

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)
})

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)
})
})