@lexical/markdown 0.2.8 → 0.3.1

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.
@@ -37,6 +37,7 @@ export type ElementTransformer = {
37
37
  export type TextFormatTransformer = {
38
38
  format: Array<TextFormatType>;
39
39
  tag: string;
40
+ intraword?: boolean;
40
41
  type: 'text-format';
41
42
  };
42
43
 
@@ -18,7 +18,7 @@ var richText = require('@lexical/rich-text');
18
18
  * This source code is licensed under the MIT license found in the
19
19
  * LICENSE file in the root directory of this source tree.
20
20
  *
21
- *
21
+ *
22
22
  */
23
23
  function indexBy(list, callback) {
24
24
  const index = {};
@@ -38,14 +38,12 @@ function indexBy(list, callback) {
38
38
  function transformersByType(transformers) {
39
39
  const byType = indexBy(transformers, t => t.type);
40
40
  return {
41
- // $FlowFixMe
42
41
  element: byType.element,
43
- // $FlowFixMe
44
42
  textFormat: byType['text-format'],
45
- // $FlowFixMe
46
43
  textMatch: byType['text-match']
47
44
  };
48
45
  }
46
+ const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/;
49
47
 
50
48
  /**
51
49
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -53,7 +51,7 @@ function transformersByType(transformers) {
53
51
  * This source code is licensed under the MIT license found in the
54
52
  * LICENSE file in the root directory of this source tree.
55
53
  *
56
- *
54
+ *
57
55
  */
58
56
  function createMarkdownExport(transformers) {
59
57
  const byType = transformersByType(transformers); // Export only uses text formats that are responsible for single format
@@ -117,7 +115,12 @@ function exportChildren(node, textTransformersIndex, textMatchTransformers) {
117
115
  }
118
116
 
119
117
  function exportTextFormat(node, textContent, textTransformers) {
120
- let output = textContent;
118
+ // This function handles the case of a string looking like this: " foo "
119
+ // Where it would be invalid markdown to generate: "** foo **"
120
+ // We instead want to trim the whitespace out, apply formatting, and then
121
+ // bring the whitespace back. So our returned string looks like this: " **foo** "
122
+ const frozenString = textContent.trim();
123
+ let output = frozenString;
121
124
  const applied = new Set();
122
125
 
123
126
  for (const transformer of textTransformers) {
@@ -141,9 +144,10 @@ function exportTextFormat(node, textContent, textTransformers) {
141
144
  output += tag;
142
145
  }
143
146
  }
144
- }
147
+ } // Replace trimmed version of textContent ensuring surrounding whitespace is not modified
145
148
 
146
- return output;
149
+
150
+ return textContent.replace(frozenString, output);
147
151
  } // Get next or previous text sibling a text node, including cases
148
152
  // when it's a child of inline element (e.g. link)
149
153
 
@@ -196,7 +200,7 @@ function hasFormat(node, format) {
196
200
  * This source code is licensed under the MIT license found in the
197
201
  * LICENSE file in the root directory of this source tree.
198
202
  *
199
- *
203
+ *
200
204
  */
201
205
  const CODE_BLOCK_REG_EXP = /^```(\w{1,10})?\s?$/;
202
206
  function createMarkdownImport(transformers) {
@@ -370,16 +374,32 @@ function findOutermostMatch(textContent, textTransformersIndex) {
370
374
  for (const match of openTagsMatch) {
371
375
  // Open tags reg exp might capture leading space so removing it
372
376
  // before using match to find transformer
373
- const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[match.replace(/^\s/, '')];
377
+ const tag = match.replace(/^\s/, '');
378
+ const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[tag];
374
379
 
375
380
  if (fullMatchRegExp == null) {
376
381
  continue;
377
382
  }
378
383
 
379
384
  const fullMatch = textContent.match(fullMatchRegExp);
385
+ const transformer = textTransformersIndex.transformersByTag[tag];
386
+
387
+ if (fullMatch != null && transformer != null) {
388
+ if (transformer.intraword !== false) {
389
+ return fullMatch;
390
+ } // For non-intraword transformers checking if it's within a word
391
+ // or surrounded with space/punctuation/newline
380
392
 
381
- if (fullMatch != null) {
382
- return fullMatch;
393
+
394
+ const {
395
+ index
396
+ } = fullMatch;
397
+ const beforeChar = textContent[index - 1];
398
+ const afterChar = textContent[index + fullMatch[0].length];
399
+
400
+ if ((!beforeChar || PUNCTUATION_OR_SPACE.test(beforeChar)) && (!afterChar || PUNCTUATION_OR_SPACE.test(afterChar))) {
401
+ return fullMatch;
402
+ }
383
403
  }
384
404
  }
385
405
 
@@ -416,9 +436,8 @@ function createTextFormatTransformersIndex(textTransformers) {
416
436
  * This source code is licensed under the MIT license found in the
417
437
  * LICENSE file in the root directory of this source tree.
418
438
  *
419
- *
439
+ *
420
440
  */
421
- const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/;
422
441
 
423
442
  function runElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers) {
424
443
  const grandParentNode = parentNode.getParent();
@@ -497,7 +516,7 @@ function runTextMatchTransformers(anchorNode, anchorOffset, transformersByTrigge
497
516
  return false;
498
517
  }
499
518
 
500
- function runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatTransformers) {
519
+ function runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
501
520
  const textContent = anchorNode.getTextContent();
502
521
  const closeTagEndIndex = anchorOffset - 1;
503
522
  const closeChar = textContent[closeTagEndIndex]; // Quick check if we're possibly at the end of inline markdown style
@@ -654,7 +673,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
654
673
  return;
655
674
  }
656
675
 
657
- runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatTransformersIndex);
676
+ runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
658
677
  };
659
678
 
660
679
  return editor.registerUpdateListener(({
@@ -707,7 +726,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
707
726
  * This source code is licensed under the MIT license found in the
708
727
  * LICENSE file in the root directory of this source tree.
709
728
  *
710
- *
729
+
711
730
  */
712
731
 
713
732
  const replaceWithBlock = createNode => {
@@ -785,7 +804,6 @@ const HEADING = {
785
804
  },
786
805
  regExp: /^(#{1,6})\s/,
787
806
  replace: replaceWithBlock(match => {
788
- // $FlowFixMe[incompatible-cast]
789
807
  const tag = 'h' + match[1].length;
790
808
  return richText.$createHeadingNode(tag);
791
809
  }),
@@ -921,7 +939,7 @@ const LINK = {
921
939
  * This source code is licensed under the MIT license found in the
922
940
  * LICENSE file in the root directory of this source tree.
923
941
  *
924
- *
942
+ *
925
943
  */
926
944
  const ELEMENT_TRANSFORMERS = [HEADING, QUOTE, CODE, UNORDERED_LIST, ORDERED_LIST]; // Order of text format transformers matters:
927
945
  //
@@ -4,24 +4,25 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- var n=require("lexical"),y=require("@lexical/code"),D=require("@lexical/link"),E=require("@lexical/list"),F=require("@lexical/rich-text");function G(a,b){const c={};for(const d of a)a=b(d),c[a]?c[a].push(d):c[a]=[d];return c}function H(a){a=G(a,b=>b.type);return{element:a.element,textFormat:a["text-format"],textMatch:a["text-match"]}}
8
- function aa(a){const b=H(a),c=b.textFormat.filter(d=>1===d.format.length);return()=>{const d=[];var e=n.$getRoot().getChildren();for(const f of e)e=ba(f,b.element,c,b.textMatch),null!=e&&d.push(e);return d.join("\n")}}function ba(a,b,c,d){for(const e of b)if(b=e.export(a,f=>J(f,c,d)),null!=b)return b;return n.$isElementNode(a)?J(a,c,d):null}
9
- function J(a,b,c){const d=[];a=a.getChildren();a:for(const e of a)if(n.$isLineBreakNode(e))d.push("\n");else if(n.$isTextNode(e))d.push(K(e,e.getTextContent(),b));else{for(const f of c)if(a=f.export(e,l=>J(l,b,c),(l,h)=>K(l,h,b)),null!=a){d.push(a);continue a}n.$isElementNode(e)&&d.push(J(e,b,c))}return d.join("")}function K(a,b,c){const d=new Set;for(const f of c){c=f.format[0];const l=f.tag;if(L(a,c)&&!d.has(c)){d.add(c);var e=M(a,!0);L(e,c)||(b=l+b);e=M(a,!1);L(e,c)||(b+=l)}}return b}
10
- function M(a,b){let c=b?a.getPreviousSibling():a.getNextSibling();c||(a=a.getParentOrThrow(),a.isInline()&&(c=b?a.getPreviousSibling():a.getNextSibling()));for(;c;){if(n.$isElementNode(c)){if(!c.isInline())break;a=b?c.getLastDescendant():c.getFirstDescendant();if(n.$isTextNode(a))return a;c=b?c.getPreviousSibling():c.getNextSibling()}if(n.$isTextNode(c))return c;if(!n.$isElementNode(c))break}return null}function L(a,b){return n.$isTextNode(a)&&a.hasFormat(b)}const N=/^```(\w{1,10})?\s?$/;
11
- function ca(a){const b=H(a),c=da(b.textFormat);return d=>{d=d.split("\n");const e=d.length,f=n.$getRoot();f.clear();for(let q=0;q<e;q++){var l=d[q];a:{var h=d,r=q;var u=f;var p=h[r].match(N);if(p)for(var t=r,m=h.length;++t<m;)if(h[t].match(N)){p=y.$createCodeNode(p[1]);h=n.$createTextNode(h.slice(r+1,t).join("\n"));p.append(h);u.append(p);u=[p,t];break a}u=[null,r]}const [k,g]=u;if(null!=k)q=g;else{p=f;m=b.element;u=c;t=b.textMatch;h=n.$createTextNode(l);r=n.$createParagraphNode();r.append(h);p.append(r);
12
- for(const {regExp:v,replace:w}of m)if(p=l.match(v)){h.setTextContent(l.slice(p[0].length));w(r,[h],p,!0);break}O(h,u,t)}}f.selectEnd()}}
13
- function O(a,b,c){const d=a.getTextContent();a:{var e=d.match(b.openTagsRegExp);if(null!=e)for(f of e)if(e=b.fullMatchRegExpByTag[f.replace(/^\s/,"")],null!=e&&(e=d.match(e),null!=e)){var f=e;break a}f=null}if(f){if(f[0]===d)var l=a;else{var h=f.index,r=h+f[0].length;0===h?[l,p]=a.splitText(r):[,l,p]=a.splitText(h,r)}l.setTextContent(f[2]);if(h=b.transformersByTag[f[1]])for(var u of h.format)l.hasFormat(u)||l.toggleFormat(u);l.hasFormat("code")||O(l,b,c);p&&O(p,b,c)}else a:for(b=a;b;){for(h of c)if(l=
14
- b.getTextContent().match(h.importRegExp)){var p=l.index;u=p+l[0].length;0===p?[r,b]=b.splitText(u):[,r,b]=b.splitText(p,u);h.replace(r,l);continue a}break}}function da(a){const b={},c={},d=[];for(const e of a){({tag:a}=e);b[a]=e;const f=a.replace(/(\*|\^)/g,"\\$1");d.push(f);c[a]=new RegExp(`(${f})(?![${f}\\s])(.*?[^${f}\\s])${f}(?!${f})`)}return{fullMatchRegExpByTag:c,openTagsRegExp:new RegExp("("+d.join("|")+")","g"),transformersByTag:b}}const P=/[!-/:-@[-`{-~\s]/;
15
- function Q(a,b,c){const d=c.length;for(;b>=d;b--){const e=b-d;if(R(a,e,c,0,d)&&" "!==a[e+d])return e}return-1}function R(a,b,c,d,e){for(let f=0;f<e;f++)if(a[b+f]!==c[d+f])return!1;return!0}
16
- const S=a=>(b,c,d)=>{d=a(d);d.append(...c);b.replace(d);d.select(0,0)},T=a=>(b,c,d)=>{var e=b.getPreviousSibling();const f=E.$createListItemNode("check"===a?"x"===d[3]:void 0);E.$isListNode(e)&&e.getListType()===a?(e.append(f),b.remove()):(e=E.$createListNode(a,"number"===a?Number(d[2]):void 0),e.append(f),b.replace(e));f.append(...c);f.select(0,0);(b=Math.floor(d[1].length/4))&&f.setIndent(b)},U=(a,b,c)=>{const d=[];var e=a.getChildren();let f=0;for(const h of e)if(E.$isListItemNode(h)){if(1===h.getChildrenSize()&&
17
- (e=h.getFirstChild(),E.$isListNode(e))){d.push(U(e,b,c+1));continue}e=" ".repeat(4*c);var l=a.getListType();l="number"===l?`${a.getStart()+f}. `:"check"===l?`- [${h.getChecked()?"x":" "}] `:"- ";d.push(e+l+b(h));f++}return d.join("\n")},V={export:(a,b)=>{if(!F.$isHeadingNode(a))return null;const c=Number(a.getTag().slice(1));return"#".repeat(c)+" "+b(a)},regExp:/^(#{1,6})\s/,replace:S(a=>F.$createHeadingNode("h"+a[1].length)),type:"element"},W={export:(a,b)=>F.$isQuoteNode(a)?"> "+b(a):null,regExp:/^>\s/,
18
- replace:S(()=>F.$createQuoteNode()),type:"element"},X={export:a=>{if(!y.$isCodeNode(a))return null;const b=a.getTextContent();return"```"+(a.getLanguage()||"")+(b?"\n"+b:"")+"\n```"},regExp:/^```(\w{1,10})?\s/,replace:S(a=>y.$createCodeNode(a?a[1]:void 0)),type:"element"},Y={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)[-*+]\s/,replace:T("bullet"),type:"element"},ea={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:T("check"),type:"element"},
19
- fa={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:T("number"),type:"element"},ha={format:["code"],tag:"`",type:"text-format"},ia={format:["bold","italic"],tag:"***",type:"text-format"},ja={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},ka={format:["bold"],tag:"**",type:"text-format"},la={format:["bold"],intraword:!1,tag:"__",type:"text-format"},ma={format:["strikethrough"],tag:"~~",type:"text-format"},na={format:["italic"],tag:"*",type:"text-format"},
20
- oa={format:["italic"],intraword:!1,tag:"_",type:"text-format"},pa={export:(a,b,c)=>{if(!D.$isLinkNode(a))return null;b=`[${a.getTextContent()}](${a.getURL()})`;const d=a.getFirstChild();return 1===a.getChildrenSize()&&n.$isTextNode(d)?c(d,b):b},importRegExp:/(?:\[([^[]+)\])(?:\(([^(]+)\))/,regExp:/(?:\[([^[]+)\])(?:\(([^(]+)\))$/,replace:(a,b)=>{const [,c,d]=b;b=D.$createLinkNode(d);const e=n.$createTextNode(c);e.setFormat(a.getFormat());b.append(e);a.replace(b)},trigger:")",type:"text-match"},qa=
21
- [V,W,X,Y,fa],ra=[ha,ia,ja,ka,la,na,oa,ma],sa=[pa],Z=[...qa,...ra,...sa];exports.$convertFromMarkdownString=function(a,b=Z){return ca(b)(a)};exports.$convertToMarkdownString=function(a=Z){return aa(a)()};exports.BOLD_ITALIC_STAR=ia;exports.BOLD_ITALIC_UNDERSCORE=ja;exports.BOLD_STAR=ka;exports.BOLD_UNDERSCORE=la;exports.CHECK_LIST=ea;exports.CODE=X;exports.ELEMENT_TRANSFORMERS=qa;exports.HEADING=V;exports.INLINE_CODE=ha;exports.ITALIC_STAR=na;exports.ITALIC_UNDERSCORE=oa;exports.LINK=pa;
22
- exports.ORDERED_LIST=fa;exports.QUOTE=W;exports.STRIKETHROUGH=ma;exports.TEXT_FORMAT_TRANSFORMERS=ra;exports.TEXT_MATCH_TRANSFORMERS=sa;exports.TRANSFORMERS=Z;exports.UNORDERED_LIST=Y;
23
- exports.registerMarkdownShortcuts=function(a,b=Z){const c=H(b),d=G(c.textFormat,({tag:f})=>f[f.length-1]),e=G(c.textMatch,({trigger:f})=>f);return a.registerUpdateListener(({tags:f,dirtyLeaves:l,editorState:h,prevEditorState:r})=>{if(!f.has("historic")){var u=h.read(n.$getSelection);f=r.read(n.$getSelection);if(n.$isRangeSelection(f)&&n.$isRangeSelection(u)&&u.isCollapsed()){r=u.anchor.key;var p=u.anchor.offset,t=h._nodeMap.get(r);n.$isTextNode(t)&&l.has(r)&&(1===p||p===f.anchor.offset+1)&&a.update(()=>
24
- {if(!t.hasFormat("code")){var m=t.getParent();if(null!==m&&!y.$isCodeNode(m)){var q=u.anchor.offset;b:{var k=c.element,g=m.getParent();if(n.$isRootNode(g)&&m.getFirstChild()===t&&(g=t.getTextContent()," "===g[q-1]))for(const {regExp:I,replace:B}of k)if((k=g.match(I))&&k[0].length===q){g=t.getNextSiblings();const [C,z]=t.splitText(q);C.remove();g=z?[z,...g]:g;B(m,g,k,!1);m=!0;break b}m=!1}if(!m){b:{k=t.getTextContent();m=e[k[q-1]];if(null!=m){q<k.length&&(k=k.slice(0,q));for(w of m)if(m=k.match(w.regExp),
25
- null!==m){k=m.index;g=k+m[0].length;var v=void 0;0===k?[v]=t.splitText(g):[,v]=t.splitText(k,g);v.selectNext();w.replace(v,m);var w=!0;break b}}w=!1}if(!w)b:{g=t.getTextContent();--q;const I=g[q];if(w=d[I])for(const B of w){var {tag:A}=B;w=A.length;const C=q-w+1;if(!(1<w&&!R(g,C,A,0,w)||" "===g[C-1])&&(v=g[q+1],!1!==B.intraword||!v||P.test(v))){m=v=t;k=Q(g,C,A);for(var x=m;0>k&&(x=x.getPreviousSibling())&&!n.$isLineBreakNode(x);)n.$isTextNode(x)&&(k=x.getTextContent(),m=x,k=Q(k,k.length,A));if(!(0>
26
- k||m===v&&k+w===C||(A=m.getTextContent(),0<k&&A[k-1]===I||(x=A[k-1],!1===B.intraword&&x&&!P.test(x))))){g=v.getTextContent();g=g.slice(0,C)+g.slice(q+1);v.setTextContent(g);g=m===v?g:A;m.setTextContent(g.slice(0,k)+g.slice(k+w));g=n.$createRangeSelection();n.$setSelection(g);q=q-w*(m===v?2:1)+1;g.anchor.set(m.__key,k,"text");g.focus.set(v.__key,q,"text");for(const z of B.format)g.hasFormat(z)||g.formatText(z);g.anchor.set(g.focus.key,g.focus.offset,g.focus.type);for(const z of B.format)g.hasFormat(z)&&
27
- g.toggleFormat(z);break b}}}}}}}})}}})};
7
+ 'use strict';var n=require("lexical"),y=require("@lexical/code"),D=require("@lexical/link"),E=require("@lexical/list"),F=require("@lexical/rich-text");function G(a,b){let c={};for(let d of a)a=b(d),c[a]?c[a].push(d):c[a]=[d];return c}function H(a){a=G(a,b=>b.type);return{element:a.element,textFormat:a["text-format"],textMatch:a["text-match"]}}let I=/[!-/:-@[-`{-~\s]/;
8
+ function aa(a){let b=H(a),c=b.textFormat.filter(d=>1===d.format.length);return()=>{let d=[];var e=n.$getRoot().getChildren();for(let f of e)e=ba(f,b.element,c,b.textMatch),null!=e&&d.push(e);return d.join("\n")}}function ba(a,b,c,d){for(let e of b)if(b=e.export(a,f=>K(f,c,d)),null!=b)return b;return n.$isElementNode(a)?K(a,c,d):null}
9
+ function K(a,b,c){let d=[];a=a.getChildren();a:for(let e of a)if(n.$isLineBreakNode(e))d.push("\n");else if(n.$isTextNode(e))d.push(L(e,e.getTextContent(),b));else{for(let f of c)if(a=f.export(e,m=>K(m,b,c),(m,h)=>L(m,h,b)),null!=a){d.push(a);continue a}n.$isElementNode(e)&&d.push(K(e,b,c))}return d.join("")}
10
+ function L(a,b,c){let d=b.trim(),e=d,f=new Set;for(let h of c){c=h.format[0];let p=h.tag;if(M(a,c)&&!f.has(c)){f.add(c);var m=N(a,!0);M(m,c)||(e=p+e);m=N(a,!1);M(m,c)||(e+=p)}}return b.replace(d,e)}
11
+ function N(a,b){let c=b?a.getPreviousSibling():a.getNextSibling();c||(a=a.getParentOrThrow(),a.isInline()&&(c=b?a.getPreviousSibling():a.getNextSibling()));for(;c;){if(n.$isElementNode(c)){if(!c.isInline())break;a=b?c.getLastDescendant():c.getFirstDescendant();if(n.$isTextNode(a))return a;c=b?c.getPreviousSibling():c.getNextSibling()}if(n.$isTextNode(c))return c;if(!n.$isElementNode(c))break}return null}function M(a,b){return n.$isTextNode(a)&&a.hasFormat(b)}let O=/^```(\w{1,10})?\s?$/;
12
+ function ca(a){let b=H(a),c=da(b.textFormat);return d=>{d=d.split("\n");let e=d.length,f=n.$getRoot();f.clear();for(let q=0;q<e;q++){var m=d[q];a:{var h=d,p=q;var r=f;var w=h[p].match(O);if(w)for(var t=p,l=h.length;++t<l;)if(h[t].match(O)){w=y.$createCodeNode(w[1]);h=n.$createTextNode(h.slice(p+1,t).join("\n"));w.append(h);r.append(w);r=[w,t];break a}r=[null,p]}let [k,g]=r;if(null!=k)q=g;else{w=f;l=b.element;r=c;t=b.textMatch;h=n.$createTextNode(m);p=n.$createParagraphNode();p.append(h);w.append(p);
13
+ for(let {regExp:u,replace:v}of l)if(w=m.match(u)){h.setTextContent(m.slice(w[0].length));v(p,[h],w,!0);break}P(h,r,t)}}f.selectEnd()}}
14
+ function P(a,b,c){let d=a.getTextContent(),e=ea(d,b);if(e){if(e[0]===d)var f=a;else{var m=e.index,h=m+e[0].length;0===m?[f,r]=a.splitText(h):[,f,r]=a.splitText(m,h)}f.setTextContent(e[2]);if(m=b.transformersByTag[e[1]])for(var p of m.format)f.hasFormat(p)||f.toggleFormat(p);f.hasFormat("code")||P(f,b,c);r&&P(r,b,c)}else a:for(b=a;b;){for(m of c)if(f=b.getTextContent().match(m.importRegExp)){var r=f.index;p=r+f[0].length;0===r?[h,b]=b.splitText(p):[,h,b]=b.splitText(r,p);m.replace(h,f);continue a}break}}
15
+ function ea(a,b){var c=a.match(b.openTagsRegExp);if(null==c)return null;for(let f of c){var d=f.replace(/^\s/,"");c=b.fullMatchRegExpByTag[d];if(null!=c&&(c=a.match(c),d=b.transformersByTag[d],null!=c&&null!=d)){if(!1!==d.intraword)return c;var {index:e}=c;d=a[e-1];e=a[e+c[0].length];if(!(d&&!I.test(d)||e&&!I.test(e)))return c}}return null}
16
+ function da(a){let b={},c={},d=[];for(let e of a){({tag:a}=e);b[a]=e;let f=a.replace(/(\*|\^)/g,"\\$1");d.push(f);c[a]=new RegExp(`(${f})(?![${f}\\s])(.*?[^${f}\\s])${f}(?!${f})`)}return{fullMatchRegExpByTag:c,openTagsRegExp:new RegExp("("+d.join("|")+")","g"),transformersByTag:b}}function Q(a,b,c){let d=c.length;for(;b>=d;b--){let e=b-d;if(R(a,e,c,0,d)&&" "!==a[e+d])return e}return-1}function R(a,b,c,d,e){for(let f=0;f<e;f++)if(a[b+f]!==c[d+f])return!1;return!0}
17
+ let S=a=>(b,c,d)=>{d=a(d);d.append(...c);b.replace(d);d.select(0,0)},T=a=>(b,c,d)=>{var e=b.getPreviousSibling();const f=E.$createListItemNode("check"===a?"x"===d[3]:void 0);E.$isListNode(e)&&e.getListType()===a?(e.append(f),b.remove()):(e=E.$createListNode(a,"number"===a?Number(d[2]):void 0),e.append(f),b.replace(e));f.append(...c);f.select(0,0);(b=Math.floor(d[1].length/4))&&f.setIndent(b)},U=(a,b,c)=>{const d=[];var e=a.getChildren();let f=0;for(const h of e)if(E.$isListItemNode(h)){if(1===h.getChildrenSize()&&
18
+ (e=h.getFirstChild(),E.$isListNode(e))){d.push(U(e,b,c+1));continue}e=" ".repeat(4*c);var m=a.getListType();m="number"===m?`${a.getStart()+f}. `:"check"===m?`- [${h.getChecked()?"x":" "}] `:"- ";d.push(e+m+b(h));f++}return d.join("\n")},V={export:(a,b)=>{if(!F.$isHeadingNode(a))return null;const c=Number(a.getTag().slice(1));return"#".repeat(c)+" "+b(a)},regExp:/^(#{1,6})\s/,replace:S(a=>F.$createHeadingNode("h"+a[1].length)),type:"element"},W={export:(a,b)=>F.$isQuoteNode(a)?"> "+b(a):null,regExp:/^>\s/,
19
+ replace:S(()=>F.$createQuoteNode()),type:"element"},X={export:a=>{if(!y.$isCodeNode(a))return null;const b=a.getTextContent();return"```"+(a.getLanguage()||"")+(b?"\n"+b:"")+"\n```"},regExp:/^```(\w{1,10})?\s/,replace:S(a=>y.$createCodeNode(a?a[1]:void 0)),type:"element"},Y={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)[-*+]\s/,replace:T("bullet"),type:"element"},fa={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:T("check"),type:"element"},
20
+ ha={export:(a,b)=>E.$isListNode(a)?U(a,b,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:T("number"),type:"element"},ia={format:["code"],tag:"`",type:"text-format"},ja={format:["bold","italic"],tag:"***",type:"text-format"},ka={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},la={format:["bold"],tag:"**",type:"text-format"},ma={format:["bold"],intraword:!1,tag:"__",type:"text-format"},na={format:["strikethrough"],tag:"~~",type:"text-format"},oa={format:["italic"],tag:"*",type:"text-format"},
21
+ pa={format:["italic"],intraword:!1,tag:"_",type:"text-format"},qa={export:(a,b,c)=>{if(!D.$isLinkNode(a))return null;b=`[${a.getTextContent()}](${a.getURL()})`;const d=a.getFirstChild();return 1===a.getChildrenSize()&&n.$isTextNode(d)?c(d,b):b},importRegExp:/(?:\[([^[]+)\])(?:\(([^(]+)\))/,regExp:/(?:\[([^[]+)\])(?:\(([^(]+)\))$/,replace:(a,b)=>{const [,c,d]=b;b=D.$createLinkNode(d);const e=n.$createTextNode(c);e.setFormat(a.getFormat());b.append(e);a.replace(b)},trigger:")",type:"text-match"},ra=
22
+ [V,W,X,Y,ha],sa=[ia,ja,ka,la,ma,oa,pa,na],ta=[qa],Z=[...ra,...sa,...ta];exports.$convertFromMarkdownString=function(a,b=Z){return ca(b)(a)};exports.$convertToMarkdownString=function(a=Z){return aa(a)()};exports.BOLD_ITALIC_STAR=ja;exports.BOLD_ITALIC_UNDERSCORE=ka;exports.BOLD_STAR=la;exports.BOLD_UNDERSCORE=ma;exports.CHECK_LIST=fa;exports.CODE=X;exports.ELEMENT_TRANSFORMERS=ra;exports.HEADING=V;exports.INLINE_CODE=ia;exports.ITALIC_STAR=oa;exports.ITALIC_UNDERSCORE=pa;exports.LINK=qa;
23
+ exports.ORDERED_LIST=ha;exports.QUOTE=W;exports.STRIKETHROUGH=na;exports.TEXT_FORMAT_TRANSFORMERS=sa;exports.TEXT_MATCH_TRANSFORMERS=ta;exports.TRANSFORMERS=Z;exports.UNORDERED_LIST=Y;
24
+ exports.registerMarkdownShortcuts=function(a,b=Z){let c=H(b),d=G(c.textFormat,({tag:f})=>f[f.length-1]),e=G(c.textMatch,({trigger:f})=>f);return a.registerUpdateListener(({tags:f,dirtyLeaves:m,editorState:h,prevEditorState:p})=>{if(!f.has("historic")){var r=h.read(n.$getSelection);f=p.read(n.$getSelection);if(n.$isRangeSelection(f)&&n.$isRangeSelection(r)&&r.isCollapsed()){p=r.anchor.key;var w=r.anchor.offset,t=h._nodeMap.get(p);n.$isTextNode(t)&&m.has(p)&&(1===w||w===f.anchor.offset+1)&&a.update(()=>
25
+ {if(!t.hasFormat("code")){var l=t.getParent();if(null!==l&&!y.$isCodeNode(l)){var q=r.anchor.offset;b:{var k=c.element,g=l.getParent();if(n.$isRootNode(g)&&l.getFirstChild()===t&&(g=t.getTextContent()," "===g[q-1]))for(let {regExp:J,replace:B}of k)if((k=g.match(J))&&k[0].length===q){g=t.getNextSiblings();let [C,z]=t.splitText(q);C.remove();g=z?[z,...g]:g;B(l,g,k,!1);l=!0;break b}l=!1}if(!l){b:{k=t.getTextContent();l=e[k[q-1]];if(null!=l){q<k.length&&(k=k.slice(0,q));for(v of l)if(l=k.match(v.regExp),
26
+ null!==l){k=l.index;g=k+l[0].length;var u=void 0;0===k?[u]=t.splitText(g):[,u]=t.splitText(k,g);u.selectNext();v.replace(u,l);var v=!0;break b}}v=!1}if(!v)b:{g=t.getTextContent();--q;let J=g[q];if(v=d[J])for(let B of v){var {tag:A}=B;v=A.length;let C=q-v+1;if(!(1<v&&!R(g,C,A,0,v)||" "===g[C-1])&&(u=g[q+1],!1!==B.intraword||!u||I.test(u))){l=u=t;k=Q(g,C,A);for(var x=l;0>k&&(x=x.getPreviousSibling())&&!n.$isLineBreakNode(x);)n.$isTextNode(x)&&(k=x.getTextContent(),l=x,k=Q(k,k.length,A));if(!(0>k||l===
27
+ u&&k+v===C||(A=l.getTextContent(),0<k&&A[k-1]===J||(x=A[k-1],!1===B.intraword&&x&&!I.test(x))))){g=u.getTextContent();g=g.slice(0,C)+g.slice(q+1);u.setTextContent(g);g=l===u?g:A;l.setTextContent(g.slice(0,k)+g.slice(k+v));g=n.$createRangeSelection();n.$setSelection(g);q=q-v*(l===u?2:1)+1;g.anchor.set(l.__key,k,"text");g.focus.set(u.__key,q,"text");for(let z of B.format)g.hasFormat(z)||g.formatText(z);g.anchor.set(g.focus.key,g.focus.offset,g.focus.type);for(let z of B.format)g.hasFormat(z)&&g.toggleFormat(z);
28
+ break b}}}}}}}})}}})}
package/README.md CHANGED
@@ -33,7 +33,7 @@ It can also be used for initializing editor's state from markdown string. Here's
33
33
  Can use `<LexicalMarkdownShortcutPlugin>` if using React
34
34
  ```jsx
35
35
  import { TRANSFORMERS } from '@lexical/markdown';
36
- import LexicalMarkdownShortcutPlugin from '@lexical/react/LexicalMarkdownShortcutPlugin';
36
+ import {MarkdownShortcutPlugin} from '@lexical/react/LexicalMarkdownShortcutPlugin';
37
37
 
38
38
  <LexicalComposer>
39
39
  <LexicalMarkdownShortcutPlugin transformers={TRANSFORMERS} />
package/package.json CHANGED
@@ -8,18 +8,18 @@
8
8
  "markdown"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.8",
11
+ "version": "0.3.1",
12
12
  "main": "LexicalMarkdown.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.8"
14
+ "lexical": "0.3.1"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.8",
18
- "@lexical/code": "0.2.8",
19
- "@lexical/text": "0.2.8",
20
- "@lexical/rich-text": "0.2.8",
21
- "@lexical/list": "0.2.8",
22
- "@lexical/link": "0.2.8"
17
+ "@lexical/utils": "0.3.1",
18
+ "@lexical/code": "0.3.1",
19
+ "@lexical/text": "0.3.1",
20
+ "@lexical/rich-text": "0.3.1",
21
+ "@lexical/list": "0.3.1",
22
+ "@lexical/link": "0.3.1"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",