@lexical/markdown 0.2.6 → 0.2.7

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.
@@ -10,6 +10,7 @@ import type {
10
10
  LexicalEditor,
11
11
  ElementNode,
12
12
  LexicalNode,
13
+ TextNode,
13
14
  TextFormatType,
14
15
  } from 'lexical';
15
16
 
@@ -33,13 +34,13 @@ export type ElementTransformer = {
33
34
  type: 'element';
34
35
  };
35
36
 
36
- export type TextFormatTransformer = $ReadOnly<{
37
- format: $ReadOnlyArray<TextFormatType>;
37
+ export type TextFormatTransformer = {
38
+ format: Array<TextFormatType>;
38
39
  tag: string;
39
40
  type: 'text-format';
40
- }>;
41
+ };
41
42
 
42
- export type TextMatchTransformer = $ReadOnly<{
43
+ export type TextMatchTransformer = {
43
44
  export: (
44
45
  node: LexicalNode,
45
46
  exportChildren: (node: ElementNode) => string,
@@ -47,10 +48,10 @@ export type TextMatchTransformer = $ReadOnly<{
47
48
  ) => string | null;
48
49
  importRegExp: RegExp;
49
50
  regExp: RegExp;
50
- replace: (node: TextNode, match: RegExp$matchResult) => void;
51
+ replace: (node: TextNode, match: RegExpMatchArray) => void;
51
52
  trigger: string;
52
53
  type: 'text-match';
53
- }>;
54
+ };
54
55
 
55
56
  // TODO:
56
57
  // transformers should be required argument, breaking change
@@ -617,7 +617,7 @@ function isEqualSubString(stringA, aStart, stringB, bStart, length) {
617
617
  return true;
618
618
  }
619
619
 
620
- function registerMarkdownShortcuts(editor, transformers) {
620
+ function registerMarkdownShortcuts(editor, transformers = TRANSFORMERS) {
621
621
  const byType = transformersByType(transformers);
622
622
  const textFormatTransformersIndex = indexBy(byType.textFormat, ({
623
623
  tag
@@ -4,11 +4,11 @@
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 I(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=I(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}
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
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
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=I(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);
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
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
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
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}}
@@ -20,7 +20,7 @@ Y={export:(a,b)=>E.$isListNode(a)?T(a,b,0):null,regExp:/^(\s*)(\d{1,})\.\s/,repl
20
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
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
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){const c=I(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:H,replace:B}of l)if((l=g.match(H))&&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 H=g[q];if(v=d[H])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]===H))){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}}}}}}})}}})};
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}}}}}}})}}})};
package/package.json CHANGED
@@ -8,18 +8,18 @@
8
8
  "markdown"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.2.6",
11
+ "version": "0.2.7",
12
12
  "main": "LexicalMarkdown.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.2.6"
14
+ "lexical": "0.2.7"
15
15
  },
16
16
  "dependencies": {
17
- "@lexical/utils": "0.2.6",
18
- "@lexical/code": "0.2.6",
19
- "@lexical/text": "0.2.6",
20
- "@lexical/rich-text": "0.2.6",
21
- "@lexical/list": "0.2.6",
22
- "@lexical/link": "0.2.6"
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"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",