mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Chore: Improve link detection function
This commit is contained in:
parent
5268b5bf6b
commit
a0faca0997
@ -36,7 +36,7 @@ import { focus } from '@joplin/lib/utils/focusHandler';
|
|||||||
const md5 = require('md5');
|
const md5 = require('md5');
|
||||||
const { clipboard } = require('electron');
|
const { clipboard } = require('electron');
|
||||||
const supportedLocales = require('./supportedLocales');
|
const supportedLocales = require('./supportedLocales');
|
||||||
import { isLink } from '@joplin/utils/url';
|
import { hasProtocol } from '@joplin/utils/url';
|
||||||
|
|
||||||
const logger = Logger.create('TinyMCE');
|
const logger = Logger.create('TinyMCE');
|
||||||
|
|
||||||
@ -1195,7 +1195,7 @@ const TinyMCE = (props: NoteBodyEditorProps, ref: any) => {
|
|||||||
editor.insertContent(result.html);
|
editor.insertContent(result.html);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (BaseItem.isMarkdownTag(pastedText) || isLink(pastedText)) { // Paste a link to a note
|
if (BaseItem.isMarkdownTag(pastedText) || hasProtocol(pastedText, ['https', 'joplin', 'file'])) { // Paste a link to a note
|
||||||
logger.info('onPaste: pasting as a Markdown tag');
|
logger.info('onPaste: pasting as a Markdown tag');
|
||||||
const result = await markupToHtml.current(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, pastedText, markupRenderOptions({ bodyOnly: true }));
|
const result = await markupToHtml.current(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, pastedText, markupRenderOptions({ bodyOnly: true }));
|
||||||
editor.insertContent(result.html);
|
editor.insertContent(result.html);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { fileUriToPath } from './url';
|
import { fileUriToPath, hasProtocol } from './url';
|
||||||
|
|
||||||
describe('utils/url', () => {
|
describe('utils/url', () => {
|
||||||
|
|
||||||
@ -28,4 +28,40 @@ describe('utils/url', () => {
|
|||||||
expect(fileUriToPath('file:///c:/AUTOEXEC.BAT', 'win32')).toBe('c:\\AUTOEXEC.BAT');
|
expect(fileUriToPath('file:///c:/AUTOEXEC.BAT', 'win32')).toBe('c:\\AUTOEXEC.BAT');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
[
|
||||||
|
'https://joplinapp.org',
|
||||||
|
'https',
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://joplinapp.org',
|
||||||
|
'http',
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://joplinapp.org',
|
||||||
|
['http', 'https'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'https://joplinapp.org',
|
||||||
|
[],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'',
|
||||||
|
[],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'joplin://openNote?id=ABCD',
|
||||||
|
['http', 'https', 'joplin'],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
])('should tell if a URL has a particular protocol', (url, protocol, expected) => {
|
||||||
|
const actual = hasProtocol(url, protocol);
|
||||||
|
expect(actual).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -101,9 +101,13 @@ export const isDataUrl = (path: string) => {
|
|||||||
return path.startsWith('data:');
|
return path.startsWith('data:');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isLink = (text: string) => {
|
export const hasProtocol = (url: string, protocol: string | string[]) => {
|
||||||
if (!text) return false;
|
if (!url) return false;
|
||||||
const linkRegex = /^(https?|file|joplin):\/\/[^)\s]+$/;
|
|
||||||
return !!text.match(linkRegex);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
const protocols = typeof protocol === 'string' ? [protocol] : protocol;
|
||||||
|
url = url.toLowerCase();
|
||||||
|
for (const p of protocols) {
|
||||||
|
if (url.startsWith(`${p.toLowerCase()}://`)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user