mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
Add parse and fetch pasted markdown titles with url
This commit is contained in:
committed by
Junyoung Choi
parent
5e134f990e
commit
d81e69bf00
@@ -14,6 +14,8 @@ import {
|
|||||||
import TextEditorInterface from 'browser/lib/TextEditorInterface'
|
import TextEditorInterface from 'browser/lib/TextEditorInterface'
|
||||||
import eventEmitter from 'browser/main/lib/eventEmitter'
|
import eventEmitter from 'browser/main/lib/eventEmitter'
|
||||||
import iconv from 'iconv-lite'
|
import iconv from 'iconv-lite'
|
||||||
|
|
||||||
|
import { isMarkdownTitleURL } from 'browser/lib/utils'
|
||||||
import styles from '../components/CodeEditor.styl'
|
import styles from '../components/CodeEditor.styl'
|
||||||
const { ipcRenderer, remote, clipboard } = require('electron')
|
const { ipcRenderer, remote, clipboard } = require('electron')
|
||||||
import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily'
|
import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily'
|
||||||
@@ -861,9 +863,11 @@ export default class CodeEditor extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pastedTxt = clipboard.readText()
|
const pastedTxt = clipboard.readText()
|
||||||
|
console.log(pastedTxt);
|
||||||
if (isInFencedCodeBlock(editor)) {
|
if (isInFencedCodeBlock(editor)) {
|
||||||
this.handlePasteText(editor, pastedTxt)
|
this.handlePasteText(editor, pastedTxt)
|
||||||
|
} else if (fetchUrlTitle && isMarkdownTitleURL(pastedTxt) && !isInLinkTag(editor)) {
|
||||||
|
this.handlePasteUrl(editor, pastedTxt)
|
||||||
} else if (fetchUrlTitle && isURL(pastedTxt) && !isInLinkTag(editor)) {
|
} else if (fetchUrlTitle && isURL(pastedTxt) && !isInLinkTag(editor)) {
|
||||||
this.handlePasteUrl(editor, pastedTxt)
|
this.handlePasteUrl(editor, pastedTxt)
|
||||||
} else if (attachmentManagement.isAttachmentLink(pastedTxt)) {
|
} else if (attachmentManagement.isAttachmentLink(pastedTxt)) {
|
||||||
@@ -905,7 +909,17 @@ export default class CodeEditor extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handlePasteUrl(editor, pastedTxt) {
|
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)
|
editor.replaceSelection(taggedUrl)
|
||||||
|
|
||||||
const isImageReponse = response => {
|
const isImageReponse = response => {
|
||||||
@@ -917,22 +931,23 @@ export default class CodeEditor extends React.Component {
|
|||||||
const replaceTaggedUrl = replacement => {
|
const replaceTaggedUrl = replacement => {
|
||||||
const value = editor.getValue()
|
const value = editor.getValue()
|
||||||
const cursor = editor.getCursor()
|
const cursor = editor.getCursor()
|
||||||
const newValue = value.replace(taggedUrl, replacement)
|
const newValue = titleMark + value.replace(taggedUrl, replacement)
|
||||||
const newCursor = Object.assign({}, cursor, {
|
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.setValue(newValue)
|
||||||
editor.setCursor(newCursor)
|
editor.setCursor(newCursor)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch(pastedTxt, {
|
fetch(urlToFetch, {
|
||||||
method: 'get'
|
method: 'get'
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (isImageReponse(response)) {
|
if (isImageReponse(response)) {
|
||||||
return this.mapImageResponse(response, pastedTxt)
|
return this.mapImageResponse(response, urlToFetch)
|
||||||
} else {
|
} else {
|
||||||
return this.mapNormalResponse(response, pastedTxt)
|
return this.mapNormalResponse(response, urlToFetch)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(replacement => {
|
.then(replacement => {
|
||||||
|
|||||||
@@ -132,8 +132,13 @@ export function isObjectEqual (a, b) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isMarkdownTitleURL (str) {
|
||||||
|
return /(^#{1,6}\s)(?:\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str)
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
lastFindInArray,
|
lastFindInArray,
|
||||||
escapeHtmlCharacters,
|
escapeHtmlCharacters,
|
||||||
isObjectEqual
|
isObjectEqual,
|
||||||
|
isMarkdownTitleURL
|
||||||
}
|
}
|
||||||
|
|||||||
14
tests/lib/utils.test.js
Normal file
14
tests/lib/utils.test.js
Normal file
@@ -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'))
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user