UNPKG

@lexical/markdown

Version:

This package contains Markdown helpers and functionality for Lexical.

10 lines (8 loc) 20.2 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ "use strict";var e=require("lexical"),t=require("@lexical/list"),n=require("@lexical/rich-text"),o=require("@lexical/utils"),r=require("@lexical/code"),s=require("@lexical/link");function i(e,t){const n={};for(const o of e){const e=t(o);e&&(n[e]?n[e].push(o):n[e]=[o])}return n}function c(e){const t=i(e,e=>e.type);return{element:t.element||[],multilineElement:t["multiline-element"]||[],textFormat:t["text-format"]||[],textMatch:t["text-match"]||[]}}const l=/[!-/:-@[-`{-~\s]/,a=/[ \t\n\r\f]/,f=/[!"#$%&'()*+,\-./:;<=>?@[\]^_`{|}~]/,d=/^\s{0,3}$/;function g(t){if(!e.$isParagraphNode(t))return!1;const n=t.getFirstChild();return null==n||1===t.getChildrenSize()&&e.$isTextNode(n)&&d.test(n.getTextContent())}function u(t,n,o,r){for(const e of n){if(!e.export)continue;const n=e.export(t,e=>p(e,o,r));if(null!=n)return n}return e.$isElementNode(t)?p(t,o,r):e.$isDecoratorNode(t)?t.getTextContent():null}function p(t,n,o,r,s){const i=[],c=t.getChildren();r||(r=[]),s||(s=[]);e:for(const t of c){for(const e of o){if(!e.export)continue;const c=e.export(t,e=>p(e,n,o,r,[...s,...r]),(e,t)=>x(e,t,n,r,s));if(null!=c){i.push(c);continue e}}e.$isLineBreakNode(t)?i.push("\n"):e.$isTextNode(t)?i.push(x(t,t.getTextContent(),n,r,s)):e.$isElementNode(t)?i.push(p(t,n,o,r,s)):e.$isDecoratorNode(t)&&i.push(t.getTextContent())}return i.join("")}function x(e,t,n,o,r){let s=0===e.getFormat()?t:function(e){return e.replace(/^\s+|\s+$/g,e=>[...e].map(e=>"&#"+e.codePointAt(0)+";").join(""))}(t);e.hasFormat("code")||(s=s.replace(/([*_`~\\])/g,"\\$1"));let i="",c="",l="";const a=h(e,!0),f=h(e,!1),d=new Set;for(const t of n){const n=t.format[0],r=t.tag;m(e,n)&&!d.has(n)&&(d.add(n),m(a,n)&&o.find(e=>e.tag===r)||(o.push({format:n,tag:r}),i+=r))}for(let t=0;t<o.length;t++){const n=m(e,o[t].format),s=m(f,o[t].format);if(n&&s)continue;const i=[...o];for(;i.length>t;){const e=i.pop();r&&e&&r.find(t=>t.tag===e.tag)||(e&&"string"==typeof e.tag&&(n?s||(l+=e.tag):c+=e.tag),o.pop())}break}return s=i+s+l,c+s}function h(t,n){const o=n?t.getPreviousSibling():t.getNextSibling();return e.$isTextNode(o)?o:null}function m(t,n){return e.$isTextNode(t)&&t.hasFormat(n)}function $(e,t){const n=e.getTextContent(),o=t.fullMatchRegExpByTag["`"],r=t.transformersByTag["`"],s=[];let i=null;if(o&&r){const e=new RegExp(o.source,"g"),t=Array.from(n.matchAll(e));for(const e of t){const t=e.index,n=t+e[0].length;i||(i={content:e[2],endIndex:n,startIndex:t,tag:"`"}),s.push({end:n,start:t})}}const c=function(e,t,n=[]){const o=[],r=new Set(Object.keys(t.transformersByTag).filter(e=>"`"!==e[0]).map(e=>e[0])),s=t=>{let n=0;for(let o=t-1;o>=0&&"\\"===e[o];o--)n++;return n%2==1},i=e=>n.some(t=>e>=t.start&&e<t.end);let c=0;for(;c<e.length;){const t=e[c];if(!r.has(t)||s(c)||i(c)){c++;continue}let n=1;for(;c+n<e.length&&e[c+n]===t;)n++;const l=N(t,e,c,n,!0),a=N(t,e,c,n,!1);(l||a)&&o.push({active:!0,canClose:a,canOpen:l,char:t,index:c,length:n,originalLength:n}),c+=n}return o}(n,t,s),l=c.length>0?function(e,t,n){const o={};let r=0,s=null;for(;r<t.length;){const i=t[r];if(!i.active||!i.canClose||0===i.length){r++;continue}const c=`${i.char}${i.canOpen}`,l=o[c]??-1;let a=!1;for(let o=r-1;o>l;o--){const c=t[o];if(!c.active||!c.canOpen||0===c.length||c.char!==i.char)continue;if(c.canClose||i.canOpen){if((c.originalLength+i.originalLength)%3==0&&c.originalLength%3!=0&&i.originalLength%3!=0)continue}const l=Math.min(c.length,i.length),f=Object.keys(n.transformersByTag).filter(e=>e[0]===c.char&&e.length<=l).sort((e,t)=>t.length-e.length)[0];if(!f)continue;a=!0;const d=f.length,g={content:e.slice(c.index+c.length,i.index),endIndex:i.index+d,startIndex:c.index+(c.length-d),tag:f};(!s||g.startIndex<s.startIndex||g.startIndex===s.startIndex&&g.endIndex>s.endIndex)&&(s=g);for(let e=o+1;e<r;e++)t[e].active=!1;c.length-=d,i.length-=d,c.active=c.length>0,i.length>0?i.index+=d:(i.active=!1,r++);break}a||(o[c]=r-1,i.canOpen||(i.active=!1),r++)}return s}(n,c,t):null;let a=null,f=null;if(i&&l?l.startIndex<=i.startIndex&&l.endIndex>=i.endIndex?(a=l,f=t.transformersByTag[l.tag]):(a=i,f=r):i?(a=i,f=r):l&&(a=l,f=t.transformersByTag[l.tag]),!a||!f)return null;const d=[n.slice(a.startIndex,a.endIndex),a.tag,a.content];return d.index=a.startIndex,d.input=n,{endIndex:a.endIndex,match:d,startIndex:a.startIndex,transformer:f}}function N(e,t,n,o,r){if(!T(t,n,o,r))return!1;if("*"===e)return!0;if("_"===e){if(!T(t,n,o,!r))return!0;const e=r?t[n-1]:t[n+o];return void 0!==e&&f.test(e)}return!0}function T(e,t,n,o){const r=e[t-1],s=e[t+n],[i,c]=o?[s,r]:[r,s];return void 0!==i&&!a.test(i)&&(!f.test(i)||(void 0===c||a.test(c)||f.test(c)))}function E(t){return e.$isTextNode(t)&&!t.hasFormat("code")}function S(e,t,n){let o=$(e,t),r=function(e,t){const n=e;let o,r,s,i;for(const e of t){if(!e.replace||!e.importRegExp)continue;const t=n.getTextContent().match(e.importRegExp);if(!t)continue;const c=t.index||0,l=e.getEndIndex?e.getEndIndex(n,t):c+t[0].length;!1!==l&&(void 0===o||void 0===r||c<o&&(l>r||l<=o))&&(o=c,r=l,s=e,i=t)}return void 0===o||void 0===r||void 0===s||void 0===i?null:{endIndex:r,match:i,startIndex:o,transformer:s}}(e,n);if(o&&r&&(o.startIndex<=r.startIndex&&o.endIndex>=r.endIndex||r.startIndex>o.endIndex?r=null:o=null),o){const r=function(e,t,n,o,r){const s=e.getTextContent();let i,c,l;if(r[0]===s?i=e:0===t?[i,c]=e.splitText(n):[l,i,c]=e.splitText(t,n),i.setTextContent(r[2]),o)for(const e of o.format)i.hasFormat(e)||i.toggleFormat(e);return{nodeAfter:c,nodeBefore:l,transformedNode:i}}(e,o.startIndex,o.endIndex,o.transformer,o.match);E(r.nodeAfter)&&S(r.nodeAfter,t,n),E(r.nodeBefore)&&S(r.nodeBefore,t,n),E(r.transformedNode)&&S(r.transformedNode,t,n)}else if(r){const o=function(e,t,n,o,r){let s,i,c;return 0===t?[s,i]=e.splitText(n):[c,s,i]=e.splitText(t,n),o.replace?{nodeAfter:i,nodeBefore:c,transformedNode:o.replace(s,r)||void 0}:null}(e,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;E(o.nodeAfter)&&S(o.nodeAfter,t,n),E(o.nodeBefore)&&S(o.nodeBefore,t,n),E(o.transformedNode)&&S(o.transformedNode,t,n)}const s=e.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,(e,t)=>String.fromCodePoint(t));e.setTextContent(s)}function C(t,n=!1){const o=c(t),r=function(e){const t={},n={},o=[],r="(?<![\\\\])";for(const r of e){const{tag:e}=r;t[e]=r;const s=e.replace(/(\*|\^|\+)/g,"\\$1");o.push(s),1===e.length?n[e]="`"===e?new RegExp("(?<![\\\\`])(`)((?:\\\\`|[^`])+?)(`)(?!`)"):new RegExp(`(?<![\\\\${s}])(${s})((\\\\${s})?.*?[^${s}\\s](\\\\${s})?)((?<!\\\\)|(?<=\\\\\\\\))(${s})(?![\\\\${s}])`):n[e]=new RegExp(`(?<!\\\\)(${s})((\\\\${s})?.*?[^\\s](\\\\${s})?)((?<!\\\\)|(?<=\\\\\\\\))(${s})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:t}}(o.textFormat);return(t,s)=>{const i=t.split("\n"),c=i.length,l=s||e.$getRoot();l.clear();for(let e=0;e<c;e++){const t=i[e],[s,c]=I(i,e,o.multilineElement,l);s?e=c:L(t,l,o.element,r,o.textMatch,n)}const a=l.getChildren();for(const e of a)!n&&g(e)&&l.getChildrenSize()>1&&e.remove();null!==e.$getSelection()&&l.selectStart()}}function I(e,t,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:s,regExpStart:i,replace:c}=r,l=e[t].match(i);if(!l)continue;if(n){const s=n({lines:e,rootNode:o,startLineIndex:t,startMatch:l,transformer:r});if(null===s)continue;if(s)return s}const a="object"==typeof s&&"regExp"in s?s.regExp:s,f=s&&"object"==typeof s&&"optional"in s?s.optional:!s;let d=t;const g=e.length;for(;d<g;){const n=a?e[d].match(a):null;if(!n&&(!f||f&&d<g-1)){d++;continue}if(n&&t===d&&n.index===l.index){d++;continue}const r=[];if(n&&t===d)r.push(e[t].slice(l[0].length,-n[0].length));else for(let o=t;o<=d;o++)if(o===t){const t=e[o].slice(l[0].length);r.push(t)}else if(o===d&&n){const t=e[o].slice(0,-n[0].length);r.push(t)}else r.push(e[o]);if(!1!==c(o,null,l,n,r,!0))return[!0,d];break}}return[!1,t]}function L(r,s,i,c,l,a){const f=e.$createTextNode(r),d=e.$createParagraphNode();d.append(f),s.append(d);for(const{regExp:e,replace:t}of i){const n=r.match(e);if(n&&(f.setTextContent(r.slice(n[0].length)),!1!==t(d,[f],n,!0)))break}if(S(f,c,l),d.isAttached()&&r.length>0){const r=d.getPreviousSibling();if(!a&&(e.$isParagraphNode(r)||n.$isQuoteNode(r)||t.$isListNode(r))){let n=r;if(t.$isListNode(r)){const e=r.getLastDescendant();n=null==e?null:o.$findMatchingParent(e,t.$isListItemNode)}null!=n&&n.getTextContentSize()>0&&(n.splice(n.getChildrenSize(),0,[e.$createLineBreakNode(),...d.getChildren()]),d.remove())}}}function R(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}const v=/^(\s*)(\d{1,})\.\s/,y=/^(\s*)[-*+]\s/,_=/^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i,O=/^(#{1,6})\s/,M=/^>\s/,A=/^([ \t]*`{3,})([\w-]+)?[ \t]?/,b=/^[ \t]*`{3,}$/,k=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,F=/^(?:\|)(.+)(?:\|)\s?$/,w=/^(\| ?:?-*:? ?)+\|\s?$/,B=/^<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i,D=/^<\/[a-z_][\w-]*\s*>/i,P=e=>new RegExp(`(?:${e.source})$`,e.flags),j=e.createState("mdListMarker",{parse:e=>"string"==typeof e&&/^[-*+]$/.test(e)?e:"-"}),H=e.createState("mdCodeFence",{parse:e=>"string"==typeof e&&/^`{3,}$/.test(e)?e:"```"}),U=e=>(t,n,o,r)=>{const s=e(o);s.append(...n),t.replace(s),r||s.select(0,0)};const z=n=>(o,r,s,i)=>{const c=o.getPreviousSibling(),l=o.getNextSibling(),a=t.$createListItemNode("check"===n?"x"===s[3]:void 0),f=s[0].trim()[0],d="bullet"!==n&&"check"!==n||f!==j.parse(f)?void 0:f;if(t.$isListNode(l)&&l.getListType()===n){d&&e.$setState(l,j,d);const t=l.getFirstChild();null!==t?t.insertBefore(a):l.append(a),o.remove()}else if(t.$isListNode(c)&&c.getListType()===n)d&&e.$setState(c,j,d),c.append(a),o.remove();else{const r=t.$createListNode(n,"number"===n?Number(s[2]):void 0);d&&e.$setState(r,j,d),r.append(a),o.replace(r)}a.append(...r),i||a.select(0,0);const g=function(e){const t=e.match(/\t/g),n=e.match(/ /g);let o=0;return t&&(o+=t.length),n&&(o+=Math.floor(n.length/4)),o}(s[1]);g&&a.setIndent(g)},q=(n,o,r)=>{const s=[],i=n.getChildren();let c=0;for(const l of i)if(t.$isListItemNode(l)){if(1===l.getChildrenSize()){const e=l.getFirstChild();if(t.$isListNode(e)){s.push(q(e,o,r+1));continue}}const i=" ".repeat(4*r),a=n.getListType(),f=e.$getState(n,j),d="number"===a?`${n.getStart()+c}. `:"check"===a?`${f} [${l.getChecked()?"x":" "}] `:f+" ";s.push(i+d+o(l)),c++}return s.join("\n")},G={dependencies:[n.HeadingNode],export:(e,t)=>{if(!n.$isHeadingNode(e))return null;const o=Number(e.getTag().slice(1));return"#".repeat(o)+" "+t(e)},regExp:O,replace:U(e=>{const t="h"+e[1].length;return n.$createHeadingNode(t)}),type:"element"},Q={dependencies:[n.QuoteNode],export:(e,t)=>{if(!n.$isQuoteNode(e))return null;const o=t(e).split("\n"),r=[];for(const e of o)r.push("> "+e);return r.join("\n")},regExp:M,replace:(t,o,r,s)=>{if(s){const r=t.getPreviousSibling();if(n.$isQuoteNode(r))return r.splice(r.getChildrenSize(),0,[e.$createLineBreakNode(),...o]),void t.remove()}const i=n.$createQuoteNode();i.append(...o),t.replace(i),s||i.select(0,0)},type:"element"},K={dependencies:[r.CodeNode],export:t=>{if(!r.$isCodeNode(t))return null;const n=t.getTextContent();let o=e.$getState(t,H);if(n.indexOf(o)>-1){const e=n.match(/`{3,}/g);if(e){const t=Math.max(...e.map(e=>e.length));o="`".repeat(t+1)}}return o+(t.getLanguage()||"")+(n?"\n"+n:"")+"\n"+o},handleImportAfterStartMatch:({lines:e,rootNode:t,startLineIndex:n,startMatch:o})=>{const r=o[1],s=r.trim().length,i=e[n],c=o.index+r.length,l=i.slice(c),a=new RegExp(`\`{${s},}$`);if(a.test(l)){const e=l.match(a),r=l.slice(0,l.lastIndexOf(e[0])),s=[...o];return s[2]="",K.replace(t,null,s,e,[r],!0),[!0,n]}const f=new RegExp(`^[ \\t]*\`{${s},}$`);for(let r=n+1;r<e.length;r++){const s=e[r];if(f.test(s)){const c=s.match(f),l=e.slice(n+1,r),a=i.slice(o[0].length);return a.length>0&&l.unshift(a),K.replace(t,null,o,c,l,!0),[!0,r]}}const d=e.slice(n+1),g=i.slice(o[0].length);return g.length>0&&d.unshift(g),K.replace(t,null,o,null,d,!0),[!0,e.length-1]},regExpEnd:{optional:!0,regExp:b},regExpStart:A,replace:(t,n,o,s,i,c)=>{let l,a;const f=o[1]?o[1].trim():"```",d=o[2]||void 0;if(!n&&i){if(1===i.length)s?(l=r.$createCodeNode(d),a=i[0]):(l=r.$createCodeNode(d),a=i[0].startsWith(" ")?i[0].slice(1):i[0]);else{for(l=r.$createCodeNode(d),i.length>0&&(0===i[0].trim().length?i.shift():i[0].startsWith(" ")&&(i[0]=i[0].slice(1)));i.length>0&&!i[i.length-1].length;)i.pop();a=i.join("\n")}e.$setState(l,H,f);const n=e.$createTextNode(a);l.append(n),t.append(l)}else n&&U(e=>r.$createCodeNode(e?e[2]:void 0))(t,n,o,c)},type:"multiline-element"},W={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?q(e,n,0):null,regExp:y,replace:z("bullet"),type:"element"},X={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?q(e,n,0):null,regExp:_,replace:z("check"),type:"element"},Y={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?q(e,n,0):null,regExp:v,replace:z("number"),type:"element"},J={format:["code"],tag:"`",type:"text-format"},V={format:["highlight"],tag:"==",type:"text-format"},Z={format:["bold","italic"],tag:"***",type:"text-format"},ee={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},te={format:["bold"],tag:"**",type:"text-format"},ne={format:["bold"],intraword:!1,tag:"__",type:"text-format"},oe={format:["strikethrough"],tag:"~~",type:"text-format"},re={format:["italic"],tag:"*",type:"text-format"},se={format:["italic"],intraword:!1,tag:"_",type:"text-format"},ie={dependencies:[s.LinkNode],export:(e,t,n)=>{if(!s.$isLinkNode(e)||s.$isAutoLinkNode(e))return null;const o=e.getTitle(),r=t(e);return o?`[${r}](${e.getURL()} "${o}")`:`[${r}](${e.getURL()})`},importRegExp:/(?:\[(.+?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[\]]*(?:\[[^[\]]*\][^[\]]*)*)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,n)=>{if(e.$findMatchingParent(t,s.$isLinkNode))return;const[,o,r,i]=n,c=s.$createLinkNode(r,{title:i}),l=o.split("[").length-1,a=o.split("]").length-1;let f=o,d="";if(l<a)return;if(l>a){const e=o.split("[");d="["+e[0],f=e.slice(1).join("[")}const g=e.$createTextNode(f);return g.setFormat(t.getFormat()),c.append(g),t.replace(c),d&&c.insertBefore(e.$createTextNode(d)),g},trigger:")",type:"text-match"},ce=[G,Q,W,Y],le=[K],ae=[J,Z,ee,te,ne,V,re,se,oe],fe=[ie],de=[...ce,...le,...ae,...fe];function ge(t,n,o,r,s){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const c=n.getTextContent();if(!s&&" "!==c[o-1])return!1;for(const{regExpStart:e,replace:i,regExpEnd:l}of r){if(l&&!("optional"in l)||l&&"optional"in l&&!l.optional)continue;const r=c.match(e);if(r){const e=s||r[0].endsWith(" ")?o:o-1;if(r[0].length!==e)continue;const c=n.getNextSiblings(),[l,a]=n.splitText(o);if(!1!==i(t,a?[a,...c]:c,r,null,null,!1))return l.remove(),!0}}return!1}function ue(e,t,n){const o=n.length;for(let r=t;r>=o;r--){const t=r-o;if(pe(e,t,n,0,o)&&" "!==e[t+o])return t}return-1}function pe(e,t,n,o,r){for(let s=0;s<r;s++)if(e[t+s]!==n[o+s])return!1;return!0}exports.$convertFromMarkdownString=function(e,t=de,n,o=!1,r=!1){const s=o?e:function(e,t=!1){const n=e.split("\n");let o=!1;const r=[];for(let e=0;e<n.length;e++){const s=n[e].trimEnd(),i=r[r.length-1];k.test(s)?r.push(s):A.test(s)||b.test(s)?(o=!o,r.push(s)):o||""===s||""===i||!i||O.test(i)||O.test(s)||M.test(s)||v.test(s)||y.test(s)||_.test(s)||F.test(s)||w.test(s)||!t||B.test(s)||D.test(s)||P(D).test(i)||P(B).test(i)||b.test(i)?r.push(s):r[r.length-1]=i+" "+s.trimStart()}return r.join("\n")}(e,r);return C(t,o)(s,n)},exports.$convertToMarkdownString=function(t=de,n,o=!1){return function(t,n=!1){const o=c(t),r=[...o.multilineElement,...o.element],s=!n,i=o.textFormat.filter(e=>1===e.format.length).sort((e,t)=>Number(e.format.includes("code"))-Number(t.format.includes("code")));return t=>{const n=[],c=(t||e.$getRoot()).getChildren();for(let e=0;e<c.length;e++){const t=c[e],l=u(t,r,i,o.textMatch);null!=l&&n.push(s&&e>0&&!g(t)&&!g(c[e-1])?"\n".concat(l):l)}return n.join("\n")}}(t,o)(n)},exports.BOLD_ITALIC_STAR=Z,exports.BOLD_ITALIC_UNDERSCORE=ee,exports.BOLD_STAR=te,exports.BOLD_UNDERSCORE=ne,exports.CHECK_LIST=X,exports.CODE=K,exports.ELEMENT_TRANSFORMERS=ce,exports.HEADING=G,exports.HIGHLIGHT=V,exports.INLINE_CODE=J,exports.ITALIC_STAR=re,exports.ITALIC_UNDERSCORE=se,exports.LINK=ie,exports.MULTILINE_ELEMENT_TRANSFORMERS=le,exports.ORDERED_LIST=Y,exports.QUOTE=Q,exports.STRIKETHROUGH=oe,exports.TEXT_FORMAT_TRANSFORMERS=ae,exports.TEXT_MATCH_TRANSFORMERS=fe,exports.TRANSFORMERS=de,exports.UNORDERED_LIST=W,exports.registerMarkdownShortcuts=function(t,n=de){const o=c(n),s=i(o.textFormat,({tag:e})=>e[e.length-1]),a=i(o.textMatch,({trigger:e})=>e);for(const e of n){const n=e.type;if("element"===n||"text-match"===n||"multiline-element"===n){const n=e.dependencies;for(const e of n)t.hasNode(e)||R(173,e.getType())}}const f=(t,n,r)=>{(function(t,n,o,r){const s=t.getParent();if(!e.$isRootOrShadowRoot(s)||t.getFirstChild()!==n)return!1;const i=n.getTextContent();if(" "!==i[o-1])return!1;for(const{regExp:e,replace:s}of r){const r=i.match(e);if(r&&r[0].length===(r[0].endsWith(" ")?o:o-1)){const e=n.getNextSiblings(),[i,c]=n.splitText(o);if(!1!==s(t,c?[c,...e]:e,r,!1))return i.remove(),!0}}return!1})(t,n,r,o.element)||ge(t,n,r,o.multilineElement)||function(e,t,n){let o=e.getTextContent();const r=n[o[t-1]];if(null==r)return!1;t<o.length&&(o=o.slice(0,t));for(const t of r){if(!t.replace||!t.regExp)continue;const n=o.match(t.regExp);if(null===n)continue;const r=n.index||0,s=r+n[0].length;let i;return 0===r?[i]=e.splitText(s):[,i]=e.splitText(r,s),i.selectNext(0,0),t.replace(i,n),!0}return!1}(n,r,a)||function(t,n,o){const r=t.getTextContent(),s=n-1,i=r[s],c=o[i];if(!c)return!1;for(const n of c){const{tag:o}=n,c=o.length,a=s-c+1;if(c>1&&!pe(r,a,o,0,c))continue;if(" "===r[a-1])continue;const f=r[s+1];if(!1===n.intraword&&f&&!l.test(f))continue;const d=t;let g=d,u=ue(r,a,o),p=g;for(;u<0&&(p=p.getPreviousSibling())&&!e.$isLineBreakNode(p);)if(e.$isTextNode(p)){if(p.hasFormat("code"))continue;const e=p.getTextContent();g=p,u=ue(e,e.length,o)}if(u<0)continue;if(g===d&&u+c===a)continue;const x=g.getTextContent();if(u>0&&x[u-1]===i)continue;const h=x[u-1];if(!1===n.intraword&&h&&!l.test(h))continue;const m=d.getTextContent(),$=m.slice(0,a)+m.slice(s+1);d.setTextContent($);const N=g===d?$:x;g.setTextContent(N.slice(0,u)+N.slice(u+c));const T=e.$getSelection(),E=e.$createRangeSelection();e.$setSelection(E);const S=s-c*(g===d?2:1)+1;E.anchor.set(g.__key,u,"text"),E.focus.set(d.__key,S,"text");for(const e of n.format)E.hasFormat(e)||E.formatText(e);E.anchor.set(E.focus.key,E.focus.offset,E.focus.type);for(const e of n.format)E.hasFormat(e)&&E.toggleFormat(e);return e.$isRangeSelection(T)&&(E.format=T.format),!0}}(n,r,s)};return e.mergeRegister(t.registerUpdateListener(({tags:n,dirtyLeaves:o,editorState:s,prevEditorState:i})=>{if(n.has(e.COLLABORATION_TAG)||n.has(e.HISTORIC_TAG))return;if(t.isComposing())return;const c=s.read(e.$getSelection),l=i.read(e.$getSelection);if(!e.$isRangeSelection(l)||!e.$isRangeSelection(c)||!c.isCollapsed()||c.is(l))return;const a=c.anchor.key,d=c.anchor.offset,g=s._nodeMap.get(a);!e.$isTextNode(g)||!o.has(a)||1!==d&&d>l.anchor.offset+1||t.update(()=>{if(!E(g))return;const e=g.getParent();null===e||r.$isCodeNode(e)||f(e,g,c.anchor.offset)})}),t.registerCommand(e.KEY_ENTER_COMMAND,t=>{if(null!==t&&t.shiftKey)return!1;const n=e.$getSelection();if(!e.$isRangeSelection(n)||!n.isCollapsed())return!1;const s=n.anchor.offset,i=n.anchor.getNode();if(!e.$isTextNode(i)||!E(i))return!1;const c=i.getParent();if(null===c||r.$isCodeNode(c))return!1;return s===i.getTextContent().length&&(!!ge(c,i,s,o.multilineElement,!0)&&(null!==t&&t.preventDefault(),!0))},e.COMMAND_PRIORITY_LOW))};