@lexical/markdown 0.35.1-nightly.20250924.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LexicalMarkdown.dev.mjs
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
|
|
9
9
|
import { $isParagraphNode, $isTextNode, $getRoot, $isElementNode, $isDecoratorNode, $isLineBreakNode, $getSelection, $createTextNode, $createParagraphNode, $createLineBreakNode, COLLABORATION_TAG, HISTORIC_TAG, $isRangeSelection, $isRootOrShadowRoot, $createRangeSelection, $setSelection } from 'lexical';
|
|
10
10
|
import { $isListNode, $isListItemNode, ListNode, ListItemNode, $createListItemNode, $createListNode } from '@lexical/list';
|
|
11
|
-
import { $isQuoteNode, HeadingNode,
|
|
11
|
+
import { $isQuoteNode, HeadingNode, QuoteNode, $createHeadingNode, $isHeadingNode, $createQuoteNode } from '@lexical/rich-text';
|
|
12
12
|
import { $findMatchingParent } from '@lexical/utils';
|
|
13
|
-
import { CodeNode, $
|
|
14
|
-
import { LinkNode, $
|
|
13
|
+
import { CodeNode, $createCodeNode, $isCodeNode } from '@lexical/code';
|
|
14
|
+
import { LinkNode, $createLinkNode, $isLinkNode, $isAutoLinkNode } from '@lexical/link';
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
package/LexicalMarkdown.prod.mjs
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{$isParagraphNode as t,$isTextNode as e,$getRoot as n,$isElementNode as o,$isDecoratorNode as r,$isLineBreakNode as i,$getSelection as s,$createTextNode as l,$createParagraphNode as c,$createLineBreakNode as f,COLLABORATION_TAG as a,HISTORIC_TAG as u,$isRangeSelection as g,$isRootOrShadowRoot as p,$createRangeSelection as d,$setSelection as h}from"lexical";import{$isListNode as m,$isListItemNode as x,ListNode as T,ListItemNode as C,$createListItemNode as E,$createListNode as y}from"@lexical/list";import{$isQuoteNode as v,HeadingNode as S,$isHeadingNode as $,QuoteNode as b,$createQuoteNode as F,$createHeadingNode as I}from"@lexical/rich-text";import{$findMatchingParent as N}from"@lexical/utils";import{CodeNode as w,$isCodeNode as k,$createCodeNode as L}from"@lexical/code";import{LinkNode as P,$isLinkNode as R,$isAutoLinkNode as B,$createLinkNode as _}from"@lexical/link";function j(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 M(t){const e=j(t,(t=>t.type));return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const A=/[!-/:-@[-`{-~\s]/,z=/^\s{0,3}$/;function U(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&z.test(o.getTextContent())}function W(t,e,n,i){for(const o of e){if(!o.export)continue;const e=o.export(t,(t=>D(t,n,i)));if(null!=e)return e}return o(t)?D(t,n,i):r(t)?t.getTextContent():null}function D(t,n,s,l,c){const f=[],a=t.getChildren();l||(l=[]),c||(c=[]);t:for(const t of a){for(const e of s){if(!e.export)continue;const o=e.export(t,(t=>D(t,n,s,l,[...c,...l])),((t,e)=>O(t,e,n,l,c)));if(null!=o){f.push(o);continue t}}i(t)?f.push("\n"):e(t)?f.push(O(t,t.getTextContent(),n,l,c)):o(t)?f.push(D(t,n,s,l,c)):r(t)&&f.push(t.getTextContent())}return f.join("")}function O(t,e,n,o,r){let i=0===t.getFormat()?e:function(t){return t.replace(/^\s+|\s+$/g,(t=>[...t].map((t=>"&#"+t.codePointAt(0)+";")).join("")))}(e);t.hasFormat("code")||(i=i.replace(/([*_`~\\])/g,"\\$1"));let s="",l="",c="";const f=q(t,!0),a=q(t,!1),u=new Set;for(const e of n){const n=e.format[0],r=e.tag;G(t,n)&&!u.has(n)&&(u.add(n),G(f,n)&&o.find((t=>t.tag===r))||(o.push({format:n,tag:r}),s+=r))}for(let e=0;e<o.length;e++){const n=G(t,o[e].format),i=G(a,o[e].format);if(n&&i)continue;const s=[...o];for(;s.length>e;){const t=s.pop();r&&t&&r.find((e=>e.tag===t.tag))||(t&&"string"==typeof t.tag&&(n?i||(c+=t.tag):l+=t.tag),o.pop())}break}return i=s+i+c,l+i}function q(t,n){let r=n?t.getPreviousSibling():t.getNextSibling();if(!r){const e=t.getParentOrThrow();e.isInline()&&(r=n?e.getPreviousSibling():e.getNextSibling())}for(;r;){if(o(r)){if(!r.isInline())break;const t=n?r.getLastDescendant():r.getFirstDescendant();if(e(t))return t;r=n?r.getPreviousSibling():r.getNextSibling()}if(e(r))return r;if(!o(r))return null}return null}function G(t,n){return e(t)&&t.hasFormat(n)}function H(t,e){const n=function(t,e){const n=t.match(e.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=e.fullMatchRegExpByTag[n];if(null==r)continue;const i=t.match(r),s=e.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:e=0}=i,n=t[e-1],o=t[e+i[0].length];if((!n||A.test(n))&&(!o||A.test(o)))return i}}return null}(t.getTextContent(),e);if(!n)return null;const o=n.index||0;return{endIndex:o+n[0].length,match:n,startIndex:o,transformer:e.transformersByTag[n[1]]}}function J(t){return e(t)&&!t.hasFormat("code")}function K(t,e,n){let o=H(t,e),r=function(t,e){const n=t;let o,r,i,s;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)&&(o=l,r=c,i=t,s=e)}return void 0===o||void 0===r||void 0===i||void 0===s?null:{endIndex:r,match:s,startIndex:o,transformer:i}}(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 i=t.getTextContent();let s,l,c;if(r[0]===i?s=t:0===e?[s,l]=t.splitText(n):[c,s,l]=t.splitText(e,n),s.setTextContent(r[2]),o)for(const t of o.format)s.hasFormat(t)||s.toggleFormat(t);return{nodeAfter:l,nodeBefore:c,transformedNode:s}}(t,o.startIndex,o.endIndex,o.transformer,o.match);J(r.nodeAfter)&&K(r.nodeAfter,e,n),J(r.nodeBefore)&&K(r.nodeBefore,e,n),J(r.transformedNode)&&K(r.transformedNode,e,n)}else if(r){const o=function(t,e,n,o,r){let i,s,l;return 0===e?[i,s]=t.splitText(n):[l,i,s]=t.splitText(e,n),o.replace?{nodeAfter:s,nodeBefore:l,transformedNode:o.replace(i,r)||void 0}:null}(t,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;J(o.nodeAfter)&&K(o.nodeAfter,e,n),J(o.nodeBefore)&&K(o.nodeBefore,e,n),J(o.transformedNode)&&K(o.transformedNode,e,n)}const i=t.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,((t,e)=>String.fromCodePoint(e)));t.setTextContent(i)}function Q(t,e=!1){const o=M(t),r=function(t){const e={},n={},o=[],r="(?<![\\\\])";for(const r of t){const{tag:t}=r;e[t]=r;const i=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),1===t.length?n[t]=new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`):n[t]=new RegExp(`(?<!\\\\)(${i})((\\\\${i})?.*?[^\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:e}}(o.textFormat);return(t,i)=>{const l=t.split("\n"),c=l.length,f=i||n();f.clear();for(let t=0;t<c;t++){const n=l[t],[i,s]=V(l,t,o.multilineElement,f);i?t=s:X(n,f,o.element,r,o.textMatch,e)}const a=f.getChildren();for(const t of a)!e&&U(t)&&f.getChildrenSize()>1&&t.remove();null!==s()&&f.selectStart()}}function V(t,e,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:i,regExpStart:s,replace:l}=r,c=t[e].match(s);if(!c)continue;if(n){const i=n({lines:t,rootNode:o,startLineIndex:e,startMatch:c,transformer:r});if(null===i)continue;if(i)return i}const f="object"==typeof i&&"regExp"in i?i.regExp:i,a=i&&"object"==typeof i&&"optional"in i?i.optional:!i;let u=e;const g=t.length;for(;u<g;){const n=f?t[u].match(f):null;if(!n&&(!a||a&&u<g-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 X(e,n,o,r,i,s){const a=l(e),u=c();u.append(a),n.append(u);for(const{regExp:t,replace:n}of o){const o=e.match(t);if(o&&(a.setTextContent(e.slice(o[0].length)),!1!==n(u,[a],o,!0)))break}if(K(a,r,i),u.isAttached()&&e.length>0){const e=u.getPreviousSibling();if(!s&&(t(e)||v(e)||m(e))){let t=e;if(m(e)){const n=e.getLastDescendant();t=null==n?null:N(n,x)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[f(),...u.getChildren()]),u.remove())}}}function Y(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 Z=/^(\s*)(\d{1,})\.\s/,tt=/^(\s*)[-*+]\s/,et=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,nt=/^(#{1,6})\s/,ot=/^>\s/,rt=/^[ \t]*```([\w-]+)?/,it=/[ \t]*```$/,st=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,lt=/^(?:\|)(.+)(?:\|)\s?$/,ct=/^(\| ?:?-*:? ?)+\|\s?$/,ft=t=>(e,n,o,r)=>{const i=t(o);i.append(...n),e.replace(i),r||i.select(0,0)};const at=t=>(e,n,o,r)=>{const i=e.getPreviousSibling(),s=e.getNextSibling(),l=E("check"===t?"x"===o[3]:void 0);if(m(s)&&s.getListType()===t){const t=s.getFirstChild();null!==t?t.insertBefore(l):s.append(l),e.remove()}else if(m(i)&&i.getListType()===t)i.append(l),e.remove();else{const n=y(t,"number"===t?Number(o[2]):void 0);n.append(l),e.replace(n)}l.append(...n),r||l.select(0,0);const c=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]);c&&l.setIndent(c)},ut=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(x(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(m(t)){o.push(ut(t,e,n+1));continue}}const r=" ".repeat(4*n),l=t.getListType(),c="number"===l?`${t.getStart()+i}. `:"check"===l?`- [${s.getChecked()?"x":" "}] `:"- ";o.push(r+c+e(s)),i++}return o.join("\n")},gt={dependencies:[S],export:(t,e)=>{if(!$(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:nt,replace:ft((t=>{const e="h"+t[1].length;return I(e)})),type:"element"},pt={dependencies:[b],export:(t,e)=>{if(!v(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:ot,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(v(n))return n.splice(n.getChildrenSize(),0,[f(),...e]),void t.remove()}const r=F();r.append(...e),t.replace(r),o||r.select(0,0)},type:"element"},dt={dependencies:[w],export:t=>{if(!k(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExpEnd:{optional:!0,regExp:it},regExpStart:rt,replace:(t,e,n,o,r,i)=>{let s,c;if(!e&&r){if(1===r.length)o?(s=L(),c=n[1]+r[0]):(s=L(n[1]),c=r[0].startsWith(" ")?r[0].slice(1):r[0]);else{if(s=L(n[1]),0===r[0].trim().length)for(;r.length>0&&!r[0].length;)r.shift();else r[0]=r[0].startsWith(" ")?r[0].slice(1):r[0];for(;r.length>0&&!r[r.length-1].length;)r.pop();c=r.join("\n")}const e=l(c);s.append(e),t.append(s)}else e&&ft((t=>L(t?t[1]:void 0)))(t,e,n,i)},type:"multiline-element"},ht={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:tt,replace:at("bullet"),type:"element"},mt={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:et,replace:at("check"),type:"element"},xt={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:Z,replace:at("number"),type:"element"},Tt={format:["code"],tag:"`",type:"text-format"},Ct={format:["highlight"],tag:"==",type:"text-format"},Et={format:["bold","italic"],tag:"***",type:"text-format"},yt={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},vt={format:["bold"],tag:"**",type:"text-format"},St={format:["bold"],intraword:!1,tag:"__",type:"text-format"},$t={format:["strikethrough"],tag:"~~",type:"text-format"},bt={format:["italic"],tag:"*",type:"text-format"},Ft={format:["italic"],intraword:!1,tag:"_",type:"text-format"},It={dependencies:[P],export:(t,e,n)=>{if(!R(t)||B(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)=>{const[,n,o,r]=e,i=_(o,{title:r}),s=n.split("[").length-1,c=n.split("]").length-1;let f=n,a="";if(s<c)return;if(s>c){const t=n.split("[");a="["+t[0],f=t.slice(1).join("[")}const u=l(f);return u.setFormat(t.getFormat()),i.append(u),t.replace(i),a&&i.insertBefore(l(a)),u},trigger:")",type:"text-match"},Nt=[gt,pt,ht,xt],wt=[dt],kt=[Tt,Et,yt,vt,St,Ct,bt,Ft,$t],Lt=[It],Pt=[...Nt,...wt,...kt,...Lt];function Rt(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(Bt(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function Bt(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function _t(t,n=Pt){const o=M(n),r=j(o.textFormat,(({tag:t})=>t[t.length-1])),l=j(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)||Y(173,e.getType())}}const c=(t,n,c)=>{(function(t,e,n,o){const r=t.getParent();if(!p(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExp:r,replace:s}of o){const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(!1!==s(t,l?[l,...r]:r,o,!1))return i.remove(),!0}}return!1})(t,n,c,o.element)||function(t,e,n,o){const r=t.getParent();if(!p(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExpStart:r,replace:s,regExpEnd:l}of o){if(l&&!("optional"in l)||l&&"optional"in l&&!l.optional)continue;const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(!1!==s(t,l?[l,...r]:r,o,null,null,!1))return i.remove(),!0}}return!1}(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,i=r+n[0].length;let s;return 0===r?[s]=t.splitText(i):[,s]=t.splitText(r,i),s.selectNext(0,0),e.replace(s,n),!0}return!1}(n,c,l)||function(t,n,o){const r=t.getTextContent(),l=n-1,c=r[l],f=o[c];if(!f)return!1;for(const n of f){const{tag:o}=n,f=o.length,a=l-f+1;if(f>1&&!Bt(r,a,o,0,f))continue;if(" "===r[a-1])continue;const u=r[l+1];if(!1===n.intraword&&u&&!A.test(u))continue;const p=t;let m=p,x=Rt(r,a,o),T=m;for(;x<0&&(T=T.getPreviousSibling())&&!i(T);)if(e(T)){if(T.hasFormat("code"))continue;const t=T.getTextContent();m=T,x=Rt(t,t.length,o)}if(x<0)continue;if(m===p&&x+f===a)continue;const C=m.getTextContent();if(x>0&&C[x-1]===c)continue;const E=C[x-1];if(!1===n.intraword&&E&&!A.test(E))continue;const y=p.getTextContent(),v=y.slice(0,a)+y.slice(l+1);p.setTextContent(v);const S=m===p?v:C;m.setTextContent(S.slice(0,x)+S.slice(x+f));const $=s(),b=d();h(b);const F=l-f*(m===p?2:1)+1;b.anchor.set(m.__key,x,"text"),b.focus.set(p.__key,F,"text");for(const t of n.format)b.hasFormat(t)||b.formatText(t);b.anchor.set(b.focus.key,b.focus.offset,b.focus.type);for(const t of n.format)b.hasFormat(t)&&b.toggleFormat(t);return g($)&&(b.format=$.format),!0}}(n,c,r)};return t.registerUpdateListener((({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:i})=>{if(n.has(a)||n.has(u))return;if(t.isComposing())return;const l=r.read(s),f=i.read(s);if(!g(f)||!g(l)||!l.isCollapsed()||l.is(f))return;const p=l.anchor.key,d=l.anchor.offset,h=r._nodeMap.get(p);!e(h)||!o.has(p)||1!==d&&d>f.anchor.offset+1||t.update((()=>{if(!J(h))return;const t=h.getParent();null===t||k(t)||c(t,h,l.anchor.offset)}))}))}function jt(t,e=Pt,n,o=!1,r=!1){const i=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 i=n[t],s=r[r.length-1];st.test(i)?r.push(i):rt.test(i)||it.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||nt.test(s)||nt.test(i)||ot.test(i)||Z.test(i)||tt.test(i)||et.test(i)||lt.test(i)||ct.test(i)||!e?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(t,r);return Q(e,o)(i,n)}function Mt(t=Pt,e,o=!1){const r=function(t,e=!1){const o=M(t),r=[...o.multilineElement,...o.element],i=!e,s=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=W(n,r,s,o.textMatch);null!=c&&e.push(i&&t>0&&!U(n)&&!U(l[t-1])?"\n".concat(c):c)}return e.join("\n")}}(t,o);return r(e)}export{jt as $convertFromMarkdownString,Mt as $convertToMarkdownString,Et as BOLD_ITALIC_STAR,yt as BOLD_ITALIC_UNDERSCORE,vt as BOLD_STAR,St as BOLD_UNDERSCORE,mt as CHECK_LIST,dt as CODE,Nt as ELEMENT_TRANSFORMERS,gt as HEADING,Ct as HIGHLIGHT,Tt as INLINE_CODE,bt as ITALIC_STAR,Ft as ITALIC_UNDERSCORE,It as LINK,wt as MULTILINE_ELEMENT_TRANSFORMERS,xt as ORDERED_LIST,pt as QUOTE,$t as STRIKETHROUGH,kt as TEXT_FORMAT_TRANSFORMERS,Lt as TEXT_MATCH_TRANSFORMERS,Pt as TRANSFORMERS,ht as UNORDERED_LIST,_t as registerMarkdownShortcuts};
|
|
9
|
+
import{$isParagraphNode as t,$isTextNode as e,$getRoot as n,$isElementNode as o,$isDecoratorNode as r,$isLineBreakNode as i,$getSelection as s,$createTextNode as l,$createParagraphNode as c,$createLineBreakNode as f,COLLABORATION_TAG as a,HISTORIC_TAG as u,$isRangeSelection as g,$isRootOrShadowRoot as p,$createRangeSelection as d,$setSelection as h}from"lexical";import{$isListNode as m,$isListItemNode as x,ListNode as T,ListItemNode as C,$createListItemNode as E,$createListNode as y}from"@lexical/list";import{$isQuoteNode as v,HeadingNode as S,QuoteNode as $,$createHeadingNode as b,$isHeadingNode as F,$createQuoteNode as I}from"@lexical/rich-text";import{$findMatchingParent as N}from"@lexical/utils";import{CodeNode as w,$createCodeNode as k,$isCodeNode as L}from"@lexical/code";import{LinkNode as P,$createLinkNode as R,$isLinkNode as B,$isAutoLinkNode as _}from"@lexical/link";function j(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 M(t){const e=j(t,(t=>t.type));return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const A=/[!-/:-@[-`{-~\s]/,z=/^\s{0,3}$/;function U(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&z.test(o.getTextContent())}function W(t,e,n,i){for(const o of e){if(!o.export)continue;const e=o.export(t,(t=>D(t,n,i)));if(null!=e)return e}return o(t)?D(t,n,i):r(t)?t.getTextContent():null}function D(t,n,s,l,c){const f=[],a=t.getChildren();l||(l=[]),c||(c=[]);t:for(const t of a){for(const e of s){if(!e.export)continue;const o=e.export(t,(t=>D(t,n,s,l,[...c,...l])),((t,e)=>O(t,e,n,l,c)));if(null!=o){f.push(o);continue t}}i(t)?f.push("\n"):e(t)?f.push(O(t,t.getTextContent(),n,l,c)):o(t)?f.push(D(t,n,s,l,c)):r(t)&&f.push(t.getTextContent())}return f.join("")}function O(t,e,n,o,r){let i=0===t.getFormat()?e:function(t){return t.replace(/^\s+|\s+$/g,(t=>[...t].map((t=>"&#"+t.codePointAt(0)+";")).join("")))}(e);t.hasFormat("code")||(i=i.replace(/([*_`~\\])/g,"\\$1"));let s="",l="",c="";const f=q(t,!0),a=q(t,!1),u=new Set;for(const e of n){const n=e.format[0],r=e.tag;G(t,n)&&!u.has(n)&&(u.add(n),G(f,n)&&o.find((t=>t.tag===r))||(o.push({format:n,tag:r}),s+=r))}for(let e=0;e<o.length;e++){const n=G(t,o[e].format),i=G(a,o[e].format);if(n&&i)continue;const s=[...o];for(;s.length>e;){const t=s.pop();r&&t&&r.find((e=>e.tag===t.tag))||(t&&"string"==typeof t.tag&&(n?i||(c+=t.tag):l+=t.tag),o.pop())}break}return i=s+i+c,l+i}function q(t,n){let r=n?t.getPreviousSibling():t.getNextSibling();if(!r){const e=t.getParentOrThrow();e.isInline()&&(r=n?e.getPreviousSibling():e.getNextSibling())}for(;r;){if(o(r)){if(!r.isInline())break;const t=n?r.getLastDescendant():r.getFirstDescendant();if(e(t))return t;r=n?r.getPreviousSibling():r.getNextSibling()}if(e(r))return r;if(!o(r))return null}return null}function G(t,n){return e(t)&&t.hasFormat(n)}function H(t,e){const n=function(t,e){const n=t.match(e.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=e.fullMatchRegExpByTag[n];if(null==r)continue;const i=t.match(r),s=e.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:e=0}=i,n=t[e-1],o=t[e+i[0].length];if((!n||A.test(n))&&(!o||A.test(o)))return i}}return null}(t.getTextContent(),e);if(!n)return null;const o=n.index||0;return{endIndex:o+n[0].length,match:n,startIndex:o,transformer:e.transformersByTag[n[1]]}}function J(t){return e(t)&&!t.hasFormat("code")}function K(t,e,n){let o=H(t,e),r=function(t,e){const n=t;let o,r,i,s;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)&&(o=l,r=c,i=t,s=e)}return void 0===o||void 0===r||void 0===i||void 0===s?null:{endIndex:r,match:s,startIndex:o,transformer:i}}(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 i=t.getTextContent();let s,l,c;if(r[0]===i?s=t:0===e?[s,l]=t.splitText(n):[c,s,l]=t.splitText(e,n),s.setTextContent(r[2]),o)for(const t of o.format)s.hasFormat(t)||s.toggleFormat(t);return{nodeAfter:l,nodeBefore:c,transformedNode:s}}(t,o.startIndex,o.endIndex,o.transformer,o.match);J(r.nodeAfter)&&K(r.nodeAfter,e,n),J(r.nodeBefore)&&K(r.nodeBefore,e,n),J(r.transformedNode)&&K(r.transformedNode,e,n)}else if(r){const o=function(t,e,n,o,r){let i,s,l;return 0===e?[i,s]=t.splitText(n):[l,i,s]=t.splitText(e,n),o.replace?{nodeAfter:s,nodeBefore:l,transformedNode:o.replace(i,r)||void 0}:null}(t,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;J(o.nodeAfter)&&K(o.nodeAfter,e,n),J(o.nodeBefore)&&K(o.nodeBefore,e,n),J(o.transformedNode)&&K(o.transformedNode,e,n)}const i=t.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,((t,e)=>String.fromCodePoint(e)));t.setTextContent(i)}function Q(t,e=!1){const o=M(t),r=function(t){const e={},n={},o=[],r="(?<![\\\\])";for(const r of t){const{tag:t}=r;e[t]=r;const i=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),1===t.length?n[t]=new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`):n[t]=new RegExp(`(?<!\\\\)(${i})((\\\\${i})?.*?[^\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:e}}(o.textFormat);return(t,i)=>{const l=t.split("\n"),c=l.length,f=i||n();f.clear();for(let t=0;t<c;t++){const n=l[t],[i,s]=V(l,t,o.multilineElement,f);i?t=s:X(n,f,o.element,r,o.textMatch,e)}const a=f.getChildren();for(const t of a)!e&&U(t)&&f.getChildrenSize()>1&&t.remove();null!==s()&&f.selectStart()}}function V(t,e,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:i,regExpStart:s,replace:l}=r,c=t[e].match(s);if(!c)continue;if(n){const i=n({lines:t,rootNode:o,startLineIndex:e,startMatch:c,transformer:r});if(null===i)continue;if(i)return i}const f="object"==typeof i&&"regExp"in i?i.regExp:i,a=i&&"object"==typeof i&&"optional"in i?i.optional:!i;let u=e;const g=t.length;for(;u<g;){const n=f?t[u].match(f):null;if(!n&&(!a||a&&u<g-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 X(e,n,o,r,i,s){const a=l(e),u=c();u.append(a),n.append(u);for(const{regExp:t,replace:n}of o){const o=e.match(t);if(o&&(a.setTextContent(e.slice(o[0].length)),!1!==n(u,[a],o,!0)))break}if(K(a,r,i),u.isAttached()&&e.length>0){const e=u.getPreviousSibling();if(!s&&(t(e)||v(e)||m(e))){let t=e;if(m(e)){const n=e.getLastDescendant();t=null==n?null:N(n,x)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[f(),...u.getChildren()]),u.remove())}}}function Y(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 Z=/^(\s*)(\d{1,})\.\s/,tt=/^(\s*)[-*+]\s/,et=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,nt=/^(#{1,6})\s/,ot=/^>\s/,rt=/^[ \t]*```([\w-]+)?/,it=/[ \t]*```$/,st=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,lt=/^(?:\|)(.+)(?:\|)\s?$/,ct=/^(\| ?:?-*:? ?)+\|\s?$/,ft=t=>(e,n,o,r)=>{const i=t(o);i.append(...n),e.replace(i),r||i.select(0,0)};const at=t=>(e,n,o,r)=>{const i=e.getPreviousSibling(),s=e.getNextSibling(),l=E("check"===t?"x"===o[3]:void 0);if(m(s)&&s.getListType()===t){const t=s.getFirstChild();null!==t?t.insertBefore(l):s.append(l),e.remove()}else if(m(i)&&i.getListType()===t)i.append(l),e.remove();else{const n=y(t,"number"===t?Number(o[2]):void 0);n.append(l),e.replace(n)}l.append(...n),r||l.select(0,0);const c=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]);c&&l.setIndent(c)},ut=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(x(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(m(t)){o.push(ut(t,e,n+1));continue}}const r=" ".repeat(4*n),l=t.getListType(),c="number"===l?`${t.getStart()+i}. `:"check"===l?`- [${s.getChecked()?"x":" "}] `:"- ";o.push(r+c+e(s)),i++}return o.join("\n")},gt={dependencies:[S],export:(t,e)=>{if(!F(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:nt,replace:ft((t=>{const e="h"+t[1].length;return b(e)})),type:"element"},pt={dependencies:[$],export:(t,e)=>{if(!v(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:ot,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(v(n))return n.splice(n.getChildrenSize(),0,[f(),...e]),void t.remove()}const r=I();r.append(...e),t.replace(r),o||r.select(0,0)},type:"element"},dt={dependencies:[w],export:t=>{if(!L(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExpEnd:{optional:!0,regExp:it},regExpStart:rt,replace:(t,e,n,o,r,i)=>{let s,c;if(!e&&r){if(1===r.length)o?(s=k(),c=n[1]+r[0]):(s=k(n[1]),c=r[0].startsWith(" ")?r[0].slice(1):r[0]);else{if(s=k(n[1]),0===r[0].trim().length)for(;r.length>0&&!r[0].length;)r.shift();else r[0]=r[0].startsWith(" ")?r[0].slice(1):r[0];for(;r.length>0&&!r[r.length-1].length;)r.pop();c=r.join("\n")}const e=l(c);s.append(e),t.append(s)}else e&&ft((t=>k(t?t[1]:void 0)))(t,e,n,i)},type:"multiline-element"},ht={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:tt,replace:at("bullet"),type:"element"},mt={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:et,replace:at("check"),type:"element"},xt={dependencies:[T,C],export:(t,e)=>m(t)?ut(t,e,0):null,regExp:Z,replace:at("number"),type:"element"},Tt={format:["code"],tag:"`",type:"text-format"},Ct={format:["highlight"],tag:"==",type:"text-format"},Et={format:["bold","italic"],tag:"***",type:"text-format"},yt={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},vt={format:["bold"],tag:"**",type:"text-format"},St={format:["bold"],intraword:!1,tag:"__",type:"text-format"},$t={format:["strikethrough"],tag:"~~",type:"text-format"},bt={format:["italic"],tag:"*",type:"text-format"},Ft={format:["italic"],intraword:!1,tag:"_",type:"text-format"},It={dependencies:[P],export:(t,e,n)=>{if(!B(t)||_(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)=>{const[,n,o,r]=e,i=R(o,{title:r}),s=n.split("[").length-1,c=n.split("]").length-1;let f=n,a="";if(s<c)return;if(s>c){const t=n.split("[");a="["+t[0],f=t.slice(1).join("[")}const u=l(f);return u.setFormat(t.getFormat()),i.append(u),t.replace(i),a&&i.insertBefore(l(a)),u},trigger:")",type:"text-match"},Nt=[gt,pt,ht,xt],wt=[dt],kt=[Tt,Et,yt,vt,St,Ct,bt,Ft,$t],Lt=[It],Pt=[...Nt,...wt,...kt,...Lt];function Rt(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(Bt(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function Bt(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function _t(t,n=Pt){const o=M(n),r=j(o.textFormat,(({tag:t})=>t[t.length-1])),l=j(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)||Y(173,e.getType())}}const c=(t,n,c)=>{(function(t,e,n,o){const r=t.getParent();if(!p(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExp:r,replace:s}of o){const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(!1!==s(t,l?[l,...r]:r,o,!1))return i.remove(),!0}}return!1})(t,n,c,o.element)||function(t,e,n,o){const r=t.getParent();if(!p(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExpStart:r,replace:s,regExpEnd:l}of o){if(l&&!("optional"in l)||l&&"optional"in l&&!l.optional)continue;const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(!1!==s(t,l?[l,...r]:r,o,null,null,!1))return i.remove(),!0}}return!1}(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,i=r+n[0].length;let s;return 0===r?[s]=t.splitText(i):[,s]=t.splitText(r,i),s.selectNext(0,0),e.replace(s,n),!0}return!1}(n,c,l)||function(t,n,o){const r=t.getTextContent(),l=n-1,c=r[l],f=o[c];if(!f)return!1;for(const n of f){const{tag:o}=n,f=o.length,a=l-f+1;if(f>1&&!Bt(r,a,o,0,f))continue;if(" "===r[a-1])continue;const u=r[l+1];if(!1===n.intraword&&u&&!A.test(u))continue;const p=t;let m=p,x=Rt(r,a,o),T=m;for(;x<0&&(T=T.getPreviousSibling())&&!i(T);)if(e(T)){if(T.hasFormat("code"))continue;const t=T.getTextContent();m=T,x=Rt(t,t.length,o)}if(x<0)continue;if(m===p&&x+f===a)continue;const C=m.getTextContent();if(x>0&&C[x-1]===c)continue;const E=C[x-1];if(!1===n.intraword&&E&&!A.test(E))continue;const y=p.getTextContent(),v=y.slice(0,a)+y.slice(l+1);p.setTextContent(v);const S=m===p?v:C;m.setTextContent(S.slice(0,x)+S.slice(x+f));const $=s(),b=d();h(b);const F=l-f*(m===p?2:1)+1;b.anchor.set(m.__key,x,"text"),b.focus.set(p.__key,F,"text");for(const t of n.format)b.hasFormat(t)||b.formatText(t);b.anchor.set(b.focus.key,b.focus.offset,b.focus.type);for(const t of n.format)b.hasFormat(t)&&b.toggleFormat(t);return g($)&&(b.format=$.format),!0}}(n,c,r)};return t.registerUpdateListener((({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:i})=>{if(n.has(a)||n.has(u))return;if(t.isComposing())return;const l=r.read(s),f=i.read(s);if(!g(f)||!g(l)||!l.isCollapsed()||l.is(f))return;const p=l.anchor.key,d=l.anchor.offset,h=r._nodeMap.get(p);!e(h)||!o.has(p)||1!==d&&d>f.anchor.offset+1||t.update((()=>{if(!J(h))return;const t=h.getParent();null===t||L(t)||c(t,h,l.anchor.offset)}))}))}function jt(t,e=Pt,n,o=!1,r=!1){const i=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 i=n[t],s=r[r.length-1];st.test(i)?r.push(i):rt.test(i)||it.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||nt.test(s)||nt.test(i)||ot.test(i)||Z.test(i)||tt.test(i)||et.test(i)||lt.test(i)||ct.test(i)||!e?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(t,r);return Q(e,o)(i,n)}function Mt(t=Pt,e,o=!1){const r=function(t,e=!1){const o=M(t),r=[...o.multilineElement,...o.element],i=!e,s=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=W(n,r,s,o.textMatch);null!=c&&e.push(i&&t>0&&!U(n)&&!U(l[t-1])?"\n".concat(c):c)}return e.join("\n")}}(t,o);return r(e)}export{jt as $convertFromMarkdownString,Mt as $convertToMarkdownString,Et as BOLD_ITALIC_STAR,yt as BOLD_ITALIC_UNDERSCORE,vt as BOLD_STAR,St as BOLD_UNDERSCORE,mt as CHECK_LIST,dt as CODE,Nt as ELEMENT_TRANSFORMERS,gt as HEADING,Ct as HIGHLIGHT,Tt as INLINE_CODE,bt as ITALIC_STAR,Ft as ITALIC_UNDERSCORE,It as LINK,wt as MULTILINE_ELEMENT_TRANSFORMERS,xt as ORDERED_LIST,pt as QUOTE,$t as STRIKETHROUGH,kt as TEXT_FORMAT_TRANSFORMERS,Lt as TEXT_MATCH_TRANSFORMERS,Pt as TRANSFORMERS,ht as UNORDERED_LIST,_t as registerMarkdownShortcuts};
|
package/package.json
CHANGED
|
@@ -8,17 +8,17 @@
|
|
|
8
8
|
"markdown"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.36.0",
|
|
12
12
|
"main": "LexicalMarkdown.js",
|
|
13
13
|
"types": "index.d.ts",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@lexical/code": "0.
|
|
16
|
-
"@lexical/link": "0.
|
|
17
|
-
"@lexical/list": "0.
|
|
18
|
-
"@lexical/rich-text": "0.
|
|
19
|
-
"@lexical/text": "0.
|
|
20
|
-
"@lexical/utils": "0.
|
|
21
|
-
"lexical": "0.
|
|
15
|
+
"@lexical/code": "0.36.0",
|
|
16
|
+
"@lexical/link": "0.36.0",
|
|
17
|
+
"@lexical/list": "0.36.0",
|
|
18
|
+
"@lexical/rich-text": "0.36.0",
|
|
19
|
+
"@lexical/text": "0.36.0",
|
|
20
|
+
"@lexical/utils": "0.36.0",
|
|
21
|
+
"lexical": "0.36.0"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|