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