Skip to content

Commit

Permalink
Stricter URL recognition for autolinking (#11871)
Browse files Browse the repository at this point in the history
Documentation editor: Use only regex-based URL recognition for pasted text; increase strictness of regex. Fixes #11697.
  • Loading branch information
kazcw authored Dec 17, 2024
1 parent cf82c8c commit 30075d2
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ test.each([
},
{
clipboard: 'example.com',
inserted: '<https://example.com>',
},
{
clipboard: 'http://example.com',
Expand All @@ -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',
Expand All @@ -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)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion app/gui/src/project-view/util/__tests__/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -19,6 +18,7 @@ const cases = {
'(a@b.cd)',
],
neither: [
'www.a.b',
'https://💩.la/',
'a.b',
'http://AsDf',
Expand Down
4 changes: 2 additions & 2 deletions app/gui/src/project-view/util/link.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down

0 comments on commit 30075d2

Please sign in to comment.