/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{Mark as t,markPasteRule as e,mergeAttributes as o,combineTransactionSteps as n,getChangedRanges as i,findChildrenInRange as s,getMarksBetween as r,getAttributes as l}from"../../core/dist/index.js";import{registerCustomProtocol as a,tokenize as u,find as d,reset as p}from"../../../linkifyjs/dist/linkify.js";import{Plugin as c,PluginKey as h}from"../../../prosemirror-state/dist/index.js";var f="[\0-   ᠎ -\u2029 　]",k=new RegExp(f),m=new RegExp(`${f}$`),g=new RegExp(f,"g");function v(t,e){const o=["http","https","ftp","ftps","mailto","tel","callto","sms","cid","xmpp"];return e&&e.forEach(t=>{const e="string"==typeof t?t:t.scheme;e&&o.push(e)}),!t||t.replace(g,"").match(new RegExp(`^(?:(?:${o.join("|")}):|[^a-z]|[a-z0-9+.-]+(?:[^a-z+.-:]|$))`,"i"))}var A=t.create({name:"link",priority:1e3,keepOnSplit:!1,exitable:!0,onCreate(){this.options.validate&&!this.options.shouldAutoLink&&(this.options.shouldAutoLink=this.options.validate,console.warn("The `validate` option is deprecated. Rename to the `shouldAutoLink` option instead.")),this.options.protocols.forEach(t=>{"string"!=typeof t?a(t.scheme,t.optionalSlashes):a(t)})},onDestroy(){p()},inclusive(){return this.options.autolink},addOptions:()=>({openOnClick:!0,enableClickSelection:!1,linkOnPaste:!0,autolink:!0,protocols:[],defaultProtocol:"http",HTMLAttributes:{target:"_blank",rel:"noopener noreferrer nofollow",class:null},isAllowedUri:(t,e)=>!!v(t,e.protocols),validate:t=>!!t,shouldAutoLink:t=>{const e=/^[a-z][a-z0-9+.-]*:\/\//i.test(t),o=/^[a-z][a-z0-9+.-]*:/i.test(t);if(e||o&&!t.includes("@"))return!0;const n=(t.includes("@")?t.split("@").pop():t).split(/[/?#:]/)[0];return!/^\d{1,3}(\.\d{1,3}){3}$/.test(n)&&!!/\./.test(n)}}),addAttributes(){return{href:{default:null,parseHTML:t=>t.getAttribute("href")},target:{default:this.options.HTMLAttributes.target},rel:{default:this.options.HTMLAttributes.rel},class:{default:this.options.HTMLAttributes.class},title:{default:null}}},parseHTML(){return[{tag:"a[href]",getAttrs:t=>{const e=t.getAttribute("href");return!(!e||!this.options.isAllowedUri(e,{defaultValidate:t=>!!v(t,this.options.protocols),protocols:this.options.protocols,defaultProtocol:this.options.defaultProtocol}))&&null}}]},renderHTML({HTMLAttributes:t}){return this.options.isAllowedUri(t.href,{defaultValidate:t=>!!v(t,this.options.protocols),protocols:this.options.protocols,defaultProtocol:this.options.defaultProtocol})?["a",o(this.options.HTMLAttributes,t),0]:["a",o(this.options.HTMLAttributes,{...t,href:""}),0]},markdownTokenName:"link",parseMarkdown:(t,e)=>e.applyMark("link",e.parseInline(t.tokens||[]),{href:t.href,title:t.title||null}),renderMarkdown:(t,e)=>{var o,n,i,s;const r=null!=(n=null==(o=t.attrs)?void 0:o.href)?n:"",l=null!=(s=null==(i=t.attrs)?void 0:i.title)?s:"",a=e.renderChildren(t);return l?`[${a}](${r} "${l}")`:`[${a}](${r})`},addCommands(){return{setLink:t=>({chain:e})=>{const{href:o}=t;return!!this.options.isAllowedUri(o,{defaultValidate:t=>!!v(t,this.options.protocols),protocols:this.options.protocols,defaultProtocol:this.options.defaultProtocol})&&e().setMark(this.name,t).setMeta("preventAutolink",!0).run()},toggleLink:t=>({chain:e})=>{const{href:o}=t||{};return!(o&&!this.options.isAllowedUri(o,{defaultValidate:t=>!!v(t,this.options.protocols),protocols:this.options.protocols,defaultProtocol:this.options.defaultProtocol}))&&e().toggleMark(this.name,t,{extendEmptyMarkRange:!0}).setMeta("preventAutolink",!0).run()},unsetLink:()=>({chain:t})=>t().unsetMark(this.name,{extendEmptyMarkRange:!0}).setMeta("preventAutolink",!0).run()}},addPasteRules(){return[e({find:t=>{const e=[];if(t){const{protocols:o,defaultProtocol:n}=this.options,i=d(t).filter(t=>t.isLink&&this.options.isAllowedUri(t.value,{defaultValidate:t=>!!v(t,o),protocols:o,defaultProtocol:n}));i.length&&i.forEach(t=>{this.options.shouldAutoLink(t.value)&&e.push({text:t.value,data:{href:t.href},index:t.start})})}return e},type:this.type,getAttributes:t=>{var e;return{href:null==(e=t.data)?void 0:e.href}}})]},addProseMirrorPlugins(){const t=[],{protocols:e,defaultProtocol:o}=this.options;var a;return this.options.autolink&&t.push((a={type:this.type,defaultProtocol:this.options.defaultProtocol,validate:t=>this.options.isAllowedUri(t,{defaultValidate:t=>!!v(t,e),protocols:e,defaultProtocol:o}),shouldAutoLink:this.options.shouldAutoLink},new c({key:new h("autolink"),appendTransaction:(t,e,o)=>{const l=t.some(t=>t.docChanged)&&!e.doc.eq(o.doc),d=t.some(t=>t.getMeta("preventAutolink"));if(!l||d)return;const{tr:p}=o,c=n(e.doc,[...t]);return i(c).forEach(({newRange:t})=>{const e=s(o.doc,t,t=>t.isTextblock);let n,i;if(e.length>1)n=e[0],i=o.doc.textBetween(n.pos,n.pos+n.node.nodeSize,void 0," ");else if(e.length){const s=o.doc.textBetween(t.from,t.to," "," ");if(!m.test(s))return;n=e[0],i=o.doc.textBetween(n.pos,t.to,void 0," ")}if(n&&i){const t=i.split(k).filter(Boolean);if(t.length<=0)return!1;const e=t[t.length-1],s=n.pos+i.lastIndexOf(e);if(!e)return!1;const d=u(e).map(t=>t.toObject(a.defaultProtocol));if(!(1===(l=d).length?l[0].isLink:3===l.length&&l[1].isLink&&["()","[]"].includes(l[0].value+l[2].value)))return!1;d.filter(t=>t.isLink).map(t=>({...t,from:s+t.start+1,to:s+t.end+1})).filter(t=>!o.schema.marks.code||!o.doc.rangeHasMark(t.from,t.to,o.schema.marks.code)).filter(t=>a.validate(t.value)).filter(t=>a.shouldAutoLink(t.value)).forEach(t=>{r(t.from,t.to,o.doc).some(t=>t.mark.type===a.type)||p.addMark(t.from,t.to,a.type.create({href:t.href}))})}var l}),p.steps.length?p:void 0}}))),t.push(function(t){return new c({key:new h("handleClickLink"),props:{handleClick:(e,o,n)=>{var i,s;if(0!==n.button)return!1;if(!e.editable)return!1;let r=null;if(n.target instanceof HTMLAnchorElement)r=n.target;else{const e=n.target;if(!e)return!1;const o=t.editor.view.dom;r=e.closest("a"),r&&!o.contains(r)&&(r=null)}if(!r)return!1;let a=!1;if(t.enableClickSelection&&(a=t.editor.commands.extendMarkRange(t.type.name)),t.openOnClick){const o=l(e.state,t.type.name),n=null!=(i=r.href)?i:o.href,u=null!=(s=r.target)?s:o.target;n&&(window.open(n,u),a=!0)}return a}}})}({type:this.type,editor:this.editor,openOnClick:"whenNotEditable"===this.options.openOnClick||this.options.openOnClick,enableClickSelection:this.options.enableClickSelection})),this.options.linkOnPaste&&t.push(function(t){return new c({key:new h("handlePasteLink"),props:{handlePaste:(e,o,n)=>{const{shouldAutoLink:i}=t,{state:s}=e,{selection:r}=s,{empty:l}=r;if(l)return!1;let a="";n.content.forEach(t=>{a+=t.textContent});const u=d(a,{defaultProtocol:t.defaultProtocol}).find(t=>t.isLink&&t.value===a);return!(!a||!u||void 0!==i&&!i(u.value))&&t.editor.commands.setMark(t.type,{href:u.href})}}})}({editor:this.editor,defaultProtocol:this.options.defaultProtocol,type:this.type,shouldAutoLink:this.options.shouldAutoLink})),t}});export{A as Link,v as isAllowedUri};