@@ -18,12 +18,6 @@ import {
1818/**
1919 * Check if the provided tokens form a valid link structure, which can either be a single link token
2020 * or a link token surrounded by parentheses or square brackets.
21- *
22- * This ensures that only complete and valid text is hyperlinked, preventing cases where a valid
23- * top-level domain (TLD) is immediately followed by an invalid character, like a number. For
24- * example, with the `find` method from Linkify, entering `example.com1` would result in
25- * `example.com` being linked and the trailing `1` left as plain text. By using the `tokenize`
26- * method, we can perform more comprehensive validation on the input text.
2721 */
2822function isValidLinkStructure ( tokens : LinkMatch [ ] ) {
2923 if ( tokens . length === 1 ) {
@@ -42,36 +36,23 @@ type AutolinkOptions = {
4236 defaultProtocol : string ;
4337 validate : ( url : string ) => boolean ;
4438 shouldAutoLink : ( url : string ) => boolean ;
45- protocols : Array < { scheme : string ; optionalSlashes ?: boolean } | string > ;
4639} ;
4740
4841/**
49- * This plugin allows you to automatically add links to your editor.
50- * @param options The plugin options
51- * @returns The plugin instance
42+ * Plugin that automatically adds link marks when typing URLs.
5243 */
5344export function autolink ( options : AutolinkOptions ) : Plugin {
5445 return new Plugin ( {
5546 key : new PluginKey ( "autolink" ) ,
5647 appendTransaction : ( transactions , oldState , newState ) => {
57- /**
58- * Does the transaction change the document?
59- */
6048 const docChanges =
6149 transactions . some ( ( transaction ) => transaction . docChanged ) &&
6250 ! oldState . doc . eq ( newState . doc ) ;
6351
64- /**
65- * Prevent autolink if the transaction is not a document change or if the transaction has the meta `preventAutolink`.
66- */
6752 const preventAutolink = transactions . some ( ( transaction ) =>
6853 transaction . getMeta ( "preventAutolink" )
6954 ) ;
7055
71- /**
72- * Prevent autolink if the transaction is not a document change
73- * or if the transaction has the meta `preventAutolink`.
74- */
7556 if ( ! docChanges || preventAutolink ) {
7657 return ;
7758 }
@@ -83,7 +64,6 @@ export function autolink(options: AutolinkOptions): Plugin {
8364 const changes = getChangedRanges ( transform ) ;
8465
8566 changes . forEach ( ( { newRange } ) => {
86- // Now let's see if we can add new links.
8767 const nodesInChangedRanges = findChildrenInRange (
8868 newState . doc ,
8969 newRange ,
@@ -94,7 +74,6 @@ export function autolink(options: AutolinkOptions): Plugin {
9474 let textBeforeWhitespace : string | undefined ;
9575
9676 if ( nodesInChangedRanges . length > 1 ) {
97- // Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
9877 textBlock = nodesInChangedRanges [ 0 ] ;
9978 textBeforeWhitespace = newState . doc . textBetween (
10079 textBlock . pos ,
@@ -151,7 +130,6 @@ export function autolink(options: AutolinkOptions): Plugin {
151130
152131 linksBeforeSpace
153132 . filter ( ( link ) => link . isLink )
154- // Calculate link position.
155133 . map ( ( link ) => ( {
156134 ...link ,
157135 from : lastWordAndBlockOffset + link . start + 1 ,
@@ -169,11 +147,8 @@ export function autolink(options: AutolinkOptions): Plugin {
169147 newState . schema . marks . code
170148 ) ;
171149 } )
172- // validate link
173150 . filter ( ( link ) => options . validate ( link . value ) )
174- // check whether should autolink
175151 . filter ( ( link ) => options . shouldAutoLink ( link . value ) )
176- // Add link mark.
177152 . forEach ( ( link ) => {
178153 if (
179154 getMarksBetween ( link . from , link . to , newState . doc ) . some (
0 commit comments