diff --git a/app/gui/src/project-view/components/DocumentationEditor/__tests__/textPaste.test.ts b/app/gui/src/project-view/components/DocumentationEditor/__tests__/textPaste.test.ts index c36f16a21e0d..dd1da68f7850 100644 --- a/app/gui/src/project-view/components/DocumentationEditor/__tests__/textPaste.test.ts +++ b/app/gui/src/project-view/components/DocumentationEditor/__tests__/textPaste.test.ts @@ -10,7 +10,6 @@ test.each([ }, { clipboard: 'example.com', - inserted: '', }, { clipboard: 'http://example.com', @@ -22,15 +21,12 @@ test.each([ }, { clipboard: 'example.com/Address containing spaces and a < character', - inserted: '', }, { clipboard: 'example.com/Address resembling *bold syntax*', - inserted: '', }, { clipboard: 'Url: www.a.example.com, another: www.b.example.com', - inserted: 'Url: , another: ', }, { 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) }) diff --git a/app/gui/src/project-view/components/DocumentationEditor/textPaste.ts b/app/gui/src/project-view/components/DocumentationEditor/textPaste.ts index d6c44409cad6..7b9f495eedbf 100644 --- a/app/gui/src/project-view/components/DocumentationEditor/textPaste.ts +++ b/app/gui/src/project-view/components/DocumentationEditor/textPaste.ts @@ -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 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) } diff --git a/app/gui/src/project-view/components/PlainTextEditor/___tests__/urlLinks.test.ts b/app/gui/src/project-view/components/PlainTextEditor/___tests__/urlLinks.test.ts index f656756588a3..c64ab341e739 100644 --- a/app/gui/src/project-view/components/PlainTextEditor/___tests__/urlLinks.test.ts +++ b/app/gui/src/project-view/components/PlainTextEditor/___tests__/urlLinks.test.ts @@ -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: [ diff --git a/app/gui/src/project-view/util/__tests__/link.test.ts b/app/gui/src/project-view/util/__tests__/link.test.ts index f8ed89a68490..1c280eebaed2 100644 --- a/app/gui/src/project-view/util/__tests__/link.test.ts +++ b/app/gui/src/project-view/util/__tests__/link.test.ts @@ -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', diff --git a/app/gui/src/project-view/util/link.ts b/app/gui/src/project-view/util/link.ts index 916f60fd5bce..76413b53e353 100644 --- a/app/gui/src/project-view/util/link.ts +++ b/app/gui/src/project-view/util/link.ts @@ -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 =