@lexical/markdown 0.2.7 → 0.3.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.
@@ -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
 
@@ -177,6 +181,10 @@ function getTextSibling(node, backward) {
177
181
  if (lexical.$isTextNode(sibling)) {
178
182
  return sibling;
179
183
  }
184
+
185
+ if (!lexical.$isElementNode(sibling)) {
186
+ return null;
187
+ }
180
188
  }
181
189
 
182
190
  return null;
@@ -192,7 +200,7 @@ function hasFormat(node, format) {
192
200
  * This source code is licensed under the MIT license found in the
193
201
  * LICENSE file in the root directory of this source tree.
194
202
  *
195
- *
203
+ *
196
204
  */
197
205
  const CODE_BLOCK_REG_EXP = /^```(\w{1,10})?\s?$/;
198
206
  function createMarkdownImport(transformers) {
@@ -366,16 +374,32 @@ function findOutermostMatch(textContent, textTransformersIndex) {
366
374
  for (const match of openTagsMatch) {
367
375
  // Open tags reg exp might capture leading space so removing it
368
376
  // before using match to find transformer
369
- const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[match.replace(/^\s/, '')];
377
+ const tag = match.replace(/^\s/, '');
378
+ const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[tag];
370
379
 
371
380
  if (fullMatchRegExp == null) {
372
381
  continue;
373
382
  }
374
383
 
375
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
376
392
 
377
- if (fullMatch != null) {
378
- 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
+ }
379
403
  }
380
404
  }
381
405
 
@@ -412,7 +436,7 @@ function createTextFormatTransformersIndex(textTransformers) {
412
436
  * This source code is licensed under the MIT license found in the
413
437
  * LICENSE file in the root directory of this source tree.
414
438
  *
415
- *
439
+ *
416
440
  */
417
441
 
418
442
  function runElementTransformers(parentNode, anchorNode, anchorOffset, elementTransformers) {
@@ -492,7 +516,7 @@ function runTextMatchTransformers(anchorNode, anchorOffset, transformersByTrigge
492
516
  return false;
493
517
  }
494
518
 
495
- function runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatTransformers) {
519
+ function runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformers) {
496
520
  const textContent = anchorNode.getTextContent();
497
521
  const closeTagEndIndex = anchorOffset - 1;
498
522
  const closeChar = textContent[closeTagEndIndex]; // Quick check if we're possibly at the end of inline markdown style
@@ -519,6 +543,13 @@ function runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatT
519
543
 
520
544
  if (textContent[closeTagStartIndex - 1] === ' ') {
521
545
  continue;
546
+ } // Some tags can not be used within words, hence should have newline/space/punctuation after it
547
+
548
+
549
+ const afterCloseTagChar = textContent[closeTagEndIndex + 1];
550
+
551
+ if (matcher.intraword === false && afterCloseTagChar && !PUNCTUATION_OR_SPACE.test(afterCloseTagChar)) {
552
+ continue;
522
553
  }
523
554
 
524
555
  const closeNode = anchorNode;
@@ -555,6 +586,13 @@ function runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatT
555
586
 
556
587
  if (openTagStartIndex > 0 && prevOpenNodeText[openTagStartIndex - 1] === closeChar) {
557
588
  continue;
589
+ } // Some tags can not be used within words, hence should have newline/space/punctuation before it
590
+
591
+
592
+ const beforeOpenTagChar = prevOpenNodeText[openTagStartIndex - 1];
593
+
594
+ if (matcher.intraword === false && beforeOpenTagChar && !PUNCTUATION_OR_SPACE.test(beforeOpenTagChar)) {
595
+ continue;
558
596
  } // Clean text from opening and closing tags (starting from closing tag
559
597
  // to prevent any offset shifts if we start from opening one)
560
598
 
@@ -635,7 +673,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
635
673
  return;
636
674
  }
637
675
 
638
- runTextFormatTransformers(editor, anchorNode, anchorOffset, textFormatTransformersIndex);
676
+ runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersIndex);
639
677
  };
640
678
 
641
679
  return editor.registerUpdateListener(({
@@ -688,7 +726,7 @@ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
688
726
  * This source code is licensed under the MIT license found in the
689
727
  * LICENSE file in the root directory of this source tree.
690
728
  *
691
- *
729
+
692
730
  */
693
731
 
694
732
  const replaceWithBlock = createNode => {
@@ -766,7 +804,6 @@ const HEADING = {
766
804
  },
767
805
  regExp: /^(#{1,6})\s/,
768
806
  replace: replaceWithBlock(match => {
769
- // $FlowFixMe[incompatible-cast]
770
807
  const tag = 'h' + match[1].length;
771
808
  return richText.$createHeadingNode(tag);
772
809
  }),
@@ -831,6 +868,7 @@ const BOLD_ITALIC_STAR = {
831
868
  };
832
869
  const BOLD_ITALIC_UNDERSCORE = {
833
870
  format: ['bold', 'italic'],
871
+ intraword: false,
834
872
  tag: '___',
835
873
  type: 'text-format'
836
874
  };
@@ -841,6 +879,7 @@ const BOLD_STAR = {
841
879
  };
842
880
  const BOLD_UNDERSCORE = {
843
881
  format: ['bold'],
882
+ intraword: false,
844
883
  tag: '__',
845
884
  type: 'text-format'
846
885
  };
@@ -856,6 +895,7 @@ const ITALIC_STAR = {
856
895
  };
857
896
  const ITALIC_UNDERSCORE = {
858
897
  format: ['italic'],
898
+ intraword: false,
859
899
  tag: '_',
860
900
  type: 'text-format'
861
901
  }; // Order of text transformers matters:
@@ -899,7 +939,7 @@ const LINK = {
899
939
  * This source code is licensed under the MIT license found in the
900
940
  * LICENSE file in the root directory of this source tree.
901
941
  *
902
- *
942
+ *
903
943
  */
904
944
  const ELEMENT_TRANSFORMERS = [HEADING, QUOTE, CODE, UNORDERED_LIST, ORDERED_LIST]; // Order of text format transformers matters:
905
945
  //
@@ -38,6 +38,7 @@ export type ElementTransformer = {
38
38
  export type TextFormatTransformer = $ReadOnly<{
39
39
  format: $ReadOnlyArray<TextFormatType>,
40
40
  tag: string,
41
+ intraword?: boolean,
41
42
  type: 'text-format',
42
43
  }>;
43
44
 
@@ -4,23 +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,k=>J(k,b,c),(k,h)=>K(k,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 k=f.tag;if(L(a,c)&&!d.has(c)){d.add(c);var e=M(a,!0);L(e,c)||(b=k+b);e=M(a,!1);L(e,c)||(b+=k)}}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}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 k=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 [l,g]=u;if(null!=l)q=g;else{p=f;m=b.element;u=c;t=b.textMatch;h=n.$createTextNode(k);r=n.$createParagraphNode();r.append(h);p.append(r);
12
- for(const {regExp:w,replace:v}of m)if(p=k.match(w)){h.setTextContent(k.slice(p[0].length));v(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 k=a;else{var h=f.index,r=h+f[0].length;0===h?[k,p]=a.splitText(r):[,k,p]=a.splitText(h,r)}k.setTextContent(f[2]);if(h=b.transformersByTag[f[1]])for(var u of h.format)k.hasFormat(u)||k.toggleFormat(u);k.hasFormat("code")||O(k,b,c);p&&O(p,b,c)}else a:for(b=a;b;){for(h of c)if(k=
14
- b.getTextContent().match(h.importRegExp)){var p=k.index;u=p+k[0].length;0===p?[r,b]=b.splitText(u):[,r,b]=b.splitText(p,u);h.replace(r,k);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}}
15
- function P(a,b,c){const d=c.length;for(;b>=d;b--){const e=b-d;if(Q(a,e,c,0,d)&&" "!==a[e+d])return e}return-1}function Q(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 R=a=>(b,c,d)=>{d=a(d);d.append(...c);b.replace(d);d.select(0,0)},S=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)},T=(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(T(e,b,c+1));continue}e=" ".repeat(4*c);var k=a.getListType();k="number"===k?`${a.getStart()+f}. `:"check"===k?`- [${h.getChecked()?"x":" "}] `:"- ";d.push(e+k+b(h));f++}return d.join("\n")},U={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:R(a=>F.$createHeadingNode("h"+a[1].length)),type:"element"},V={export:(a,b)=>F.$isQuoteNode(a)?"> "+b(a):null,regExp:/^>\s/,
18
- replace:R(()=>F.$createQuoteNode()),type:"element"},W={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:R(a=>y.$createCodeNode(a?a[1]:void 0)),type:"element"},X={export:(a,b)=>E.$isListNode(a)?T(a,b,0):null,regExp:/^(\s*)[-*+]\s/,replace:S("bullet"),type:"element"},ea={export:(a,b)=>E.$isListNode(a)?T(a,b,0):null,regExp:/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,replace:S("check"),type:"element"},
19
- Y={export:(a,b)=>E.$isListNode(a)?T(a,b,0):null,regExp:/^(\s*)(\d{1,})\.\s/,replace:S("number"),type:"element"},fa={format:["code"],tag:"`",type:"text-format"},ha={format:["bold","italic"],tag:"***",type:"text-format"},ia={format:["bold","italic"],tag:"___",type:"text-format"},ja={format:["bold"],tag:"**",type:"text-format"},ka={format:["bold"],tag:"__",type:"text-format"},la={format:["strikethrough"],tag:"~~",type:"text-format"},ma={format:["italic"],tag:"*",type:"text-format"},na={format:["italic"],
20
- tag:"_",type:"text-format"},oa={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"},pa=[U,V,W,X,Y],qa=[fa,ha,ia,ja,ka,ma,
21
- na,la],ra=[oa],Z=[...pa,...qa,...ra];exports.$convertFromMarkdownString=function(a,b=Z){return ca(b)(a)};exports.$convertToMarkdownString=function(a=Z){return aa(a)()};exports.BOLD_ITALIC_STAR=ha;exports.BOLD_ITALIC_UNDERSCORE=ia;exports.BOLD_STAR=ja;exports.BOLD_UNDERSCORE=ka;exports.CHECK_LIST=ea;exports.CODE=W;exports.ELEMENT_TRANSFORMERS=pa;exports.HEADING=U;exports.INLINE_CODE=fa;exports.ITALIC_STAR=ma;exports.ITALIC_UNDERSCORE=na;exports.LINK=oa;exports.ORDERED_LIST=Y;exports.QUOTE=V;
22
- exports.STRIKETHROUGH=la;exports.TEXT_FORMAT_TRANSFORMERS=qa;exports.TEXT_MATCH_TRANSFORMERS=ra;exports.TRANSFORMERS=Z;exports.UNORDERED_LIST=X;
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:k,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)&&k.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 l=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 l)if((l=g.match(I))&&l[0].length===q){g=t.getNextSiblings();const [z,x]=t.splitText(q);z.remove();g=x?[x,...g]:g;B(m,g,l,!1);m=!0;break b}m=!1}if(!m){b:{l=t.getTextContent();m=e[l[q-1]];if(null!=m){q<l.length&&(l=l.slice(0,q));for(v of m)if(m=l.match(v.regExp),
25
- null!==m){l=m.index;g=l+m[0].length;var w=void 0;0===l?[w]=t.splitText(g):[,w]=t.splitText(l,g);w.selectNext();v.replace(w,m);var v=!0;break b}}v=!1}if(!v)b:{g=t.getTextContent();--q;const I=g[q];if(v=d[I])for(const B of v){var {tag:A}=B;m=A.length;const z=q-m+1;if(1<m&&!Q(g,z,A,0,m))continue;if(" "===g[z-1])continue;w=v=t;l=P(g,z,A);let x=w;for(;0>l&&(x=x.getPreviousSibling())&&!n.$isLineBreakNode(x);)n.$isTextNode(x)&&(l=x.getTextContent(),w=x,l=P(l,l.length,A));if(!(0>l||w===v&&l+m===z||(A=w.getTextContent(),
26
- 0<l&&A[l-1]===I))){g=v.getTextContent();g=g.slice(0,z)+g.slice(q+1);v.setTextContent(g);g=w===v?g:A;w.setTextContent(g.slice(0,l)+g.slice(l+m));g=n.$createRangeSelection();n.$setSelection(g);q=q-m*(w===v?2:1)+1;g.anchor.set(w.__key,l,"text");g.focus.set(v.__key,q,"text");for(const C of B.format)g.hasFormat(C)||g.formatText(C);g.anchor.set(g.focus.key,g.focus.offset,g.focus.type);for(const C of B.format)g.hasFormat(C)&&g.toggleFormat(C);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/package.json CHANGED
@@ -8,18 +8,18 @@
8
8
  "markdown"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.7",
11
+ "version": "0.3.0",
12
12
  "main": "LexicalMarkdown.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.7"
14
+ "lexical": "0.3.0"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.7",
18
- "@lexical/code": "0.2.7",
19
- "@lexical/text": "0.2.7",
20
- "@lexical/rich-text": "0.2.7",
21
- "@lexical/list": "0.2.7",
22
- "@lexical/link": "0.2.7"
17
+ "@lexical/utils": "0.3.0",
18
+ "@lexical/code": "0.3.0",
19
+ "@lexical/text": "0.3.0",
20
+ "@lexical/rich-text": "0.3.0",
21
+ "@lexical/list": "0.3.0",
22
+ "@lexical/link": "0.3.0"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",