@lexical/markdown
Version:
This package contains Markdown helpers and functionality for Lexical.
10 lines (8 loc) • 25.8 kB
JavaScript
/**
* 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.
*
*/
;var e=require("lexical"),t=require("@lexical/selection"),n=require("@lexical/code-core"),o=require("@lexical/link"),r=require("@lexical/list"),s=require("@lexical/rich-text");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 l(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 c=/[!-/:-@[-`{-~\s]/,a=/\s/,d=/[!"#$%&'()*+,\-./:;<=>?@[\]^_`{|}~]/,f=/^\s{0,3}$/;function u(t){if(!e.$isParagraphNode(t))return!1;const n=t.getFirstChild();return null==n||1===t.getChildrenSize()&&e.$isTextNode(n)&&f.test(n.getTextContent())}function g(e){return e.replace(/\\([!-/:-@[-`{-~])/g,"$1").replace(/&#(\d+);/g,(e,t)=>String.fromCodePoint(Number(t)))}const p=/^(\s*)(\d{1,})\.\s/,h=/^(\s*)[-*+]\s/,x=/^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i,m=/^(#{1,6})\s/,$=/^>\s/,N=/^([ \t]*`{3,})([\w-]+)?[ \t]?/,T=/^[ \t]*`{3,}$/,C=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,S=/^(?:\|)(.+)(?:\|)\s?$/;function E(e){if("|"!==e[0])return!1;const{length:t}=e;let n=1,o=0;for(;n<t;){let t=n;for(" "===e[t]&&t++,":"===e[t]&&t++;"-"===e[t];)t++;if(":"===e[t]&&t++," "===e[t]&&t++,"|"!==e[t])break;o++,n=t+1}return o>0&&(n===t||n===t-1&&/\s/.test(e[n]))}const I=/^<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i,R=/^<\/[a-z_][\w-]*\s*>/i,L=e=>new RegExp(`(?:${e.source})$`,e.flags),O=/* @__PURE__ */e.createState("mdListMarker",{parse:e=>"string"==typeof e&&/^[-*+]$/.test(e)?e:"-",resetOnCopyNode:!0}),v=/* @__PURE__ */e.createState("mdCodeFence",{parse:e=>"string"==typeof e&&/^`{3,}$/.test(e)?e:"```",resetOnCopyNode:!0}),_=/* @__PURE__ */e.createState("mdHardLineBreak",{parse:e=>"string"==typeof e&&/^(\\| {2,})$/.test(e)?e:"",resetOnCopyNode:!0});function y(e){if(e.endsWith("\\"))return[e.slice(0,-1),"\\"];const t=e.match(/^(.*?\S)( {2,})$/);return t?[t[1],t[2]]:null}function k(t){const n=t.getChildren(),o=n.length-1,r=n[o];if(!e.$isTextNode(r))return null;const s=r.getTextContent(),i=y(s);if(null!==i){const[e,t]=i;return r.setTextContent(e),t}return/^ {2,}$/.test(s)&&function(t,n){for(let o=n-1;o>=0;o--){if(e.$isLineBreakNode(t[o]))return!1;if(/\S/.test(t[o].getTextContent()))return!0}return!1}(n,o)?(r.setTextContent(""),s):null}function w(t){const n=e.$createLineBreakNode(),o=k(t);return null!==o&&e.$setState(n,_,o),n}const M=e=>(t,n,o,r)=>{const s=e(o);s.append(...n),t.replace(s),r||s.select(0,0)};const b=t=>(n,o,i,l)=>{if(s.$isHeadingNode(n))return!1;const c=n.getPreviousSibling(),a=n.getNextSibling(),d=r.$createListItemNode("check"===t?"x"===i[3]:void 0),f=i[0].trim()[0],u="bullet"!==t&&"check"!==t||f!==O.parse(f)?void 0:f;if(r.$isListNode(a)&&a.getListType()===t){u&&e.$setState(a,O,u);const o=a.getFirstChild();null!==o?o.insertBefore(d):a.append(d),"number"===t&&a.setStart(Number(i[2])),n.remove()}else if(r.$isListNode(c)&&c.getListType()===t)u&&e.$setState(c,O,u),c.append(d),n.remove();else{const o=r.$createListNode(t,"number"===t?Number(i[2]):void 0);u&&e.$setState(o,O,u),o.append(d),n.replace(o)}d.append(...o),l||d.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}(i[1]);g&&d.setIndent(g)},A=(t,n,o,s)=>{const i=[],l=t.getChildren();let c=0;for(const a of l)if(r.$isListItemNode(a)){if(1===a.getChildrenSize()){const e=a.getFirstChild();if(r.$isListNode(e)){const t=A(e,n,o+1,s);t&&i.push(t);continue}}if(s&&!a.getChildren().some(e=>e.isSelected(s)))continue;const l=" ".repeat(4*o),d=t.getListType(),f=e.$getState(t,O),u="number"===d?`${t.getStart()+c}. `:"check"===d?`${f} [${a.getChecked()?"x":" "}] `:f+" ";let g=n(a);"number"!==d&&(g=g.replace(/^(\s{0,3}\d+)(\.\s)/,"$1\\$2")),i.push(l+u+g),c++}return i.join("\n")},F={dependencies:[s.HeadingNode],export:(e,t)=>{if(!s.$isHeadingNode(e))return null;const n=Number(e.getTag().slice(1));return"#".repeat(n)+" "+t(e)},regExp:m,replace:M(e=>{const t="h"+e[1].length;return s.$createHeadingNode(t)}),triggerOnEnter:!0,type:"element"},B={dependencies:[s.QuoteNode],export:(e,t)=>{if(!s.$isQuoteNode(e))return null;const n=t(e).split("\n"),o=[];for(const e of n)o.push("> "+e);return o.join("\n")},regExp:$,replace:(e,t,n,o)=>{if(o){const n=e.getPreviousSibling();if(s.$isQuoteNode(n))return n.splice(n.getChildrenSize(),0,[w(n),...t]),void e.remove()}const r=s.$createQuoteNode();r.append(...t),e.replace(r),o||r.select(0,0)},triggerOnEnter:!0,type:"element"},D={dependencies:[n.CodeNode],export:t=>{if(!n.$isCodeNode(t))return null;const o=t.getTextContent();let r=e.$getState(t,v);if(o.indexOf(r)>-1){const e=o.match(/`{3,}/g);if(e){const t=Math.max(...e.map(e=>e.length));r="`".repeat(t+1)}}return r+(t.getLanguage()||"")+(o?"\n"+o:"")+"\n"+r},handleImportAfterStartMatch:({lines:e,rootNode:t,startLineIndex:n,startMatch:o})=>{const r=o[1],s=r.trim().length,i=e[n],l=o.index+r.length,c=i.slice(l),a=new RegExp(`\`{${s},}$`);if(a.test(c)){const e=c.match(a),r=c.slice(0,c.lastIndexOf(e[0])),s=[...o];return s[2]="",D.replace(t,null,s,e,[r],!0),[!0,n]}const d=new RegExp(`^[ \\t]*\`{${s},}$`);for(let r=n+1;r<e.length;r++){const s=e[r];if(d.test(s)){const l=s.match(d),c=e.slice(n+1,r),a=i.slice(o[0].length);return a.length>0&&c.unshift(a),D.replace(t,null,o,l,c,!0),[!0,r]}}const f=e.slice(n+1),u=i.slice(o[0].length);return u.length>0&&f.unshift(u),D.replace(t,null,o,null,f,!0),[!0,e.length-1]},regExpEnd:{optional:!0,regExp:T},regExpStart:N,replace:(t,o,r,s,i,l)=>{let c,a;const d=r[1]?r[1].trim():"```",f=r[2]||void 0;if(!o&&i){if(1===i.length)s?(c=n.$createCodeNode(f),a=i[0]):(c=n.$createCodeNode(f),a=i[0].startsWith(" ")?i[0].slice(1):i[0]);else{for(c=n.$createCodeNode(f),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(c,v,d);const o=e.$createTextNode(a);c.append(o),t.append(c)}else o&&M(e=>n.$createCodeNode(e?e[2]:void 0))(t,o,r,l)},type:"multiline-element"},P={dependencies:[r.ListNode,r.ListItemNode],export:(e,t,n)=>r.$isListNode(e)?A(e,t,0,n):null,regExp:h,replace:b("bullet"),triggerOnEnter:!0,type:"element"},H={dependencies:[r.ListNode,r.ListItemNode],export:(e,t,n)=>r.$isListNode(e)?A(e,t,0,n):null,regExp:x,replace:b("check"),triggerOnEnter:!0,type:"element"},U={dependencies:[r.ListNode,r.ListItemNode],export:(e,t,n)=>r.$isListNode(e)?A(e,t,0,n):null,regExp:p,replace:b("number"),triggerOnEnter:!0,type:"element"},j={format:["code"],tag:"`",type:"text-format"};const W={format:["highlight"],tag:"==",type:"text-format"},z={format:["bold","italic"],tag:"***",type:"text-format"},G={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},q={format:["bold"],tag:"**",type:"text-format"},Q={format:["bold"],intraword:!1,tag:"__",type:"text-format"},K={format:["strikethrough"],tag:"~~",type:"text-format"},Y={format:["italic"],tag:"*",type:"text-format"},X={format:["italic"],intraword:!1,tag:"_",type:"text-format"},J={dependencies:[o.LinkNode],export:(e,t,n)=>{if(!o.$isLinkNode(e)||o.$isAutoLinkNode(e))return null;const r=t(e);let s=e.getTitle();null!=s&&(s=s.replace(/([\\"])/g,"\\$1"));return s?`[${r}](${e.getURL()} "${s}")`:`[${r}](${e.getURL()})`},importRegExp:/(?:\[(.+?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[\]]*(?:\[[^[\]]*\][^[\]]*)*)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,n)=>{if(e.$findMatchingParent(t,o.$isLinkNode))return;const[,r,s,i]=n,l=null!=s?g(s):void 0,c=null!=i?g(i):void 0,a=o.$createLinkNode(l,{title:c}),d=r.split("[").length-1,f=r.split("]").length-1;let u=r,p="";if(d<f)return;if(d>f){const e=r.split("[");p="["+e[0],u=e.slice(1).join("[")}const h=e.$createTextNode(u);return h.setFormat(t.getFormat()),a.append(h),t.replace(a),p&&a.insertBefore(e.$createTextNode(p)),h},trigger:")",type:"text-match"},V=[F,B,P,U],Z=[D],ee=[j,z,G,q,Q,W,Y,X,K],te=[J],ne=[...V,...Z,...ee,...te];function oe(e,t=!1){const n=e.split("\n");let o=0;const r=[];for(let e=0;e<n.length;e++){const s=n[e],i=s.trimEnd(),l=r[r.length-1],c=e<n.length-1?y(s):null,a=void 0!==l&&null!==y(l);if(C.test(i))r.push(i);else if(0===o){{const e=i.match(N);if(e){o=e[1].trim().length,r.push(i);continue}}""===i||""===l||!l||m.test(l)||m.test(i)||$.test(i)||p.test(i)||h.test(i)||x.test(i)||S.test(i)||E(i)||a||!t||I.test(i)||R.test(i)||L(R).test(l)||L(I).test(l)||T.test(l)?r.push(!t&&""!==i||null!==c?s:i):r[r.length-1]=l+" "+(null===c?i:s).trimStart()}else{if(T.test(i)&&i.trim().length>=o){o=0,r.push(i);continue}r.push(s)}}return r.join("\n")}function re(t,n,o,r,s,i){let l=t.isSelected(n);for(const c of o){if(!c.export)continue;const o=c.export(t,e=>se(e,n,r,s,i).markdown,n);if(null!=o){if(!l&&e.$isElementNode(t)){se(t,n,r,s,i).shouldInclude&&(l=!0)}return{markdown:o,shouldInclude:l}}}if(e.$isElementNode(t)){const e=se(t,n,r,s,i);return{markdown:e.markdown,shouldInclude:l||e.shouldInclude}}return e.$isDecoratorNode(t)?{markdown:t.getTextContent(),shouldInclude:l}:{markdown:null,shouldInclude:l}}function se(n,o,r,s,i,l,c){const a=[],d=n.getChildren();let f=!1;l||(l=[]),c||(c=[]);e:for(const n of d){let d=n.isSelected(o);for(const u of s){if(!u.export)continue;const g=u.export(n,e=>se(e,o,r,s,i,l,[...c,...l]).markdown,(e,n)=>ae(e,t.$sliceSelectedTextNodeContent(o,e,"clone").getTextContent(),r,l,c,i));if(null!=g){!d&&e.$isElementNode(n)&&n.getChildren().some(e=>e.isSelected(o))&&n.extractWithChild(n,o,"html")&&(d=!0),d&&(a.push(g),f=!0);continue e}}if(e.$isLineBreakNode(n))d&&(a.push(ce(n)),f=!0);else if(e.$isTextNode(n)){if(d){const e=t.$sliceSelectedTextNodeContent(o,n,"clone");a.push(ae(n,e.getTextContent(),r,l,c,i)),f=!0}}else if(e.$isElementNode(n)){const e=se(n,o,r,s,i,l,c);!d&&e.shouldInclude&&n.extractWithChild(n,o,"html")&&(d=!0),(d||e.shouldInclude)&&(a.push(e.markdown),f=!0)}else e.$isDecoratorNode(n)&&d&&(a.push(n.getTextContent()),f=!0)}return{markdown:a.join(""),shouldInclude:f}}function ie(t,n,o,r,s){for(const e of n){if(!e.export)continue;const n=e.export(t,e=>le(e,o,r,void 0,void 0,s));if(null!=n)return n}return e.$isElementNode(t)?le(t,o,r,void 0,void 0,s):e.$isDecoratorNode(t)?t.getTextContent():null}function le(t,n,o,r,s,i=!1){const l=[],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=>le(e,n,o,r,[...s,...r],i),(e,t)=>ae(e,t,n,r,s,i));if(null!=c){l.push(c);continue e}}e.$isLineBreakNode(t)?l.push(ce(t)):e.$isTextNode(t)?l.push(ae(t,t.getTextContent(),n,r,s,i)):e.$isElementNode(t)?l.push(le(t,n,o,r,s,i)):e.$isDecoratorNode(t)&&l.push(t.getTextContent())}return l.join("")}function ce(t){return e.$getState(t,_)+"\n"}function ae(e,t,n,o,r,s=!1){const i=e.hasFormat("code");let l,c,a,d,f=t;if(i||(f=s?f.replace(/([*_`~])/g,"\\$1"):f.replace(/([*_`~\\])/g,"\\$1")),i){const{fence:e,padded:n}=function(e){const t=e.match(/`+/g),n=t?Math.max(...t.map(e=>e.length)):0;return{fence:"`".repeat(n+1),padded:0===e.length||e.includes("`")||/^\s/.test(e)&&/\s$/.test(e)?` ${e} `:e}}(t);l="",a="",c=e+n+e,d=!1}else{const e=f.match(/^(\s*)(.*?)(\s*)$/s)||["","",f,""];l=e[1],c=e[2],a=e[3],d=""===c}let u="",g="",p="";const h=de(e,!0),x=de(e,!1),m=new Set;for(const t of n){const n=t.format[0],r=t.tag;"code"!==n&&(ue(e,n)&&!m.has(n)&&(m.add(n),ue(h,n)&&o.find(e=>e.tag===r)||(o.push({format:n,tag:r}),u+=r)))}for(let t=0;t<o.length;t++){const n=fe(e,o[t].format),s=fe(x,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||(p+=e.tag):g+=e.tag),o.pop())}break}return d&&!e.hasFormat("code")?g+f:g+l+u+c+p+a}function de(t,n){const o=n?t.getPreviousSibling():t.getNextSibling();return e.$isTextNode(o)?o:null}function fe(t,n){return e.$isTextNode(t)&&t.hasFormat(n)}function ue(e,t){return!!fe(e,t)&&("code"===t||(!e||!/^\s*$/.test(e.getTextContent())))}function ge(e,t){const n=e.getTextContent(),o=t.transformersByTag["`"],r=[];let s=null;if(o){const e=function(e){const t=t=>{let n=0;for(let o=t-1;o>=0&&"\\"===e[o];o--)n++;return n%2==1},n=[];let o=0;for(;o<e.length;)if("`"===e[o]){let t=1;for(;o+t<e.length&&"`"===e[o+t];)t++;n.push({index:o,length:t}),o+=t}else o++;const r=[];let s=0;for(;s<n.length;){const o=n[s];if(t(o.index)){s++;continue}let i=-1;for(let e=s+1;e<n.length;e++)if(n[e].length===o.length){i=e;break}if(-1===i){s++;continue}const l=n[i];let c=e.slice(o.index+o.length,l.index);c.length>=2&&c.startsWith(" ")&&c.endsWith(" ")&&/[^ ]/.test(c)&&(c=c.slice(1,-1)),r.push({content:c,endIndex:l.index+l.length,startIndex:o.index}),s=i+1}return r}(n);for(const t of e)s||(s={content:t.content,endIndex:t.endIndex,startIndex:t.startIndex,tag:"`"}),r.push({end:t.endIndex,start:t.startIndex})}const i=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 l=0;for(;l<e.length;){const t=e[l];if(!r.has(t)||s(l)||i(l)){l++;continue}let n=1;for(;l+n<e.length&&e[l+n]===t;)n++;const c=pe(t,e,l,n,!0),a=pe(t,e,l,n,!1);(c||a)&&o.push({active:!0,canClose:a,canOpen:c,char:t,index:l,length:n}),l+=n}return o}(n,t,r),l=i.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 l=`${i.char}${i.canOpen}${i.length%3}`,c=o[l]??-1;let a=!1;for(let o=r-1;o>c;o--){const l=t[o];if(!l.active||!l.canOpen||0===l.length||l.char!==i.char)continue;if(l.canClose||i.canOpen){if((l.length+i.length)%3==0&&l.length%3!=0&&i.length%3!=0)continue}const c=Math.min(l.length,i.length),d=Object.keys(n.transformersByTag).filter(e=>e[0]===l.char&&e.length<=c).sort((e,t)=>t.length-e.length)[0];if(!d)continue;a=!0;const f=d.length,u={content:e.slice(l.index+l.length,i.index),endIndex:i.index+f,startIndex:l.index+(l.length-f),tag:d};(!s||u.startIndex<s.startIndex||u.startIndex===s.startIndex&&u.endIndex>s.endIndex)&&(s=u);for(let e=o+1;e<r;e++)t[e].active=!1;l.length-=f,i.length-=f,l.active=l.length>0,i.length>0?i.index+=f:(i.active=!1,r++);break}a||(o[l]=r-1,i.canOpen||(i.active=!1),r++)}return s}(n,i,t):null;let c=null,a=null;if(s&&l?l.startIndex<=s.startIndex&&l.endIndex>=s.endIndex?(c=l,a=t.transformersByTag[l.tag]):(c=s,a=o):s?(c=s,a=o):l&&(c=l,a=t.transformersByTag[l.tag]),!c||!a)return null;const d=[n.slice(c.startIndex,c.endIndex),c.tag,c.content];return d.index=c.startIndex,d.input=n,{endIndex:c.endIndex,isCodeSpan:a===o,match:d,startIndex:c.startIndex,transformer:a}}function pe(e,t,n,o,r){if(!he(t,n,o,r))return!1;if("*"===e)return!0;if("_"===e){if(!he(t,n,o,!r))return!0;const e=r?t[n-1]:t[n+o];return void 0!==e&&d.test(e)}return!0}function he(e,t,n,o){const r=e[t-1],s=e[t+n],[i,l]=o?[s,r]:[r,s];return void 0!==i&&!a.test(i)&&(!d.test(i)||(void 0===l||a.test(l)||d.test(l)))}function xe(t){return e.$isTextNode(t)&&!t.hasFormat("code")}function me(e,t,n){let o=ge(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 l=t.index||0,c=e.getEndIndex?e.getEndIndex(n,t):l+t[0].length;!1!==c&&(void 0===o||void 0===r||l<o&&(c>r||c<=o))&&(o=l,r=c,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.isCodeSpan?r.startIndex<=o.startIndex&&r.endIndex>=o.endIndex?o=null:r=null: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,l,c;if(r[0]===s?i=e:0===t?[i,l]=e.splitText(n):[c,i,l]=e.splitText(t,n),i.setTextContent(r[2]),o)for(const e of o.format)i.hasFormat(e)||i.toggleFormat(e);return{nodeAfter:l,nodeBefore:c,transformedNode:i}}(e,o.startIndex,o.endIndex,o.transformer,o.match);xe(r.nodeAfter)&&me(r.nodeAfter,t,n),xe(r.nodeBefore)&&me(r.nodeBefore,t,n),xe(r.transformedNode)&&me(r.transformedNode,t,n)}else if(r){const o=function(e,t,n,o,r){let s,i,l;return 0===t?[s,i]=e.splitText(n):[l,s,i]=e.splitText(t,n),o.replace?{nodeAfter:i,nodeBefore:l,transformedNode:o.replace(s,r)||void 0}:null}(e,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;xe(o.nodeAfter)&&me(o.nodeAfter,t,n),xe(o.nodeBefore)&&me(o.nodeBefore,t,n),xe(o.transformedNode)&&me(o.transformedNode,t,n)}const s=g(e.getTextContent());e.setTextContent(s)}function $e(t,n,o,r=!1){const s=l(o),i=function(e){const t={},n={},o=[];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(`(${o.join("|")})`,"g"),transformersByTag:t}}(s.textFormat),c=t.split("\n"),a=c.length;for(let e=0;e<a;e++){const t=c[e],[o,l]=Ne(c,e,s.multilineElement,n);o?e=l:Te(t,n,s.element,i,s.textMatch,r)}const d=n.getChildren();for(const t of d)if(!r&&u(t)&&n.getChildrenSize()>1)t.remove();else if(e.$isElementNode(t))for(const e of t.getAllTextNodes())Ce(e)}function Ne(e,t,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:s,regExpStart:i,replace:l}=r,c=e[t].match(i);if(!c)continue;if(n){const s=n({lines:e,rootNode:o,startLineIndex:t,startMatch:c,transformer:r});if(null===s)continue;if(s)return s}const a="object"==typeof s&&"regExp"in s?s.regExp:s,d=s&&"object"==typeof s&&"optional"in s?s.optional:!s;let f=t;const u=e.length;for(;f<u;){const n=a?e[f].match(a):null;if(!n&&(!d||d&&f<u-1)){f++;continue}if(n&&t===f&&n.index===c.index){f++;continue}const r=[];if(n&&t===f)r.push(e[t].slice(c[0].length,-n[0].length));else for(let o=t;o<=f;o++)if(o===t){const t=e[o].slice(c[0].length);r.push(t)}else if(o===f&&n){const t=e[o].slice(0,-n[0].length);r.push(t)}else r.push(e[o]);if(!1!==l(o,null,c,n,r,!0))return[!0,f];break}}return[!1,t]}function Te(t,n,o,i,l,c){const a=e.$createTextNode(t),d=e.$createParagraphNode();d.append(a),n.append(d);for(const{regExp:e,replace:n}of o){const o=t.match(e);if(o&&(a.setTextContent(t.slice(o[0].length)),!1!==n(d,[a],o,!0)))break}if(me(a,i,l),null!==d.getParent()&&t.length>0){const t=d.getPreviousSibling();if(!c&&(e.$isParagraphNode(t)||s.$isQuoteNode(t)||r.$isListNode(t))){let n=t;if(r.$isListNode(t)){const o=t.getLastDescendant();n=null==o?null:e.$findMatchingParent(o,r.$isListItemNode)}null!=n&&n.getTextContentSize()>0&&(n.splice(n.getChildrenSize(),0,[w(n),...d.getChildren()]),d.remove())}}}function Ce(t){const n=new Set,o=t.getTextContent();let r=o.indexOf("\t");for(;-1!==r;)n.add(r),n.add(r+1),r=o.indexOf("\t",r+1);t.splitText(...n).forEach(t=>{"\t"===t.getTextContent()&&t.replace(e.$createTabNode())})}function Se(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.`)}function Ee(t,n,o,r,s){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const l=n.getTextContent();if(!s&&" "!==l[o-1])return!1;for(const{regExp:e,replace:i}of r){const r=l.match(e),c=s||r&&r[0].endsWith(" ")?o:o-1;if(r&&r[0].length===c){const e=n.getNextSiblings(),[s,l]=n.splitText(o);if(!1!==i(t,l?[l,...e]:e,r,!1))return s.remove(),!0}}return!1}function Ie(t,n,o,r,s){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const l=n.getTextContent();if(!s&&" "!==l[o-1])return!1;for(const{regExpStart:e,replace:i,regExpEnd:c}of r){if(c&&!("optional"in c)||c&&"optional"in c&&!c.optional)continue;const r=l.match(e);if(r){const e=s||r[0].endsWith(" ")?o:o-1;if(r[0].length!==e)continue;const l=n.getNextSiblings(),[c,a]=n.splitText(o);if(!1!==i(t,a?[a,...l]:l,r,null,null,!1))return c.remove(),!0}}return!1}function Re(e,t){let n=0;const o=e.getTextContent();for(let e=0;e<t;e++)"`"===o[e]&&n++;return n%2!=0}function Le(e,t,n){const o=n.length;for(let r=t;r>=o;r--){const t=r-o;if(Oe(e,t,n,0,o)&&" "!==e[t+o])return t}return-1}function Oe(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(t,n=ne,o,r=!1,s=!1){const i=r?t:oe(t,s),l=o||e.$getRoot();l.clear(),$e(i,l,n,r),null!==e.$getSelection()&&l.selectStart()},exports.$convertSelectionToMarkdownString=function(t=ne,n,o=!1){if(!n||e.$isRangeSelection(n)&&n.isCollapsed())return"";const r=function(t,n=!1){const o=l(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 l=[],c=e.$getRoot().getChildren();for(let e=0;e<c.length;e++){const a=c[e],{shouldInclude:d,markdown:f}=re(a,t,r,i,o.textMatch,n);d&&null!=f&&l.push(s&&e>0&&!u(a)&&!u(c[e-1])?"\n".concat(f):f)}return l.join("\n")}}(t,o);return r(n)},exports.$convertToMarkdownString=function(t=ne,n,o=!1){return function(t,n=!1){const o=l(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 l=[],c=(t||e.$getRoot()).getChildren();for(let e=0;e<c.length;e++){const t=c[e],a=ie(t,r,i,o.textMatch,n);null!=a&&l.push(s&&e>0&&!u(t)&&!u(c[e-1])?"\n".concat(a):a)}return l.join("\n")}}(t,o)(n)},exports.$generateNodesFromMarkdownString=function(t,n=ne,o=!1,r=!1){const s=o?t:oe(t,r),i=new e.ArtificialNode__DO_NOT_USE;return $e(s,i,n,o),i.getChildren()},exports.BOLD_ITALIC_STAR=z,exports.BOLD_ITALIC_UNDERSCORE=G,exports.BOLD_STAR=q,exports.BOLD_UNDERSCORE=Q,exports.CHECK_LIST=H,exports.CODE=D,exports.ELEMENT_TRANSFORMERS=V,exports.HEADING=F,exports.HIGHLIGHT=W,exports.INLINE_CODE=j,exports.ITALIC_STAR=Y,exports.ITALIC_UNDERSCORE=X,exports.LINK=J,exports.MULTILINE_ELEMENT_TRANSFORMERS=Z,exports.ORDERED_LIST=U,exports.QUOTE=B,exports.STRIKETHROUGH=K,exports.TEXT_FORMAT_TRANSFORMERS=ee,exports.TEXT_MATCH_TRANSFORMERS=te,exports.TRANSFORMERS=ne,exports.UNORDERED_LIST=P,exports.isTableRowDivider=E,exports.registerMarkdownShortcuts=function(t,o=ne){const r=l(o),s=r.element.filter(e=>e.triggerOnEnter),a=i(r.textFormat,({tag:e})=>e[e.length-1]),d=i(r.textMatch,({trigger:e})=>e),f=new Set([" "]);for(const e of r.textFormat)f.add(e.tag.slice(-1));for(const e of r.textMatch)void 0!==e.trigger&&f.add(e.trigger);for(const e of o){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)||Se(173,e.getType())}}const u=(t,n,o)=>!!Ee(t,n,o,r.element)||(!!Ie(t,n,o,r.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,o,d)||!!function(t,n,o){const r=t.getTextContent(),s=n-1,i=r[s],l=o[i];if(!l)return!1;for(const n of l){const{tag:o}=n,l=o.length,a=s-l+1;if(l>1&&!Oe(r,a,o,0,l))continue;if(" "===r[a-1])continue;const d=r[s+1];if(!1===n.intraword&&d&&!c.test(d))continue;const f=t;let u=f,g=Le(r,a,o),p=u;for(;g<0&&(p=p.getPreviousSibling())&&!e.$isLineBreakNode(p);)if(e.$isTextNode(p)){if(p.hasFormat("code"))continue;const e=p.getTextContent();u=p,g=Le(e,e.length,o)}if(g<0)continue;if(u===f&&g+l===a)continue;const h=u.getTextContent();if(g>0&&h[g-1]===i)continue;const x=h[g-1];if(!1===n.intraword&&x&&!c.test(x))continue;if(!n.format.includes("code")&&Re(u,g))continue;const m=f.getTextContent(),$=m.slice(0,a)+m.slice(s+1);f.setTextContent($);const N=u===f?$:h;u.setTextContent(N.slice(0,g)+N.slice(g+l));const T=e.$getSelection(),C=e.$createRangeSelection();e.$setSelection(C);const S=s-l*(u===f?2:1)+1;C.anchor.set(u.__key,g,"text"),C.focus.set(f.__key,S,"text");for(const t of n.format)C.formatText(t,e.TEXT_TYPE_TO_FORMAT[t]);C.anchor.set(C.focus.key,C.focus.offset,C.focus.type);for(const e of n.format)C.hasFormat(e)&&C.toggleFormat(e);return e.$isRangeSelection(T)&&(C.format=T.format),!0}return!1}(n,o,a)));return e.mergeRegister(t.registerUpdateListener(({tags:o,dirtyLeaves:r,editorState:s,prevEditorState:i})=>{if(o.has(e.COLLABORATION_TAG)||o.has(e.HISTORIC_TAG))return;if(t.isComposing())return;const l=o.has(e.COMPOSITION_END_TAG),c=s.read(e.$getSelection),a=i.read(e.$getSelection);if(!e.$isRangeSelection(a)||!e.$isRangeSelection(c)||!c.isCollapsed()||c.is(a)&&!l)return;const d=c.anchor.key,g=c.anchor.offset,p=s._nodeMap.get(d);if(e.$isTextNode(p)&&r.has(d)&&(l||1===g||!(g>a.anchor.offset+1))){if(l){const e=s.read(()=>p.getTextContent())[g-1];if(!f.has(e))return}t.update(()=>{if(!xe(p))return;const t=p.getParent();null===t||n.$isCodeNode(t)||u(t,p,c.anchor.offset)&&e.$addUpdateTag(e.HISTORY_PUSH_TAG)})}}),t.registerCommand(e.KEY_ENTER_COMMAND,t=>{if(null!==t&&t.shiftKey)return!1;const o=e.$getSelection();if(!e.$isRangeSelection(o)||!o.isCollapsed())return!1;const i=o.anchor.offset,l=o.anchor.getNode();if(!e.$isTextNode(l)||!xe(l))return!1;const c=l.getParent();if(null===c||n.$isCodeNode(c))return!1;return i===l.getTextContent().length&&(!(!Ie(c,l,i,r.multilineElement,!0)&&!Ee(c,l,i,s,!0))&&(null!==t&&t.preventDefault(),!0))},e.COMMAND_PRIORITY_LOW))};