1
1
mirror of https://github.com/enso-org/enso.git synced 2024-12-22 18:38:11 +03:00

Stricter URL recognition for autolinking ()

Documentation editor: Use only regex-based URL recognition for pasted text; increase strictness of regex. Fixes .
This commit is contained in:
Kaz Wesley 2024-12-17 06:13:30 -08:00 committed by GitHub
parent cf82c8c3c7
commit 30075d26bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 8 additions and 38 deletions
app/gui/src/project-view
components
DocumentationEditor
PlainTextEditor/___tests__
util

View File

@ -10,7 +10,6 @@ test.each([
},
{
clipboard: 'example.com',
inserted: '<https://example.com>',
},
{
clipboard: 'http://example.com',
@ -22,15 +21,12 @@ test.each([
},
{
clipboard: 'example.com/Address containing spaces and a < character',
inserted: '<https://example.com/Address containing spaces and a %3C character>',
},
{
clipboard: 'example.com/Address resembling *bold syntax*',
inserted: '<https://example.com/Address resembling %2Abold syntax%2A>',
},
{
clipboard: 'Url: www.a.example.com, another: www.b.example.com',
inserted: 'Url: <https://www.a.example.com>, another: <https://www.b.example.com>',
},
{
clipboard: 'gopher:///no/autolinking/unusual/protocols',
@ -53,6 +49,9 @@ test.each([
{
clipboard: 'example.com with trailing text',
},
{
clipboard: 'Standard.Base.Math',
},
])('Auto-linking pasted text: $clipboard', ({ clipboard, inserted }) => {
expect(transformPastedText(clipboard)).toBe(inserted ?? clipboard)
})

View File

@ -5,30 +5,10 @@ function uriEscapeChar(char: string) {
}
function toAutoLink(text: string) {
return `<${addProtocolIfMissing(text).replaceAll(/[\][<>*`]/g, uriEscapeChar)}>`
}
function addProtocolIfMissing(url: string) {
return (URL.canParse(url) ? '' : 'https://') + url
}
/**
* Return whether the input is likely to be a URL, possibly with the protocol omitted. This matches more aggressively
* than {@link LINKABLE_URL_REGEX}, but rejects some inputs that would technically make valid URLs but are more likely
* to be other text.
*/
function isReasonableUrl(text: string) {
const textWithProto = addProtocolIfMissing(text)
let textAsUrl: URL | undefined
try {
textAsUrl = new URL(textWithProto)
} catch {
return false
}
return textAsUrl.protocol.match(/https?:/) && textAsUrl.hostname.match(/\.[a-z]/)
return `<${text.replaceAll(/[\][<>*`]/g, uriEscapeChar)}>`
}
/** Convert the input to Markdown. This includes converting any likely URLs to <autolink>s. */
export function transformPastedText(text: string): string {
return isReasonableUrl(text) ? toAutoLink(text) : text.replaceAll(LINKABLE_URL_REGEX, toAutoLink)
return text.replaceAll(LINKABLE_URL_REGEX, toAutoLink)
}

View File

@ -50,15 +50,6 @@ test.each([
},
],
},
{
text: 'Url: www.example.com',
expectedLinks: [
{
text: 'www.example.com',
href: 'https://www.example.com',
},
],
},
{
text: 'Email: user@example.com',
expectedLinks: [

View File

@ -3,7 +3,6 @@ import { LINKABLE_EMAIL_REGEX, LINKABLE_URL_REGEX } from '../link'
const cases = {
urls: [
'www.a.b',
'http://example.com',
'https://a.b',
'https://some.local',
@ -19,6 +18,7 @@ const cases = {
'(a@b.cd)',
],
neither: [
'www.a.b',
'https://💩.la/',
'a.b',
'http://AsDf',

View File

@ -1,9 +1,9 @@
/**
* Heuristic that matches strings suitable to be automatically interpreted as links. Recognizes absolute URLs with
* `http` and `https` protocols, and some protocol-less strings that are likely to be URLs.
* `http` and `https` protocols.
*/
export const LINKABLE_URL_REGEX =
/(?:https?:\/\/(?:www\.)?|www\.)[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*/g
/https?:\/\/[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*/g
/** Heuristic that matches strings suitable to be automatically interpreted as email addresses. */
export const LINKABLE_EMAIL_REGEX =