@lexical/markdown
Version:
This package contains Markdown helpers and functionality for Lexical.
10 lines (8 loc) • 25.2 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.
*
*/
import{$isParagraphNode as t,$isTextNode as e,$setState as n,$createTextNode as o,$getState as r,$findMatchingParent as s,$createLineBreakNode as i,createState as l,$isLineBreakNode as c,$getRoot as f,$isElementNode as a,$isDecoratorNode as u,$createParagraphNode as d,$createTabNode as g,mergeRegister as h,COLLABORATION_TAG as p,HISTORIC_TAG as x,COMPOSITION_END_TAG as m,$getSelection as C,$isRangeSelection as I,$addUpdateTag as T,HISTORY_PUSH_TAG as $,KEY_ENTER_COMMAND as E,COMMAND_PRIORITY_LOW as v,$isRootOrShadowRoot as y,$createRangeSelection as S,$setSelection as b,TEXT_TYPE_TO_FORMAT as w,ArtificialNode__DO_NOT_USE as k}from"lexical";import{$sliceSelectedTextNodeContent as N}from"@lexical/selection";import{CodeNode as F,$createCodeNode as O,$isCodeNode as M}from"@lexical/code-core";import{LinkNode as _,$isLinkNode as B,$createLinkNode as L,$isAutoLinkNode as R}from"@lexical/link";import{ListNode as j,ListItemNode as P,$isListNode as A,$createListItemNode as W,$createListNode as z,$isListItemNode as U}from"@lexical/list";import{HeadingNode as D,QuoteNode as H,$isHeadingNode as K,$isQuoteNode as q,$createQuoteNode as G,$createHeadingNode as J}from"@lexical/rich-text";function Q(t,e){const n={};for(const o of t){const t=e(o);t&&(n[t]?n[t].push(o):n[t]=[o])}return n}function V(t){const e=Q(t,t=>t.type);return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const X=/[!-/:-@[-`{-~\s]/,Y=/\s/,Z=/[!"#$%&'()*+,\-./:;<=>?@[\]^_`{|}~]/,tt=/^\s{0,3}$/;function et(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&tt.test(o.getTextContent())}function nt(t){return t.replace(/\\([!-/:-@[-`{-~])/g,"$1").replace(/&#(\d+);/g,(t,e)=>String.fromCodePoint(Number(e)))}const ot=/^(\s*)(\d{1,})\.\s/,rt=/^(\s*)[-*+]\s/,st=/^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i,it=/^(#{1,6})\s/,lt=/^>\s/,ct=/^([ \t]*`{3,})([\w-]+)?[ \t]?/,ft=/^[ \t]*`{3,}$/,at=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,ut=/^(?:\|)(.+)(?:\|)\s?$/;function dt(t){if("|"!==t[0])return!1;const{length:e}=t;let n=1,o=0;for(;n<e;){let e=n;for(" "===t[e]&&e++,":"===t[e]&&e++;"-"===t[e];)e++;if(":"===t[e]&&e++," "===t[e]&&e++,"|"!==t[e])break;o++,n=e+1}return o>0&&(n===e||n===e-1&&/\s/.test(t[n]))}const gt=/^<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i,ht=/^<\/[a-z_][\w-]*\s*>/i,pt=t=>new RegExp(`(?:${t.source})$`,t.flags),xt=/* @__PURE__ */l("mdListMarker",{parse:t=>"string"==typeof t&&/^[-*+]$/.test(t)?t:"-",resetOnCopyNode:!0}),mt=/* @__PURE__ */l("mdCodeFence",{parse:t=>"string"==typeof t&&/^`{3,}$/.test(t)?t:"```",resetOnCopyNode:!0}),Ct=/* @__PURE__ */l("mdHardLineBreak",{parse:t=>"string"==typeof t&&/^(\\| {2,})$/.test(t)?t:"",resetOnCopyNode:!0});function It(t){if(t.endsWith("\\"))return[t.slice(0,-1),"\\"];const e=t.match(/^(.*?\S)( {2,})$/);return e?[e[1],e[2]]:null}function Tt(t){const n=t.getChildren(),o=n.length-1,r=n[o];if(!e(r))return null;const s=r.getTextContent(),i=It(s);if(null!==i){const[t,e]=i;return r.setTextContent(t),e}return/^ {2,}$/.test(s)&&function(t,e){for(let n=e-1;n>=0;n--){if(c(t[n]))return!1;if(/\S/.test(t[n].getTextContent()))return!0}return!1}(n,o)?(r.setTextContent(""),s):null}function $t(t){const e=i(),o=Tt(t);return null!==o&&n(e,Ct,o),e}const Et=t=>(e,n,o,r)=>{const s=t(o);s.append(...n),e.replace(s),r||s.select(0,0)};const vt=t=>(e,o,r,s)=>{if(K(e))return!1;const i=e.getPreviousSibling(),l=e.getNextSibling(),c=W("check"===t?"x"===r[3]:void 0),f=r[0].trim()[0],a="bullet"!==t&&"check"!==t||f!==xt.parse(f)?void 0:f;if(A(l)&&l.getListType()===t){a&&n(l,xt,a);const o=l.getFirstChild();null!==o?o.insertBefore(c):l.append(c),"number"===t&&l.setStart(Number(r[2])),e.remove()}else if(A(i)&&i.getListType()===t)a&&n(i,xt,a),i.append(c),e.remove();else{const o=z(t,"number"===t?Number(r[2]):void 0);a&&n(o,xt,a),o.append(c),e.replace(o)}c.append(...o),s||c.select(0,0);const u=function(t){const e=t.match(/\t/g),n=t.match(/ /g);let o=0;return e&&(o+=e.length),n&&(o+=Math.floor(n.length/4)),o}(r[1]);u&&c.setIndent(u)},yt=(t,e,n,o)=>{const s=[],i=t.getChildren();let l=0;for(const c of i)if(U(c)){if(1===c.getChildrenSize()){const t=c.getFirstChild();if(A(t)){const r=yt(t,e,n+1,o);r&&s.push(r);continue}}if(o&&!c.getChildren().some(t=>t.isSelected(o)))continue;const i=" ".repeat(4*n),f=t.getListType(),a=r(t,xt),u="number"===f?`${t.getStart()+l}. `:"check"===f?`${a} [${c.getChecked()?"x":" "}] `:a+" ";let d=e(c);"number"!==f&&(d=d.replace(/^(\s{0,3}\d+)(\.\s)/,"$1\\$2")),s.push(i+u+d),l++}return s.join("\n")},St={dependencies:[D],export:(t,e)=>{if(!K(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:it,replace:Et(t=>{const e="h"+t[1].length;return J(e)}),triggerOnEnter:!0,type:"element"},bt={dependencies:[H],export:(t,e)=>{if(!q(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:lt,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(q(n))return n.splice(n.getChildrenSize(),0,[$t(n),...e]),void t.remove()}const r=G();r.append(...e),t.replace(r),o||r.select(0,0)},triggerOnEnter:!0,type:"element"},wt={dependencies:[F],export:t=>{if(!M(t))return null;const e=t.getTextContent();let n=r(t,mt);if(e.indexOf(n)>-1){const t=e.match(/`{3,}/g);if(t){const e=Math.max(...t.map(t=>t.length));n="`".repeat(e+1)}}return n+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n"+n},handleImportAfterStartMatch:({lines:t,rootNode:e,startLineIndex:n,startMatch:o})=>{const r=o[1],s=r.trim().length,i=t[n],l=o.index+r.length,c=i.slice(l),f=new RegExp(`\`{${s},}$`);if(f.test(c)){const t=c.match(f),r=c.slice(0,c.lastIndexOf(t[0])),s=[...o];return s[2]="",wt.replace(e,null,s,t,[r],!0),[!0,n]}const a=new RegExp(`^[ \\t]*\`{${s},}$`);for(let r=n+1;r<t.length;r++){const s=t[r];if(a.test(s)){const l=s.match(a),c=t.slice(n+1,r),f=i.slice(o[0].length);return f.length>0&&c.unshift(f),wt.replace(e,null,o,l,c,!0),[!0,r]}}const u=t.slice(n+1),d=i.slice(o[0].length);return d.length>0&&u.unshift(d),wt.replace(e,null,o,null,u,!0),[!0,t.length-1]},regExpEnd:{optional:!0,regExp:ft},regExpStart:ct,replace:(t,e,r,s,i,l)=>{let c,f;const a=r[1]?r[1].trim():"```",u=r[2]||void 0;if(!e&&i){if(1===i.length)s?(c=O(u),f=i[0]):(c=O(u),f=i[0].startsWith(" ")?i[0].slice(1):i[0]);else{for(c=O(u),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();f=i.join("\n")}n(c,mt,a);const e=o(f);c.append(e),t.append(c)}else e&&Et(t=>O(t?t[2]:void 0))(t,e,r,l)},type:"multiline-element"},kt={dependencies:[j,P],export:(t,e,n)=>A(t)?yt(t,e,0,n):null,regExp:rt,replace:vt("bullet"),triggerOnEnter:!0,type:"element"},Nt={dependencies:[j,P],export:(t,e,n)=>A(t)?yt(t,e,0,n):null,regExp:st,replace:vt("check"),triggerOnEnter:!0,type:"element"},Ft={dependencies:[j,P],export:(t,e,n)=>A(t)?yt(t,e,0,n):null,regExp:ot,replace:vt("number"),triggerOnEnter:!0,type:"element"},Ot={format:["code"],tag:"`",type:"text-format"};const Mt={format:["highlight"],tag:"==",type:"text-format"},_t={format:["bold","italic"],tag:"***",type:"text-format"},Bt={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},Lt={format:["bold"],tag:"**",type:"text-format"},Rt={format:["bold"],intraword:!1,tag:"__",type:"text-format"},jt={format:["strikethrough"],tag:"~~",type:"text-format"},Pt={format:["italic"],tag:"*",type:"text-format"},At={format:["italic"],intraword:!1,tag:"_",type:"text-format"},Wt={dependencies:[_],export:(t,e,n)=>{if(!B(t)||R(t))return null;const o=e(t);let r=t.getTitle();null!=r&&(r=r.replace(/([\\"])/g,"\\$1"));return r?`[${o}](${t.getURL()} "${r}")`:`[${o}](${t.getURL()})`},importRegExp:/(?:\[(.+?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[\]]*(?:\[[^[\]]*\][^[\]]*)*)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,e)=>{if(s(t,B))return;const[,n,r,i]=e,l=null!=r?nt(r):void 0,c=null!=i?nt(i):void 0,f=L(l,{title:c}),a=n.split("[").length-1,u=n.split("]").length-1;let d=n,g="";if(a<u)return;if(a>u){const t=n.split("[");g="["+t[0],d=t.slice(1).join("[")}const h=o(d);return h.setFormat(t.getFormat()),f.append(h),t.replace(f),g&&f.insertBefore(o(g)),h},trigger:")",type:"text-match"},zt=[St,bt,kt,Ft],Ut=[wt],Dt=[Ot,_t,Bt,Lt,Rt,Mt,Pt,At,jt],Ht=[Wt],Kt=[...zt,...Ut,...Dt,...Ht];function qt(t,e=!1){const n=t.split("\n");let o=0;const r=[];for(let t=0;t<n.length;t++){const s=n[t],i=s.trimEnd(),l=r[r.length-1],c=t<n.length-1?It(s):null,f=void 0!==l&&null!==It(l);if(at.test(i))r.push(i);else if(0===o){{const t=i.match(ct);if(t){o=t[1].trim().length,r.push(i);continue}}""===i||""===l||!l||it.test(l)||it.test(i)||lt.test(i)||ot.test(i)||rt.test(i)||st.test(i)||ut.test(i)||dt(i)||f||!e||gt.test(i)||ht.test(i)||pt(ht).test(l)||pt(gt).test(l)||ft.test(l)?r.push(!e&&""!==i||null!==c?s:i):r[r.length-1]=l+" "+(null===c?i:s).trimStart()}else{if(ft.test(i)&&i.trim().length>=o){o=0,r.push(i);continue}r.push(s)}}return r.join("\n")}function Gt(t,e,n,o,r,s){let i=t.isSelected(e);for(const l of n){if(!l.export)continue;const n=l.export(t,t=>Jt(t,e,o,r,s).markdown,e);if(null!=n){if(!i&&a(t)){Jt(t,e,o,r,s).shouldInclude&&(i=!0)}return{markdown:n,shouldInclude:i}}}if(a(t)){const n=Jt(t,e,o,r,s);return{markdown:n.markdown,shouldInclude:i||n.shouldInclude}}return u(t)?{markdown:t.getTextContent(),shouldInclude:i}:{markdown:null,shouldInclude:i}}function Jt(t,n,o,r,s,i,l){const f=[],d=t.getChildren();let g=!1;i||(i=[]),l||(l=[]);t:for(const t of d){let d=t.isSelected(n);for(const e of r){if(!e.export)continue;const c=e.export(t,t=>Jt(t,n,o,r,s,i,[...l,...i]).markdown,(t,e)=>Yt(t,N(n,t,"clone").getTextContent(),o,i,l,s));if(null!=c){!d&&a(t)&&t.getChildren().some(t=>t.isSelected(n))&&t.extractWithChild(t,n,"html")&&(d=!0),d&&(f.push(c),g=!0);continue t}}if(c(t))d&&(f.push(Xt(t)),g=!0);else if(e(t)){if(d){const e=N(n,t,"clone");f.push(Yt(t,e.getTextContent(),o,i,l,s)),g=!0}}else if(a(t)){const e=Jt(t,n,o,r,s,i,l);!d&&e.shouldInclude&&t.extractWithChild(t,n,"html")&&(d=!0),(d||e.shouldInclude)&&(f.push(e.markdown),g=!0)}else u(t)&&d&&(f.push(t.getTextContent()),g=!0)}return{markdown:f.join(""),shouldInclude:g}}function Qt(t,e,n,o,r){for(const s of e){if(!s.export)continue;const e=s.export(t,t=>Vt(t,n,o,void 0,void 0,r));if(null!=e)return e}return a(t)?Vt(t,n,o,void 0,void 0,r):u(t)?t.getTextContent():null}function Vt(t,n,o,r,s,i=!1){const l=[],f=t.getChildren();r||(r=[]),s||(s=[]);t:for(const t of f){for(const e of o){if(!e.export)continue;const c=e.export(t,t=>Vt(t,n,o,r,[...s,...r],i),(t,e)=>Yt(t,e,n,r,s,i));if(null!=c){l.push(c);continue t}}c(t)?l.push(Xt(t)):e(t)?l.push(Yt(t,t.getTextContent(),n,r,s,i)):a(t)?l.push(Vt(t,n,o,r,s,i)):u(t)&&l.push(t.getTextContent())}return l.join("")}function Xt(t){return r(t,Ct)+"\n"}function Yt(t,e,n,o,r,s=!1){const i=t.hasFormat("code");let l,c,f,a,u=e;if(i||(u=s?u.replace(/([*_`~])/g,"\\$1"):u.replace(/([*_`~\\])/g,"\\$1")),i){const{fence:t,padded:n}=function(t){const e=t.match(/`+/g),n=e?Math.max(...e.map(t=>t.length)):0;return{fence:"`".repeat(n+1),padded:0===t.length||t.includes("`")||/^\s/.test(t)&&/\s$/.test(t)?` ${t} `:t}}(e);l="",f="",c=t+n+t,a=!1}else{const t=u.match(/^(\s*)(.*?)(\s*)$/s)||["","",u,""];l=t[1],c=t[2],f=t[3],a=""===c}let d="",g="",h="";const p=Zt(t,!0),x=Zt(t,!1),m=new Set;for(const e of n){const n=e.format[0],r=e.tag;"code"!==n&&(ee(t,n)&&!m.has(n)&&(m.add(n),ee(p,n)&&o.find(t=>t.tag===r)||(o.push({format:n,tag:r}),d+=r)))}for(let e=0;e<o.length;e++){const n=te(t,o[e].format),s=te(x,o[e].format);if(n&&s)continue;const i=[...o];for(;i.length>e;){const t=i.pop();r&&t&&r.find(e=>e.tag===t.tag)||(t&&"string"==typeof t.tag&&(n?s||(h+=t.tag):g+=t.tag),o.pop())}break}return a&&!t.hasFormat("code")?g+u:g+l+d+c+h+f}function Zt(t,n){const o=n?t.getPreviousSibling():t.getNextSibling();return e(o)?o:null}function te(t,n){return e(t)&&t.hasFormat(n)}function ee(t,e){return!!te(t,e)&&("code"===e||(!t||!/^\s*$/.test(t.getTextContent())))}function ne(t,e){const n=t.getTextContent(),o=e.transformersByTag["`"],r=[];let s=null;if(o){const t=function(t){const e=e=>{let n=0;for(let o=e-1;o>=0&&"\\"===t[o];o--)n++;return n%2==1},n=[];let o=0;for(;o<t.length;)if("`"===t[o]){let e=1;for(;o+e<t.length&&"`"===t[o+e];)e++;n.push({index:o,length:e}),o+=e}else o++;const r=[];let s=0;for(;s<n.length;){const o=n[s];if(e(o.index)){s++;continue}let i=-1;for(let t=s+1;t<n.length;t++)if(n[t].length===o.length){i=t;break}if(-1===i){s++;continue}const l=n[i];let c=t.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 e of t)s||(s={content:e.content,endIndex:e.endIndex,startIndex:e.startIndex,tag:"`"}),r.push({end:e.endIndex,start:e.startIndex})}const i=function(t,e,n=[]){const o=[],r=new Set(Object.keys(e.transformersByTag).filter(t=>"`"!==t[0]).map(t=>t[0])),s=e=>{let n=0;for(let o=e-1;o>=0&&"\\"===t[o];o--)n++;return n%2==1},i=t=>n.some(e=>t>=e.start&&t<e.end);let l=0;for(;l<t.length;){const e=t[l];if(!r.has(e)||s(l)||i(l)){l++;continue}let n=1;for(;l+n<t.length&&t[l+n]===e;)n++;const c=oe(e,t,l,n,!0),f=oe(e,t,l,n,!1);(c||f)&&o.push({active:!0,canClose:f,canOpen:c,char:e,index:l,length:n}),l+=n}return o}(n,e,r),l=i.length>0?function(t,e,n){const o={};let r=0,s=null;for(;r<e.length;){const i=e[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 f=!1;for(let o=r-1;o>c;o--){const l=e[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),a=Object.keys(n.transformersByTag).filter(t=>t[0]===l.char&&t.length<=c).sort((t,e)=>e.length-t.length)[0];if(!a)continue;f=!0;const u=a.length,d={content:t.slice(l.index+l.length,i.index),endIndex:i.index+u,startIndex:l.index+(l.length-u),tag:a};(!s||d.startIndex<s.startIndex||d.startIndex===s.startIndex&&d.endIndex>s.endIndex)&&(s=d);for(let t=o+1;t<r;t++)e[t].active=!1;l.length-=u,i.length-=u,l.active=l.length>0,i.length>0?i.index+=u:(i.active=!1,r++);break}f||(o[l]=r-1,i.canOpen||(i.active=!1),r++)}return s}(n,i,e):null;let c=null,f=null;if(s&&l?l.startIndex<=s.startIndex&&l.endIndex>=s.endIndex?(c=l,f=e.transformersByTag[l.tag]):(c=s,f=o):s?(c=s,f=o):l&&(c=l,f=e.transformersByTag[l.tag]),!c||!f)return null;const a=[n.slice(c.startIndex,c.endIndex),c.tag,c.content];return a.index=c.startIndex,a.input=n,{endIndex:c.endIndex,isCodeSpan:f===o,match:a,startIndex:c.startIndex,transformer:f}}function oe(t,e,n,o,r){if(!re(e,n,o,r))return!1;if("*"===t)return!0;if("_"===t){if(!re(e,n,o,!r))return!0;const t=r?e[n-1]:e[n+o];return void 0!==t&&Z.test(t)}return!0}function re(t,e,n,o){const r=t[e-1],s=t[e+n],[i,l]=o?[s,r]:[r,s];return void 0!==i&&!Y.test(i)&&(!Z.test(i)||(void 0===l||Y.test(l)||Z.test(l)))}function se(t){return e(t)&&!t.hasFormat("code")}function ie(t,e,n){let o=ne(t,e),r=function(t,e){const n=t;let o,r,s,i;for(const t of e){if(!t.replace||!t.importRegExp)continue;const e=n.getTextContent().match(t.importRegExp);if(!e)continue;const l=e.index||0,c=t.getEndIndex?t.getEndIndex(n,e):l+e[0].length;!1!==c&&(void 0===o||void 0===r||l<o&&(c>r||c<=o))&&(o=l,r=c,s=t,i=e)}return void 0===o||void 0===r||void 0===s||void 0===i?null:{endIndex:r,match:i,startIndex:o,transformer:s}}(t,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(t,e,n,o,r){const s=t.getTextContent();let i,l,c;if(r[0]===s?i=t:0===e?[i,l]=t.splitText(n):[c,i,l]=t.splitText(e,n),i.setTextContent(r[2]),o)for(const t of o.format)i.hasFormat(t)||i.toggleFormat(t);return{nodeAfter:l,nodeBefore:c,transformedNode:i}}(t,o.startIndex,o.endIndex,o.transformer,o.match);se(r.nodeAfter)&&ie(r.nodeAfter,e,n),se(r.nodeBefore)&&ie(r.nodeBefore,e,n),se(r.transformedNode)&&ie(r.transformedNode,e,n)}else if(r){const o=function(t,e,n,o,r){let s,i,l;return 0===e?[s,i]=t.splitText(n):[l,s,i]=t.splitText(e,n),o.replace?{nodeAfter:i,nodeBefore:l,transformedNode:o.replace(s,r)||void 0}:null}(t,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;se(o.nodeAfter)&&ie(o.nodeAfter,e,n),se(o.nodeBefore)&&ie(o.nodeBefore,e,n),se(o.transformedNode)&&ie(o.transformedNode,e,n)}const s=nt(t.getTextContent());t.setTextContent(s)}function le(t,e,n,o=!1){const r=V(n),s=function(t){const e={},n={},o=[];for(const r of t){const{tag:t}=r;e[t]=r;const s=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(s),1===t.length?n[t]="`"===t?new RegExp("(^|[^\\\\`])(`)((?:\\\\`|[^`])+?)(`)(?!`)"):new RegExp(`(^|[^\\\\${s}])(${s})((\\\\${s})?.*?[^${s}\\s](\\\\${s})?)(${s})(?![\\\\${s}])`):n[t]=new RegExp(`(^|[^\\\\])(${s})((\\\\${s})?.*?[^\\s](\\\\${s})?)(${s})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`(${o.join("|")})`,"g"),transformersByTag:e}}(r.textFormat),i=t.split("\n"),l=i.length;for(let t=0;t<l;t++){const n=i[t],[l,c]=ce(i,t,r.multilineElement,e);l?t=c:fe(n,e,r.element,s,r.textMatch,o)}const c=e.getChildren();for(const t of c)if(!o&&et(t)&&e.getChildrenSize()>1)t.remove();else if(a(t))for(const e of t.getAllTextNodes())ae(e)}function ce(t,e,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:s,regExpStart:i,replace:l}=r,c=t[e].match(i);if(!c)continue;if(n){const s=n({lines:t,rootNode:o,startLineIndex:e,startMatch:c,transformer:r});if(null===s)continue;if(s)return s}const f="object"==typeof s&&"regExp"in s?s.regExp:s,a=s&&"object"==typeof s&&"optional"in s?s.optional:!s;let u=e;const d=t.length;for(;u<d;){const n=f?t[u].match(f):null;if(!n&&(!a||a&&u<d-1)){u++;continue}if(n&&e===u&&n.index===c.index){u++;continue}const r=[];if(n&&e===u)r.push(t[e].slice(c[0].length,-n[0].length));else for(let o=e;o<=u;o++)if(o===e){const e=t[o].slice(c[0].length);r.push(e)}else if(o===u&&n){const e=t[o].slice(0,-n[0].length);r.push(e)}else r.push(t[o]);if(!1!==l(o,null,c,n,r,!0))return[!0,u];break}}return[!1,e]}function fe(e,n,r,i,l,c){const f=o(e),a=d();a.append(f),n.append(a);for(const{regExp:t,replace:n}of r){const o=e.match(t);if(o&&(f.setTextContent(e.slice(o[0].length)),!1!==n(a,[f],o,!0)))break}if(ie(f,i,l),null!==a.getParent()&&e.length>0){const e=a.getPreviousSibling();if(!c&&(t(e)||q(e)||A(e))){let t=e;if(A(e)){const n=e.getLastDescendant();t=null==n?null:s(n,U)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[$t(t),...a.getChildren()]),a.remove())}}}function ae(t){const e=new Set,n=t.getTextContent();let o=n.indexOf("\t");for(;-1!==o;)e.add(o),e.add(o+1),o=n.indexOf("\t",o+1);t.splitText(...e).forEach(t=>{"\t"===t.getTextContent()&&t.replace(g())})}function ue(t,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",t);for(const t of e)o.append("v",t);throw n.search=o.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function de(t,e,n,o,r){const s=t.getParent();if(!y(s)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(!r&&" "!==i[n-1])return!1;for(const{regExp:s,replace:l}of o){const o=i.match(s),c=r||o&&o[0].endsWith(" ")?n:n-1;if(o&&o[0].length===c){const r=e.getNextSiblings(),[s,i]=e.splitText(n);if(!1!==l(t,i?[i,...r]:r,o,!1))return s.remove(),!0}}return!1}function ge(t,e,n,o,r){const s=t.getParent();if(!y(s)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(!r&&" "!==i[n-1])return!1;for(const{regExpStart:s,replace:l,regExpEnd:c}of o){if(c&&!("optional"in c)||c&&"optional"in c&&!c.optional)continue;const o=i.match(s);if(o){const s=r||o[0].endsWith(" ")?n:n-1;if(o[0].length!==s)continue;const i=e.getNextSiblings(),[c,f]=e.splitText(n);if(!1!==l(t,f?[f,...i]:i,o,null,null,!1))return c.remove(),!0}}return!1}function he(t,e){let n=0;const o=t.getTextContent();for(let t=0;t<e;t++)"`"===o[t]&&n++;return n%2!=0}function pe(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(xe(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function xe(t,e,n,o,r){for(let s=0;s<r;s++)if(t[e+s]!==n[o+s])return!1;return!0}function me(t,n=Kt){const o=V(n),r=o.element.filter(t=>t.triggerOnEnter),s=Q(o.textFormat,({tag:t})=>t[t.length-1]),i=Q(o.textMatch,({trigger:t})=>t),l=new Set([" "]);for(const t of o.textFormat)l.add(t.tag.slice(-1));for(const t of o.textMatch)void 0!==t.trigger&&l.add(t.trigger);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)||ue(173,e.getType())}}const f=(t,n,r)=>!!de(t,n,r,o.element)||(!!ge(t,n,r,o.multilineElement)||(!!function(t,e,n){let o=t.getTextContent();const r=n[o[e-1]];if(null==r)return!1;e<o.length&&(o=o.slice(0,e));for(const e of r){if(!e.replace||!e.regExp)continue;const n=o.match(e.regExp);if(null===n)continue;const r=n.index||0,s=r+n[0].length;let i;return 0===r?[i]=t.splitText(s):[,i]=t.splitText(r,s),i.selectNext(0,0),e.replace(i,n),!0}return!1}(n,r,i)||!!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,f=s-l+1;if(l>1&&!xe(r,f,o,0,l))continue;if(" "===r[f-1])continue;const a=r[s+1];if(!1===n.intraword&&a&&!X.test(a))continue;const u=t;let d=u,g=pe(r,f,o),h=d;for(;g<0&&(h=h.getPreviousSibling())&&!c(h);)if(e(h)){if(h.hasFormat("code"))continue;const t=h.getTextContent();d=h,g=pe(t,t.length,o)}if(g<0)continue;if(d===u&&g+l===f)continue;const p=d.getTextContent();if(g>0&&p[g-1]===i)continue;const x=p[g-1];if(!1===n.intraword&&x&&!X.test(x))continue;if(!n.format.includes("code")&&he(d,g))continue;const m=u.getTextContent(),T=m.slice(0,f)+m.slice(s+1);u.setTextContent(T);const $=d===u?T:p;d.setTextContent($.slice(0,g)+$.slice(g+l));const E=C(),v=S();b(v);const y=s-l*(d===u?2:1)+1;v.anchor.set(d.__key,g,"text"),v.focus.set(u.__key,y,"text");for(const t of n.format)v.formatText(t,w[t]);v.anchor.set(v.focus.key,v.focus.offset,v.focus.type);for(const t of n.format)v.hasFormat(t)&&v.toggleFormat(t);return I(E)&&(v.format=E.format),!0}return!1}(n,r,s)));return h(t.registerUpdateListener(({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:s})=>{if(n.has(p)||n.has(x))return;if(t.isComposing())return;const i=n.has(m),c=r.read(C),a=s.read(C);if(!I(a)||!I(c)||!c.isCollapsed()||c.is(a)&&!i)return;const u=c.anchor.key,d=c.anchor.offset,g=r._nodeMap.get(u);if(e(g)&&o.has(u)&&(i||1===d||!(d>a.anchor.offset+1))){if(i){const t=r.read(()=>g.getTextContent())[d-1];if(!l.has(t))return}t.update(()=>{if(!se(g))return;const t=g.getParent();null===t||M(t)||f(t,g,c.anchor.offset)&&T($)})}}),t.registerCommand(E,t=>{if(null!==t&&t.shiftKey)return!1;const n=C();if(!I(n)||!n.isCollapsed())return!1;const s=n.anchor.offset,i=n.anchor.getNode();if(!e(i)||!se(i))return!1;const l=i.getParent();if(null===l||M(l))return!1;return s===i.getTextContent().length&&(!(!ge(l,i,s,o.multilineElement,!0)&&!de(l,i,s,r,!0))&&(null!==t&&t.preventDefault(),!0))},v))}function Ce(t,e=Kt,n,o=!1,r=!1){const s=o?t:qt(t,r),i=n||f();i.clear(),le(s,i,e,o),null!==C()&&i.selectStart()}function Ie(t,e=Kt,n=!1,o=!1){const r=n?t:qt(t,o),s=new k;return le(r,s,e,n),s.getChildren()}function Te(t=Kt,e,n=!1){const o=function(t,e=!1){const n=V(t),o=[...n.multilineElement,...n.element],r=!e,s=n.textFormat.filter(t=>1===t.format.length).sort((t,e)=>Number(t.format.includes("code"))-Number(e.format.includes("code")));return t=>{const i=[],l=(t||f()).getChildren();for(let t=0;t<l.length;t++){const c=l[t],f=Qt(c,o,s,n.textMatch,e);null!=f&&i.push(r&&t>0&&!et(c)&&!et(l[t-1])?"\n".concat(f):f)}return i.join("\n")}}(t,n);return o(e)}function $e(t=Kt,e,n=!1){if(!e||I(e)&&e.isCollapsed())return"";const o=function(t,e=!1){const n=V(t),o=[...n.multilineElement,...n.element],r=!e,s=n.textFormat.filter(t=>1===t.format.length).sort((t,e)=>Number(t.format.includes("code"))-Number(e.format.includes("code")));return t=>{const i=[],l=f().getChildren();for(let c=0;c<l.length;c++){const f=l[c],{shouldInclude:a,markdown:u}=Gt(f,t,o,s,n.textMatch,e);a&&null!=u&&i.push(r&&c>0&&!et(f)&&!et(l[c-1])?"\n".concat(u):u)}return i.join("\n")}}(t,n);return o(e)}export{Ce as $convertFromMarkdownString,$e as $convertSelectionToMarkdownString,Te as $convertToMarkdownString,Ie as $generateNodesFromMarkdownString,_t as BOLD_ITALIC_STAR,Bt as BOLD_ITALIC_UNDERSCORE,Lt as BOLD_STAR,Rt as BOLD_UNDERSCORE,Nt as CHECK_LIST,wt as CODE,zt as ELEMENT_TRANSFORMERS,St as HEADING,Mt as HIGHLIGHT,Ot as INLINE_CODE,Pt as ITALIC_STAR,At as ITALIC_UNDERSCORE,Wt as LINK,Ut as MULTILINE_ELEMENT_TRANSFORMERS,Ft as ORDERED_LIST,bt as QUOTE,jt as STRIKETHROUGH,Dt as TEXT_FORMAT_TRANSFORMERS,Ht as TEXT_MATCH_TRANSFORMERS,Kt as TRANSFORMERS,kt as UNORDERED_LIST,dt as isTableRowDivider,me as registerMarkdownShortcuts};