Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extension-link): Avoid auto-linking partial text for invalid TLDs #4865

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/extension-link/src/helpers/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ import {
} from '@tiptap/core'
import { MarkType } from '@tiptap/pm/model'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { find } from 'linkifyjs'
import { MultiToken, tokenize } from 'linkifyjs'

/**
* Check if the provided tokens form a valid link structure, which can either be a single link token
* or a link token surrounded by parentheses or square brackets.
*
* This ensures that only complete and valid text is hyperlinked, preventing cases where a valid
* top-level domain (TLD) is immediately followed by an invalid character, like a number. For
* example, with the `find` method from Linkify, entering `example.com1` would result in
* `example.com` being linked and the trailing `1` left as plain text. By using the `tokenize`
* method, we can perform more comprehensive validation on the input text.
*/
function isValidLinkStructure(tokens: Array<ReturnType<MultiToken['toObject']>>) {
if (tokens.length === 1) {
return tokens[0].isLink
}

if (tokens.length === 3 && tokens[1].isLink) {
return ['()', '[]'].includes(tokens[0].value + tokens[2].value)
}

return false
}

type AutolinkOptions = {
type: MarkType
Expand Down Expand Up @@ -77,7 +99,13 @@ export function autolink(options: AutolinkOptions): Plugin {
return false
}

find(lastWordBeforeSpace)
const linksBeforeSpace = tokenize(lastWordBeforeSpace).map(t => t.toObject())

if (!isValidLinkStructure(linksBeforeSpace)) {
return false
}

linksBeforeSpace
.filter(link => link.isLink)
// Calculate link position.
.map(link => ({
Expand Down
Loading