@lexical/markdown 0.36.3-nightly.20251003.0 → 0.36.3-nightly.20251006.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.js
CHANGED
|
@@ -741,7 +741,7 @@ function formatDevErrorMessage(message) {
|
|
|
741
741
|
|
|
742
742
|
const ORDERED_LIST_REGEX = /^(\s*)(\d{1,})\.\s/;
|
|
743
743
|
const UNORDERED_LIST_REGEX = /^(\s*)[-*+]\s/;
|
|
744
|
-
const CHECK_LIST_REGEX = /^(\s*)(
|
|
744
|
+
const CHECK_LIST_REGEX = /^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i;
|
|
745
745
|
const HEADING_REGEX = /^(#{1,6})\s/;
|
|
746
746
|
const QUOTE_REGEX = /^>\s/;
|
|
747
747
|
const CODE_START_REGEX = /^[ \t]*```([\w-]+)?/;
|
|
@@ -749,6 +749,9 @@ const CODE_END_REGEX = /[ \t]*```$/;
|
|
|
749
749
|
const CODE_SINGLE_LINE_REGEX = /^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/;
|
|
750
750
|
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
|
|
751
751
|
const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/;
|
|
752
|
+
const listMarkerState = lexical.createState('mdListMarker', {
|
|
753
|
+
parse: v => typeof v === 'string' ? v : '-'
|
|
754
|
+
});
|
|
752
755
|
const createBlockNode = createNode => {
|
|
753
756
|
return (parentNode, children, match, isImport) => {
|
|
754
757
|
const node = createNode(match);
|
|
@@ -780,7 +783,11 @@ const listReplace = listType => {
|
|
|
780
783
|
const previousNode = parentNode.getPreviousSibling();
|
|
781
784
|
const nextNode = parentNode.getNextSibling();
|
|
782
785
|
const listItem = list.$createListItemNode(listType === 'check' ? match[3] === 'x' : undefined);
|
|
786
|
+
const listMarker = listType === 'bullet' || listType === 'check' ? match[0].trim()[0] : undefined;
|
|
783
787
|
if (list.$isListNode(nextNode) && nextNode.getListType() === listType) {
|
|
788
|
+
if (listMarker) {
|
|
789
|
+
lexical.$setState(nextNode, listMarkerState, listMarker);
|
|
790
|
+
}
|
|
784
791
|
const firstChild = nextNode.getFirstChild();
|
|
785
792
|
if (firstChild !== null) {
|
|
786
793
|
firstChild.insertBefore(listItem);
|
|
@@ -790,10 +797,16 @@ const listReplace = listType => {
|
|
|
790
797
|
}
|
|
791
798
|
parentNode.remove();
|
|
792
799
|
} else if (list.$isListNode(previousNode) && previousNode.getListType() === listType) {
|
|
800
|
+
if (listMarker) {
|
|
801
|
+
lexical.$setState(previousNode, listMarkerState, listMarker);
|
|
802
|
+
}
|
|
793
803
|
previousNode.append(listItem);
|
|
794
804
|
parentNode.remove();
|
|
795
805
|
} else {
|
|
796
806
|
const list$1 = list.$createListNode(listType, listType === 'number' ? Number(match[2]) : undefined);
|
|
807
|
+
if (listMarker) {
|
|
808
|
+
lexical.$setState(list$1, listMarkerState, listMarker);
|
|
809
|
+
}
|
|
797
810
|
list$1.append(listItem);
|
|
798
811
|
parentNode.replace(list$1);
|
|
799
812
|
}
|
|
@@ -807,7 +820,7 @@ const listReplace = listType => {
|
|
|
807
820
|
}
|
|
808
821
|
};
|
|
809
822
|
};
|
|
810
|
-
const listExport = (listNode, exportChildren, depth) => {
|
|
823
|
+
const $listExport = (listNode, exportChildren, depth) => {
|
|
811
824
|
const output = [];
|
|
812
825
|
const children = listNode.getChildren();
|
|
813
826
|
let index = 0;
|
|
@@ -816,13 +829,14 @@ const listExport = (listNode, exportChildren, depth) => {
|
|
|
816
829
|
if (listItemNode.getChildrenSize() === 1) {
|
|
817
830
|
const firstChild = listItemNode.getFirstChild();
|
|
818
831
|
if (list.$isListNode(firstChild)) {
|
|
819
|
-
output.push(listExport(firstChild, exportChildren, depth + 1));
|
|
832
|
+
output.push($listExport(firstChild, exportChildren, depth + 1));
|
|
820
833
|
continue;
|
|
821
834
|
}
|
|
822
835
|
}
|
|
823
836
|
const indent = ' '.repeat(depth * LIST_INDENT_SIZE);
|
|
824
837
|
const listType = listNode.getListType();
|
|
825
|
-
const
|
|
838
|
+
const listMarker = lexical.$getState(listNode, listMarkerState);
|
|
839
|
+
const prefix = listType === 'number' ? `${listNode.getStart() + index}. ` : listType === 'check' ? `${listMarker} [${listItemNode.getChecked() ? 'x' : ' '}] ` : listMarker + ' ';
|
|
826
840
|
output.push(indent + prefix + exportChildren(listItemNode));
|
|
827
841
|
index++;
|
|
828
842
|
}
|
|
@@ -939,7 +953,7 @@ const CODE = {
|
|
|
939
953
|
const UNORDERED_LIST = {
|
|
940
954
|
dependencies: [list.ListNode, list.ListItemNode],
|
|
941
955
|
export: (node, exportChildren) => {
|
|
942
|
-
return list.$isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
956
|
+
return list.$isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
943
957
|
},
|
|
944
958
|
regExp: UNORDERED_LIST_REGEX,
|
|
945
959
|
replace: listReplace('bullet'),
|
|
@@ -948,7 +962,7 @@ const UNORDERED_LIST = {
|
|
|
948
962
|
const CHECK_LIST = {
|
|
949
963
|
dependencies: [list.ListNode, list.ListItemNode],
|
|
950
964
|
export: (node, exportChildren) => {
|
|
951
|
-
return list.$isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
965
|
+
return list.$isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
952
966
|
},
|
|
953
967
|
regExp: CHECK_LIST_REGEX,
|
|
954
968
|
replace: listReplace('check'),
|
|
@@ -957,7 +971,7 @@ const CHECK_LIST = {
|
|
|
957
971
|
const ORDERED_LIST = {
|
|
958
972
|
dependencies: [list.ListNode, list.ListItemNode],
|
|
959
973
|
export: (node, exportChildren) => {
|
|
960
|
-
return list.$isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
974
|
+
return list.$isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
961
975
|
},
|
|
962
976
|
regExp: ORDERED_LIST_REGEX,
|
|
963
977
|
replace: listReplace('number'),
|
package/LexicalMarkdown.dev.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { $isParagraphNode, $isTextNode, $getRoot, $isElementNode, $isDecoratorNode, $isLineBreakNode, $getSelection, $createTextNode, $createParagraphNode, $createLineBreakNode, COLLABORATION_TAG, HISTORIC_TAG, $isRangeSelection, $isRootOrShadowRoot, $createRangeSelection, $setSelection } from 'lexical';
|
|
9
|
+
import { $isParagraphNode, $isTextNode, $getRoot, $isElementNode, $isDecoratorNode, $isLineBreakNode, $getSelection, $createTextNode, $createParagraphNode, $createLineBreakNode, createState, $setState, $getState, COLLABORATION_TAG, HISTORIC_TAG, $isRangeSelection, $isRootOrShadowRoot, $createRangeSelection, $setSelection } from 'lexical';
|
|
10
10
|
import { $isListNode, $isListItemNode, ListNode, ListItemNode, $createListItemNode, $createListNode } from '@lexical/list';
|
|
11
11
|
import { $isQuoteNode, HeadingNode, QuoteNode, $createHeadingNode, $isHeadingNode, $createQuoteNode } from '@lexical/rich-text';
|
|
12
12
|
import { $findMatchingParent } from '@lexical/utils';
|
|
@@ -739,7 +739,7 @@ function formatDevErrorMessage(message) {
|
|
|
739
739
|
|
|
740
740
|
const ORDERED_LIST_REGEX = /^(\s*)(\d{1,})\.\s/;
|
|
741
741
|
const UNORDERED_LIST_REGEX = /^(\s*)[-*+]\s/;
|
|
742
|
-
const CHECK_LIST_REGEX = /^(\s*)(
|
|
742
|
+
const CHECK_LIST_REGEX = /^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i;
|
|
743
743
|
const HEADING_REGEX = /^(#{1,6})\s/;
|
|
744
744
|
const QUOTE_REGEX = /^>\s/;
|
|
745
745
|
const CODE_START_REGEX = /^[ \t]*```([\w-]+)?/;
|
|
@@ -747,6 +747,9 @@ const CODE_END_REGEX = /[ \t]*```$/;
|
|
|
747
747
|
const CODE_SINGLE_LINE_REGEX = /^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/;
|
|
748
748
|
const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
|
|
749
749
|
const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/;
|
|
750
|
+
const listMarkerState = createState('mdListMarker', {
|
|
751
|
+
parse: v => typeof v === 'string' ? v : '-'
|
|
752
|
+
});
|
|
750
753
|
const createBlockNode = createNode => {
|
|
751
754
|
return (parentNode, children, match, isImport) => {
|
|
752
755
|
const node = createNode(match);
|
|
@@ -778,7 +781,11 @@ const listReplace = listType => {
|
|
|
778
781
|
const previousNode = parentNode.getPreviousSibling();
|
|
779
782
|
const nextNode = parentNode.getNextSibling();
|
|
780
783
|
const listItem = $createListItemNode(listType === 'check' ? match[3] === 'x' : undefined);
|
|
784
|
+
const listMarker = listType === 'bullet' || listType === 'check' ? match[0].trim()[0] : undefined;
|
|
781
785
|
if ($isListNode(nextNode) && nextNode.getListType() === listType) {
|
|
786
|
+
if (listMarker) {
|
|
787
|
+
$setState(nextNode, listMarkerState, listMarker);
|
|
788
|
+
}
|
|
782
789
|
const firstChild = nextNode.getFirstChild();
|
|
783
790
|
if (firstChild !== null) {
|
|
784
791
|
firstChild.insertBefore(listItem);
|
|
@@ -788,10 +795,16 @@ const listReplace = listType => {
|
|
|
788
795
|
}
|
|
789
796
|
parentNode.remove();
|
|
790
797
|
} else if ($isListNode(previousNode) && previousNode.getListType() === listType) {
|
|
798
|
+
if (listMarker) {
|
|
799
|
+
$setState(previousNode, listMarkerState, listMarker);
|
|
800
|
+
}
|
|
791
801
|
previousNode.append(listItem);
|
|
792
802
|
parentNode.remove();
|
|
793
803
|
} else {
|
|
794
804
|
const list = $createListNode(listType, listType === 'number' ? Number(match[2]) : undefined);
|
|
805
|
+
if (listMarker) {
|
|
806
|
+
$setState(list, listMarkerState, listMarker);
|
|
807
|
+
}
|
|
795
808
|
list.append(listItem);
|
|
796
809
|
parentNode.replace(list);
|
|
797
810
|
}
|
|
@@ -805,7 +818,7 @@ const listReplace = listType => {
|
|
|
805
818
|
}
|
|
806
819
|
};
|
|
807
820
|
};
|
|
808
|
-
const listExport = (listNode, exportChildren, depth) => {
|
|
821
|
+
const $listExport = (listNode, exportChildren, depth) => {
|
|
809
822
|
const output = [];
|
|
810
823
|
const children = listNode.getChildren();
|
|
811
824
|
let index = 0;
|
|
@@ -814,13 +827,14 @@ const listExport = (listNode, exportChildren, depth) => {
|
|
|
814
827
|
if (listItemNode.getChildrenSize() === 1) {
|
|
815
828
|
const firstChild = listItemNode.getFirstChild();
|
|
816
829
|
if ($isListNode(firstChild)) {
|
|
817
|
-
output.push(listExport(firstChild, exportChildren, depth + 1));
|
|
830
|
+
output.push($listExport(firstChild, exportChildren, depth + 1));
|
|
818
831
|
continue;
|
|
819
832
|
}
|
|
820
833
|
}
|
|
821
834
|
const indent = ' '.repeat(depth * LIST_INDENT_SIZE);
|
|
822
835
|
const listType = listNode.getListType();
|
|
823
|
-
const
|
|
836
|
+
const listMarker = $getState(listNode, listMarkerState);
|
|
837
|
+
const prefix = listType === 'number' ? `${listNode.getStart() + index}. ` : listType === 'check' ? `${listMarker} [${listItemNode.getChecked() ? 'x' : ' '}] ` : listMarker + ' ';
|
|
824
838
|
output.push(indent + prefix + exportChildren(listItemNode));
|
|
825
839
|
index++;
|
|
826
840
|
}
|
|
@@ -937,7 +951,7 @@ const CODE = {
|
|
|
937
951
|
const UNORDERED_LIST = {
|
|
938
952
|
dependencies: [ListNode, ListItemNode],
|
|
939
953
|
export: (node, exportChildren) => {
|
|
940
|
-
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
954
|
+
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
941
955
|
},
|
|
942
956
|
regExp: UNORDERED_LIST_REGEX,
|
|
943
957
|
replace: listReplace('bullet'),
|
|
@@ -946,7 +960,7 @@ const UNORDERED_LIST = {
|
|
|
946
960
|
const CHECK_LIST = {
|
|
947
961
|
dependencies: [ListNode, ListItemNode],
|
|
948
962
|
export: (node, exportChildren) => {
|
|
949
|
-
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
963
|
+
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
950
964
|
},
|
|
951
965
|
regExp: CHECK_LIST_REGEX,
|
|
952
966
|
replace: listReplace('check'),
|
|
@@ -955,7 +969,7 @@ const CHECK_LIST = {
|
|
|
955
969
|
const ORDERED_LIST = {
|
|
956
970
|
dependencies: [ListNode, ListItemNode],
|
|
957
971
|
export: (node, exportChildren) => {
|
|
958
|
-
return $isListNode(node) ? listExport(node, exportChildren, 0) : null;
|
|
972
|
+
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
|
|
959
973
|
},
|
|
960
974
|
regExp: ORDERED_LIST_REGEX,
|
|
961
975
|
replace: listReplace('number'),
|
package/LexicalMarkdown.prod.js
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
"use strict";var e=require("lexical"),t=require("@lexical/list"),n=require("@lexical/rich-text"),o=require("@lexical/utils"),r=require("@lexical/code"),i=require("@lexical/link");function s(e,t){const n={};for(const o of e){const e=t(o);e&&(n[e]?n[e].push(o):n[e]=[o])}return n}function c(e){const t=s(e,e=>e.type);return{element:t.element||[],multilineElement:t["multiline-element"]||[],textFormat:t["text-format"]||[],textMatch:t["text-match"]||[]}}const l=/[!-/:-@[-`{-~\s]/,a=/^\s{0,3}$/;function f(t){if(!e.$isParagraphNode(t))return!1;const n=t.getFirstChild();return null==n||1===t.getChildrenSize()&&e.$isTextNode(n)&&a.test(n.getTextContent())}function d(t,n,o,r){for(const e of n){if(!e.export)continue;const n=e.export(t,e=>u(e,o,r));if(null!=n)return n}return e.$isElementNode(t)?u(t,o,r):e.$isDecoratorNode(t)?t.getTextContent():null}function u(t,n,o,r,i){const s=[],c=t.getChildren();r||(r=[]),i||(i=[]);e:for(const t of c){for(const e of o){if(!e.export)continue;const c=e.export(t,e=>u(e,n,o,r,[...i,...r]),(e,t)=>g(e,t,n,r,i));if(null!=c){s.push(c);continue e}}e.$isLineBreakNode(t)?s.push("\n"):e.$isTextNode(t)?s.push(g(t,t.getTextContent(),n,r,i)):e.$isElementNode(t)?s.push(u(t,n,o,r,i)):e.$isDecoratorNode(t)&&s.push(t.getTextContent())}return s.join("")}function g(e,t,n,o,r){let i=0===e.getFormat()?t:function(e){return e.replace(/^\s+|\s+$/g,e=>[...e].map(e=>"&#"+e.codePointAt(0)+";").join(""))}(t);e.hasFormat("code")||(i=i.replace(/([*_`~\\])/g,"\\$1"));let s="",c="",l="";const a=p(e,!0),f=p(e,!1),d=new Set;for(const t of n){const n=t.format[0],r=t.tag;x(e,n)&&!d.has(n)&&(d.add(n),x(a,n)&&o.find(e=>e.tag===r)||(o.push({format:n,tag:r}),s+=r))}for(let t=0;t<o.length;t++){const n=x(e,o[t].format),i=x(f,o[t].format);if(n&&i)continue;const s=[...o];for(;s.length>t;){const e=s.pop();r&&e&&r.find(t=>t.tag===e.tag)||(e&&"string"==typeof e.tag&&(n?i||(l+=e.tag):c+=e.tag),o.pop())}break}return i=s+i+l,c+i}function p(t,n){let o=n?t.getPreviousSibling():t.getNextSibling();if(!o){const e=t.getParentOrThrow();e.isInline()&&(o=n?e.getPreviousSibling():e.getNextSibling())}for(;o;){if(e.$isElementNode(o)){if(!o.isInline())break;const t=n?o.getLastDescendant():o.getFirstDescendant();if(e.$isTextNode(t))return t;o=n?o.getPreviousSibling():o.getNextSibling()}if(e.$isTextNode(o))return o;if(!e.$isElementNode(o))return null}return null}function x(t,n){return e.$isTextNode(t)&&t.hasFormat(n)}function h(e,t){const n=function(e,t){const n=e.match(t.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=t.fullMatchRegExpByTag[n];if(null==r)continue;const i=e.match(r),s=t.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:t=0}=i,n=e[t-1],o=e[t+i[0].length];if((!n||l.test(n))&&(!o||l.test(o)))return i}}return null}(e.getTextContent(),t);if(!n)return null;const o=n.index||0;return{endIndex:o+n[0].length,match:n,startIndex:o,transformer:t.transformersByTag[n[1]]}}function m(t){return e.$isTextNode(t)&&!t.hasFormat("code")}function $(e,t,n){let o=h(e,t),r=function(e,t){const n=e;let o,r,i,s;for(const e of t){if(!e.replace||!e.importRegExp)continue;const t=n.getTextContent().match(e.importRegExp);if(!t)continue;const c=t.index||0,l=e.getEndIndex?e.getEndIndex(n,t):c+t[0].length;!1!==l&&(void 0===o||void 0===r||c<o&&(l>r||l<=o))&&(o=c,r=l,i=e,s=t)}return void 0===o||void 0===r||void 0===i||void 0===s?null:{endIndex:r,match:s,startIndex:o,transformer:i}}(e,n);if(o&&r&&(o.startIndex<=r.startIndex&&o.endIndex>=r.endIndex||r.startIndex>o.endIndex?r=null:o=null),o){const r=function(e,t,n,o,r){const i=e.getTextContent();let s,c,l;if(r[0]===i?s=e:0===t?[s,c]=e.splitText(n):[l,s,c]=e.splitText(t,n),s.setTextContent(r[2]),o)for(const e of o.format)s.hasFormat(e)||s.toggleFormat(e);return{nodeAfter:c,nodeBefore:l,transformedNode:s}}(e,o.startIndex,o.endIndex,o.transformer,o.match);m(r.nodeAfter)&&$(r.nodeAfter,t,n),m(r.nodeBefore)&&$(r.nodeBefore,t,n),m(r.transformedNode)&&$(r.transformedNode,t,n)}else if(r){const o=function(e,t,n,o,r){let i,s,c;return 0===t?[i,s]=e.splitText(n):[c,i,s]=e.splitText(t,n),o.replace?{nodeAfter:s,nodeBefore:c,transformedNode:o.replace(i,r)||void 0}:null}(e,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;m(o.nodeAfter)&&$(o.nodeAfter,t,n),m(o.nodeBefore)&&$(o.nodeBefore,t,n),m(o.transformedNode)&&$(o.transformedNode,t,n)}const i=e.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,(e,t)=>String.fromCodePoint(t));e.setTextContent(i)}function N(t,n=!1){const o=c(t),r=function(e){const t={},n={},o=[],r="(?<![\\\\])";for(const r of e){const{tag:e}=r;t[e]=r;const i=e.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),1===e.length?n[e]=new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`):n[e]=new RegExp(`(?<!\\\\)(${i})((\\\\${i})?.*?[^\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:t}}(o.textFormat);return(t,i)=>{const s=t.split("\n"),c=s.length,l=i||e.$getRoot();l.clear();for(let e=0;e<c;e++){const t=s[e],[i,c]=T(s,e,o.multilineElement,l);i?e=c:E(t,l,o.element,r,o.textMatch,n)}const a=l.getChildren();for(const e of a)!n&&f(e)&&l.getChildrenSize()>1&&e.remove();null!==e.$getSelection()&&l.selectStart()}}function T(e,t,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:i,regExpStart:s,replace:c}=r,l=e[t].match(s);if(!l)continue;if(n){const i=n({lines:e,rootNode:o,startLineIndex:t,startMatch:l,transformer:r});if(null===i)continue;if(i)return i}const a="object"==typeof i&&"regExp"in i?i.regExp:i,f=i&&"object"==typeof i&&"optional"in i?i.optional:!i;let d=t;const u=e.length;for(;d<u;){const n=a?e[d].match(a):null;if(!n&&(!f||f&&d<u-1)){d++;continue}if(n&&t===d&&n.index===l.index){d++;continue}const r=[];if(n&&t===d)r.push(e[t].slice(l[0].length,-n[0].length));else for(let o=t;o<=d;o++)if(o===t){const t=e[o].slice(l[0].length);r.push(t)}else if(o===d&&n){const t=e[o].slice(0,-n[0].length);r.push(t)}else r.push(e[o]);if(!1!==c(o,null,l,n,r,!0))return[!0,d];break}}return[!1,t]}function E(r,i,s,c,l,a){const f=e.$createTextNode(r),d=e.$createParagraphNode();d.append(f),i.append(d);for(const{regExp:e,replace:t}of s){const n=r.match(e);if(n&&(f.setTextContent(r.slice(n[0].length)),!1!==t(d,[f],n,!0)))break}if($(f,c,l),d.isAttached()&&r.length>0){const r=d.getPreviousSibling();if(!a&&(e.$isParagraphNode(r)||n.$isQuoteNode(r)||t.$isListNode(r))){let n=r;if(t.$isListNode(r)){const e=r.getLastDescendant();n=null==e?null:o.$findMatchingParent(e,t.$isListItemNode)}null!=n&&n.getTextContentSize()>0&&(n.splice(n.getChildrenSize(),0,[e.$createLineBreakNode(),...d.getChildren()]),d.remove())}}}function S(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}const C=/^(\s*)(\d{1,})\.\s/,L=/^(\s*)[-*+]\s/,R=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,I=/^(#{1,6})\s/,y=/^>\s/,v=/^[ \t]*```([\w-]+)?/,_=/[ \t]*```$/,b=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,A=/^(?:\|)(.+)(?:\|)\s?$/,F=/^(\| ?:?-*:? ?)+\|\s?$/,O=e=>(t,n,o,r)=>{const i=e(o);i.append(...n),t.replace(i),r||i.select(0,0)};const k=e=>(n,o,r,i)=>{const s=n.getPreviousSibling(),c=n.getNextSibling(),l=t.$createListItemNode("check"===e?"x"===r[3]:void 0);if(t.$isListNode(c)&&c.getListType()===e){const e=c.getFirstChild();null!==e?e.insertBefore(l):c.append(l),n.remove()}else if(t.$isListNode(s)&&s.getListType()===e)s.append(l),n.remove();else{const o=t.$createListNode(e,"number"===e?Number(r[2]):void 0);o.append(l),n.replace(o)}l.append(...o),i||l.select(0,0);const a=function(e){const t=e.match(/\t/g),n=e.match(/ /g);let o=0;return t&&(o+=t.length),n&&(o+=Math.floor(n.length/4)),o}(r[1]);a&&l.setIndent(a)},M=(e,n,o)=>{const r=[],i=e.getChildren();let s=0;for(const c of i)if(t.$isListItemNode(c)){if(1===c.getChildrenSize()){const e=c.getFirstChild();if(t.$isListNode(e)){r.push(M(e,n,o+1));continue}}const i=" ".repeat(4*o),l=e.getListType(),a="number"===l?`${e.getStart()+s}. `:"check"===l?`- [${c.getChecked()?"x":" "}] `:"- ";r.push(i+a+n(c)),s++}return r.join("\n")},B={dependencies:[n.HeadingNode],export:(e,t)=>{if(!n.$isHeadingNode(e))return null;const o=Number(e.getTag().slice(1));return"#".repeat(o)+" "+t(e)},regExp:I,replace:O(e=>{const t="h"+e[1].length;return n.$createHeadingNode(t)}),type:"element"},w={dependencies:[n.QuoteNode],export:(e,t)=>{if(!n.$isQuoteNode(e))return null;const o=t(e).split("\n"),r=[];for(const e of o)r.push("> "+e);return r.join("\n")},regExp:y,replace:(t,o,r,i)=>{if(i){const r=t.getPreviousSibling();if(n.$isQuoteNode(r))return r.splice(r.getChildrenSize(),0,[e.$createLineBreakNode(),...o]),void t.remove()}const s=n.$createQuoteNode();s.append(...o),t.replace(s),i||s.select(0,0)},type:"element"},D={dependencies:[r.CodeNode],export:e=>{if(!r.$isCodeNode(e))return null;const t=e.getTextContent();return"```"+(e.getLanguage()||"")+(t?"\n"+t:"")+"\n```"},regExpEnd:{optional:!0,regExp:_},regExpStart:v,replace:(t,n,o,i,s,c)=>{let l,a;if(!n&&s){if(1===s.length)i?(l=r.$createCodeNode(),a=o[1]+s[0]):(l=r.$createCodeNode(o[1]),a=s[0].startsWith(" ")?s[0].slice(1):s[0]);else{if(l=r.$createCodeNode(o[1]),0===s[0].trim().length)for(;s.length>0&&!s[0].length;)s.shift();else s[0]=s[0].startsWith(" ")?s[0].slice(1):s[0];for(;s.length>0&&!s[s.length-1].length;)s.pop();a=s.join("\n")}const n=e.$createTextNode(a);l.append(n),t.append(l)}else n&&O(e=>r.$createCodeNode(e?e[1]:void 0))(t,n,o,c)},type:"multiline-element"},P={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?M(e,n,0):null,regExp:L,replace:k("bullet"),type:"element"},H={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?M(e,n,0):null,regExp:R,replace:k("check"),type:"element"},U={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?M(e,n,0):null,regExp:C,replace:k("number"),type:"element"},j={format:["code"],tag:"`",type:"text-format"},q={format:["highlight"],tag:"==",type:"text-format"},z={format:["bold","italic"],tag:"***",type:"text-format"},G={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},Q={format:["bold"],tag:"**",type:"text-format"},W={format:["bold"],intraword:!1,tag:"__",type:"text-format"},K={format:["strikethrough"],tag:"~~",type:"text-format"},X={format:["italic"],tag:"*",type:"text-format"},J={format:["italic"],intraword:!1,tag:"_",type:"text-format"},V={dependencies:[i.LinkNode],export:(e,t,n)=>{if(!i.$isLinkNode(e)||i.$isAutoLinkNode(e))return null;const o=e.getTitle(),r=t(e);return o?`[${r}](${e.getURL()} "${o}")`:`[${r}](${e.getURL()})`},importRegExp:/(?:\[(.*?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[(.*?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,n)=>{const[,o,r,s]=n,c=i.$createLinkNode(r,{title:s}),l=o.split("[").length-1,a=o.split("]").length-1;let f=o,d="";if(l<a)return;if(l>a){const e=o.split("[");d="["+e[0],f=e.slice(1).join("[")}const u=e.$createTextNode(f);return u.setFormat(t.getFormat()),c.append(u),t.replace(c),d&&c.insertBefore(e.$createTextNode(d)),u},trigger:")",type:"text-match"},Y=[B,w,P,U],Z=[D],ee=[j,z,G,Q,W,q,X,J,K],te=[V],ne=[...Y,...Z,...ee,...te];function oe(e,t,n){const o=n.length;for(let r=t;r>=o;r--){const t=r-o;if(re(e,t,n,0,o)&&" "!==e[t+o])return t}return-1}function re(e,t,n,o,r){for(let i=0;i<r;i++)if(e[t+i]!==n[o+i])return!1;return!0}exports.$convertFromMarkdownString=function(e,t=ne,n,o=!1,r=!1){const i=o?e:function(e,t=!1){const n=e.split("\n");let o=!1;const r=[];for(let e=0;e<n.length;e++){const i=n[e],s=r[r.length-1];b.test(i)?r.push(i):v.test(i)||_.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||I.test(s)||I.test(i)||y.test(i)||C.test(i)||L.test(i)||R.test(i)||A.test(i)||F.test(i)||!t?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(e,r);return N(t,o)(i,n)},exports.$convertToMarkdownString=function(t=ne,n,o=!1){return function(t,n=!1){const o=c(t),r=[...o.multilineElement,...o.element],i=!n,s=o.textFormat.filter(e=>1===e.format.length).sort((e,t)=>Number(e.format.includes("code"))-Number(t.format.includes("code")));return t=>{const n=[],c=(t||e.$getRoot()).getChildren();for(let e=0;e<c.length;e++){const t=c[e],l=d(t,r,s,o.textMatch);null!=l&&n.push(i&&e>0&&!f(t)&&!f(c[e-1])?"\n".concat(l):l)}return n.join("\n")}}(t,o)(n)},exports.BOLD_ITALIC_STAR=z,exports.BOLD_ITALIC_UNDERSCORE=G,exports.BOLD_STAR=Q,exports.BOLD_UNDERSCORE=W,exports.CHECK_LIST=H,exports.CODE=D,exports.ELEMENT_TRANSFORMERS=Y,exports.HEADING=B,exports.HIGHLIGHT=q,exports.INLINE_CODE=j,exports.ITALIC_STAR=X,exports.ITALIC_UNDERSCORE=J,exports.LINK=V,exports.MULTILINE_ELEMENT_TRANSFORMERS=Z,exports.ORDERED_LIST=U,exports.QUOTE=w,exports.STRIKETHROUGH=K,exports.TEXT_FORMAT_TRANSFORMERS=ee,exports.TEXT_MATCH_TRANSFORMERS=te,exports.TRANSFORMERS=ne,exports.UNORDERED_LIST=P,exports.registerMarkdownShortcuts=function(t,n=ne){const o=c(n),i=s(o.textFormat,({tag:e})=>e[e.length-1]),a=s(o.textMatch,({trigger:e})=>e);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)||S(173,e.getType())}}const f=(t,n,r)=>{(function(t,n,o,r){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const s=n.getTextContent();if(" "!==s[o-1])return!1;for(const{regExp:e,replace:i}of r){const r=s.match(e);if(r&&r[0].length===(r[0].endsWith(" ")?o:o-1)){const e=n.getNextSiblings(),[s,c]=n.splitText(o);if(!1!==i(t,c?[c,...e]:e,r,!1))return s.remove(),!0}}return!1})(t,n,r,o.element)||function(t,n,o,r){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const s=n.getTextContent();if(" "!==s[o-1])return!1;for(const{regExpStart:e,replace:i,regExpEnd:c}of r){if(c&&!("optional"in c)||c&&"optional"in c&&!c.optional)continue;const r=s.match(e);if(r&&r[0].length===(r[0].endsWith(" ")?o:o-1)){const e=n.getNextSiblings(),[s,c]=n.splitText(o);if(!1!==i(t,c?[c,...e]:e,r,null,null,!1))return s.remove(),!0}}return!1}(t,n,r,o.multilineElement)||function(e,t,n){let o=e.getTextContent();const r=n[o[t-1]];if(null==r)return!1;t<o.length&&(o=o.slice(0,t));for(const t of r){if(!t.replace||!t.regExp)continue;const n=o.match(t.regExp);if(null===n)continue;const r=n.index||0,i=r+n[0].length;let s;return 0===r?[s]=e.splitText(i):[,s]=e.splitText(r,i),s.selectNext(0,0),t.replace(s,n),!0}return!1}(n,r,a)||function(t,n,o){const r=t.getTextContent(),i=n-1,s=r[i],c=o[s];if(!c)return!1;for(const n of c){const{tag:o}=n,c=o.length,a=i-c+1;if(c>1&&!re(r,a,o,0,c))continue;if(" "===r[a-1])continue;const f=r[i+1];if(!1===n.intraword&&f&&!l.test(f))continue;const d=t;let u=d,g=oe(r,a,o),p=u;for(;g<0&&(p=p.getPreviousSibling())&&!e.$isLineBreakNode(p);)if(e.$isTextNode(p)){if(p.hasFormat("code"))continue;const e=p.getTextContent();u=p,g=oe(e,e.length,o)}if(g<0)continue;if(u===d&&g+c===a)continue;const x=u.getTextContent();if(g>0&&x[g-1]===s)continue;const h=x[g-1];if(!1===n.intraword&&h&&!l.test(h))continue;const m=d.getTextContent(),$=m.slice(0,a)+m.slice(i+1);d.setTextContent($);const N=u===d?$:x;u.setTextContent(N.slice(0,g)+N.slice(g+c));const T=e.$getSelection(),E=e.$createRangeSelection();e.$setSelection(E);const S=i-c*(u===d?2:1)+1;E.anchor.set(u.__key,g,"text"),E.focus.set(d.__key,S,"text");for(const e of n.format)E.hasFormat(e)||E.formatText(e);E.anchor.set(E.focus.key,E.focus.offset,E.focus.type);for(const e of n.format)E.hasFormat(e)&&E.toggleFormat(e);return e.$isRangeSelection(T)&&(E.format=T.format),!0}}(n,r,i)};return t.registerUpdateListener(({tags:n,dirtyLeaves:o,editorState:i,prevEditorState:s})=>{if(n.has(e.COLLABORATION_TAG)||n.has(e.HISTORIC_TAG))return;if(t.isComposing())return;const c=i.read(e.$getSelection),l=s.read(e.$getSelection);if(!e.$isRangeSelection(l)||!e.$isRangeSelection(c)||!c.isCollapsed()||c.is(l))return;const a=c.anchor.key,d=c.anchor.offset,u=i._nodeMap.get(a);!e.$isTextNode(u)||!o.has(a)||1!==d&&d>l.anchor.offset+1||t.update(()=>{if(!m(u))return;const e=u.getParent();null===e||r.$isCodeNode(e)||f(e,u,c.anchor.offset)})})};
|
|
9
|
+
"use strict";var e=require("lexical"),t=require("@lexical/list"),n=require("@lexical/rich-text"),o=require("@lexical/utils"),r=require("@lexical/code"),i=require("@lexical/link");function s(e,t){const n={};for(const o of e){const e=t(o);e&&(n[e]?n[e].push(o):n[e]=[o])}return n}function c(e){const t=s(e,e=>e.type);return{element:t.element||[],multilineElement:t["multiline-element"]||[],textFormat:t["text-format"]||[],textMatch:t["text-match"]||[]}}const l=/[!-/:-@[-`{-~\s]/,a=/^\s{0,3}$/;function f(t){if(!e.$isParagraphNode(t))return!1;const n=t.getFirstChild();return null==n||1===t.getChildrenSize()&&e.$isTextNode(n)&&a.test(n.getTextContent())}function d(t,n,o,r){for(const e of n){if(!e.export)continue;const n=e.export(t,e=>u(e,o,r));if(null!=n)return n}return e.$isElementNode(t)?u(t,o,r):e.$isDecoratorNode(t)?t.getTextContent():null}function u(t,n,o,r,i){const s=[],c=t.getChildren();r||(r=[]),i||(i=[]);e:for(const t of c){for(const e of o){if(!e.export)continue;const c=e.export(t,e=>u(e,n,o,r,[...i,...r]),(e,t)=>g(e,t,n,r,i));if(null!=c){s.push(c);continue e}}e.$isLineBreakNode(t)?s.push("\n"):e.$isTextNode(t)?s.push(g(t,t.getTextContent(),n,r,i)):e.$isElementNode(t)?s.push(u(t,n,o,r,i)):e.$isDecoratorNode(t)&&s.push(t.getTextContent())}return s.join("")}function g(e,t,n,o,r){let i=0===e.getFormat()?t:function(e){return e.replace(/^\s+|\s+$/g,e=>[...e].map(e=>"&#"+e.codePointAt(0)+";").join(""))}(t);e.hasFormat("code")||(i=i.replace(/([*_`~\\])/g,"\\$1"));let s="",c="",l="";const a=p(e,!0),f=p(e,!1),d=new Set;for(const t of n){const n=t.format[0],r=t.tag;x(e,n)&&!d.has(n)&&(d.add(n),x(a,n)&&o.find(e=>e.tag===r)||(o.push({format:n,tag:r}),s+=r))}for(let t=0;t<o.length;t++){const n=x(e,o[t].format),i=x(f,o[t].format);if(n&&i)continue;const s=[...o];for(;s.length>t;){const e=s.pop();r&&e&&r.find(t=>t.tag===e.tag)||(e&&"string"==typeof e.tag&&(n?i||(l+=e.tag):c+=e.tag),o.pop())}break}return i=s+i+l,c+i}function p(t,n){let o=n?t.getPreviousSibling():t.getNextSibling();if(!o){const e=t.getParentOrThrow();e.isInline()&&(o=n?e.getPreviousSibling():e.getNextSibling())}for(;o;){if(e.$isElementNode(o)){if(!o.isInline())break;const t=n?o.getLastDescendant():o.getFirstDescendant();if(e.$isTextNode(t))return t;o=n?o.getPreviousSibling():o.getNextSibling()}if(e.$isTextNode(o))return o;if(!e.$isElementNode(o))return null}return null}function x(t,n){return e.$isTextNode(t)&&t.hasFormat(n)}function h(e,t){const n=function(e,t){const n=e.match(t.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=t.fullMatchRegExpByTag[n];if(null==r)continue;const i=e.match(r),s=t.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:t=0}=i,n=e[t-1],o=e[t+i[0].length];if((!n||l.test(n))&&(!o||l.test(o)))return i}}return null}(e.getTextContent(),t);if(!n)return null;const o=n.index||0;return{endIndex:o+n[0].length,match:n,startIndex:o,transformer:t.transformersByTag[n[1]]}}function m(t){return e.$isTextNode(t)&&!t.hasFormat("code")}function $(e,t,n){let o=h(e,t),r=function(e,t){const n=e;let o,r,i,s;for(const e of t){if(!e.replace||!e.importRegExp)continue;const t=n.getTextContent().match(e.importRegExp);if(!t)continue;const c=t.index||0,l=e.getEndIndex?e.getEndIndex(n,t):c+t[0].length;!1!==l&&(void 0===o||void 0===r||c<o&&(l>r||l<=o))&&(o=c,r=l,i=e,s=t)}return void 0===o||void 0===r||void 0===i||void 0===s?null:{endIndex:r,match:s,startIndex:o,transformer:i}}(e,n);if(o&&r&&(o.startIndex<=r.startIndex&&o.endIndex>=r.endIndex||r.startIndex>o.endIndex?r=null:o=null),o){const r=function(e,t,n,o,r){const i=e.getTextContent();let s,c,l;if(r[0]===i?s=e:0===t?[s,c]=e.splitText(n):[l,s,c]=e.splitText(t,n),s.setTextContent(r[2]),o)for(const e of o.format)s.hasFormat(e)||s.toggleFormat(e);return{nodeAfter:c,nodeBefore:l,transformedNode:s}}(e,o.startIndex,o.endIndex,o.transformer,o.match);m(r.nodeAfter)&&$(r.nodeAfter,t,n),m(r.nodeBefore)&&$(r.nodeBefore,t,n),m(r.transformedNode)&&$(r.transformedNode,t,n)}else if(r){const o=function(e,t,n,o,r){let i,s,c;return 0===t?[i,s]=e.splitText(n):[c,i,s]=e.splitText(t,n),o.replace?{nodeAfter:s,nodeBefore:c,transformedNode:o.replace(i,r)||void 0}:null}(e,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;m(o.nodeAfter)&&$(o.nodeAfter,t,n),m(o.nodeBefore)&&$(o.nodeBefore,t,n),m(o.transformedNode)&&$(o.transformedNode,t,n)}const i=e.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,(e,t)=>String.fromCodePoint(t));e.setTextContent(i)}function N(t,n=!1){const o=c(t),r=function(e){const t={},n={},o=[],r="(?<![\\\\])";for(const r of e){const{tag:e}=r;t[e]=r;const i=e.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),1===e.length?n[e]=new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`):n[e]=new RegExp(`(?<!\\\\)(${i})((\\\\${i})?.*?[^\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:t}}(o.textFormat);return(t,i)=>{const s=t.split("\n"),c=s.length,l=i||e.$getRoot();l.clear();for(let e=0;e<c;e++){const t=s[e],[i,c]=T(s,e,o.multilineElement,l);i?e=c:E(t,l,o.element,r,o.textMatch,n)}const a=l.getChildren();for(const e of a)!n&&f(e)&&l.getChildrenSize()>1&&e.remove();null!==e.$getSelection()&&l.selectStart()}}function T(e,t,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:i,regExpStart:s,replace:c}=r,l=e[t].match(s);if(!l)continue;if(n){const i=n({lines:e,rootNode:o,startLineIndex:t,startMatch:l,transformer:r});if(null===i)continue;if(i)return i}const a="object"==typeof i&&"regExp"in i?i.regExp:i,f=i&&"object"==typeof i&&"optional"in i?i.optional:!i;let d=t;const u=e.length;for(;d<u;){const n=a?e[d].match(a):null;if(!n&&(!f||f&&d<u-1)){d++;continue}if(n&&t===d&&n.index===l.index){d++;continue}const r=[];if(n&&t===d)r.push(e[t].slice(l[0].length,-n[0].length));else for(let o=t;o<=d;o++)if(o===t){const t=e[o].slice(l[0].length);r.push(t)}else if(o===d&&n){const t=e[o].slice(0,-n[0].length);r.push(t)}else r.push(e[o]);if(!1!==c(o,null,l,n,r,!0))return[!0,d];break}}return[!1,t]}function E(r,i,s,c,l,a){const f=e.$createTextNode(r),d=e.$createParagraphNode();d.append(f),i.append(d);for(const{regExp:e,replace:t}of s){const n=r.match(e);if(n&&(f.setTextContent(r.slice(n[0].length)),!1!==t(d,[f],n,!0)))break}if($(f,c,l),d.isAttached()&&r.length>0){const r=d.getPreviousSibling();if(!a&&(e.$isParagraphNode(r)||n.$isQuoteNode(r)||t.$isListNode(r))){let n=r;if(t.$isListNode(r)){const e=r.getLastDescendant();n=null==e?null:o.$findMatchingParent(e,t.$isListItemNode)}null!=n&&n.getTextContentSize()>0&&(n.splice(n.getChildrenSize(),0,[e.$createLineBreakNode(),...d.getChildren()]),d.remove())}}}function S(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}const C=/^(\s*)(\d{1,})\.\s/,L=/^(\s*)[-*+]\s/,R=/^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i,I=/^(#{1,6})\s/,y=/^>\s/,v=/^[ \t]*```([\w-]+)?/,_=/[ \t]*```$/,b=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,A=/^(?:\|)(.+)(?:\|)\s?$/,F=/^(\| ?:?-*:? ?)+\|\s?$/,k=e.createState("mdListMarker",{parse:e=>"string"==typeof e?e:"-"}),M=e=>(t,n,o,r)=>{const i=e(o);i.append(...n),t.replace(i),r||i.select(0,0)};const O=n=>(o,r,i,s)=>{const c=o.getPreviousSibling(),l=o.getNextSibling(),a=t.$createListItemNode("check"===n?"x"===i[3]:void 0),f="bullet"===n||"check"===n?i[0].trim()[0]:void 0;if(t.$isListNode(l)&&l.getListType()===n){f&&e.$setState(l,k,f);const t=l.getFirstChild();null!==t?t.insertBefore(a):l.append(a),o.remove()}else if(t.$isListNode(c)&&c.getListType()===n)f&&e.$setState(c,k,f),c.append(a),o.remove();else{const r=t.$createListNode(n,"number"===n?Number(i[2]):void 0);f&&e.$setState(r,k,f),r.append(a),o.replace(r)}a.append(...r),s||a.select(0,0);const d=function(e){const t=e.match(/\t/g),n=e.match(/ /g);let o=0;return t&&(o+=t.length),n&&(o+=Math.floor(n.length/4)),o}(i[1]);d&&a.setIndent(d)},B=(n,o,r)=>{const i=[],s=n.getChildren();let c=0;for(const l of s)if(t.$isListItemNode(l)){if(1===l.getChildrenSize()){const e=l.getFirstChild();if(t.$isListNode(e)){i.push(B(e,o,r+1));continue}}const s=" ".repeat(4*r),a=n.getListType(),f=e.$getState(n,k),d="number"===a?`${n.getStart()+c}. `:"check"===a?`${f} [${l.getChecked()?"x":" "}] `:f+" ";i.push(s+d+o(l)),c++}return i.join("\n")},w={dependencies:[n.HeadingNode],export:(e,t)=>{if(!n.$isHeadingNode(e))return null;const o=Number(e.getTag().slice(1));return"#".repeat(o)+" "+t(e)},regExp:I,replace:M(e=>{const t="h"+e[1].length;return n.$createHeadingNode(t)}),type:"element"},D={dependencies:[n.QuoteNode],export:(e,t)=>{if(!n.$isQuoteNode(e))return null;const o=t(e).split("\n"),r=[];for(const e of o)r.push("> "+e);return r.join("\n")},regExp:y,replace:(t,o,r,i)=>{if(i){const r=t.getPreviousSibling();if(n.$isQuoteNode(r))return r.splice(r.getChildrenSize(),0,[e.$createLineBreakNode(),...o]),void t.remove()}const s=n.$createQuoteNode();s.append(...o),t.replace(s),i||s.select(0,0)},type:"element"},P={dependencies:[r.CodeNode],export:e=>{if(!r.$isCodeNode(e))return null;const t=e.getTextContent();return"```"+(e.getLanguage()||"")+(t?"\n"+t:"")+"\n```"},regExpEnd:{optional:!0,regExp:_},regExpStart:v,replace:(t,n,o,i,s,c)=>{let l,a;if(!n&&s){if(1===s.length)i?(l=r.$createCodeNode(),a=o[1]+s[0]):(l=r.$createCodeNode(o[1]),a=s[0].startsWith(" ")?s[0].slice(1):s[0]);else{if(l=r.$createCodeNode(o[1]),0===s[0].trim().length)for(;s.length>0&&!s[0].length;)s.shift();else s[0]=s[0].startsWith(" ")?s[0].slice(1):s[0];for(;s.length>0&&!s[s.length-1].length;)s.pop();a=s.join("\n")}const n=e.$createTextNode(a);l.append(n),t.append(l)}else n&&M(e=>r.$createCodeNode(e?e[1]:void 0))(t,n,o,c)},type:"multiline-element"},H={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?B(e,n,0):null,regExp:L,replace:O("bullet"),type:"element"},U={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?B(e,n,0):null,regExp:R,replace:O("check"),type:"element"},j={dependencies:[t.ListNode,t.ListItemNode],export:(e,n)=>t.$isListNode(e)?B(e,n,0):null,regExp:C,replace:O("number"),type:"element"},q={format:["code"],tag:"`",type:"text-format"},z={format:["highlight"],tag:"==",type:"text-format"},G={format:["bold","italic"],tag:"***",type:"text-format"},Q={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},W={format:["bold"],tag:"**",type:"text-format"},K={format:["bold"],intraword:!1,tag:"__",type:"text-format"},X={format:["strikethrough"],tag:"~~",type:"text-format"},J={format:["italic"],tag:"*",type:"text-format"},V={format:["italic"],intraword:!1,tag:"_",type:"text-format"},Y={dependencies:[i.LinkNode],export:(e,t,n)=>{if(!i.$isLinkNode(e)||i.$isAutoLinkNode(e))return null;const o=e.getTitle(),r=t(e);return o?`[${r}](${e.getURL()} "${o}")`:`[${r}](${e.getURL()})`},importRegExp:/(?:\[(.*?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[(.*?)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,n)=>{const[,o,r,s]=n,c=i.$createLinkNode(r,{title:s}),l=o.split("[").length-1,a=o.split("]").length-1;let f=o,d="";if(l<a)return;if(l>a){const e=o.split("[");d="["+e[0],f=e.slice(1).join("[")}const u=e.$createTextNode(f);return u.setFormat(t.getFormat()),c.append(u),t.replace(c),d&&c.insertBefore(e.$createTextNode(d)),u},trigger:")",type:"text-match"},Z=[w,D,H,j],ee=[P],te=[q,G,Q,W,K,z,J,V,X],ne=[Y],oe=[...Z,...ee,...te,...ne];function re(e,t,n){const o=n.length;for(let r=t;r>=o;r--){const t=r-o;if(ie(e,t,n,0,o)&&" "!==e[t+o])return t}return-1}function ie(e,t,n,o,r){for(let i=0;i<r;i++)if(e[t+i]!==n[o+i])return!1;return!0}exports.$convertFromMarkdownString=function(e,t=oe,n,o=!1,r=!1){const i=o?e:function(e,t=!1){const n=e.split("\n");let o=!1;const r=[];for(let e=0;e<n.length;e++){const i=n[e],s=r[r.length-1];b.test(i)?r.push(i):v.test(i)||_.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||I.test(s)||I.test(i)||y.test(i)||C.test(i)||L.test(i)||R.test(i)||A.test(i)||F.test(i)||!t?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(e,r);return N(t,o)(i,n)},exports.$convertToMarkdownString=function(t=oe,n,o=!1){return function(t,n=!1){const o=c(t),r=[...o.multilineElement,...o.element],i=!n,s=o.textFormat.filter(e=>1===e.format.length).sort((e,t)=>Number(e.format.includes("code"))-Number(t.format.includes("code")));return t=>{const n=[],c=(t||e.$getRoot()).getChildren();for(let e=0;e<c.length;e++){const t=c[e],l=d(t,r,s,o.textMatch);null!=l&&n.push(i&&e>0&&!f(t)&&!f(c[e-1])?"\n".concat(l):l)}return n.join("\n")}}(t,o)(n)},exports.BOLD_ITALIC_STAR=G,exports.BOLD_ITALIC_UNDERSCORE=Q,exports.BOLD_STAR=W,exports.BOLD_UNDERSCORE=K,exports.CHECK_LIST=U,exports.CODE=P,exports.ELEMENT_TRANSFORMERS=Z,exports.HEADING=w,exports.HIGHLIGHT=z,exports.INLINE_CODE=q,exports.ITALIC_STAR=J,exports.ITALIC_UNDERSCORE=V,exports.LINK=Y,exports.MULTILINE_ELEMENT_TRANSFORMERS=ee,exports.ORDERED_LIST=j,exports.QUOTE=D,exports.STRIKETHROUGH=X,exports.TEXT_FORMAT_TRANSFORMERS=te,exports.TEXT_MATCH_TRANSFORMERS=ne,exports.TRANSFORMERS=oe,exports.UNORDERED_LIST=H,exports.registerMarkdownShortcuts=function(t,n=oe){const o=c(n),i=s(o.textFormat,({tag:e})=>e[e.length-1]),a=s(o.textMatch,({trigger:e})=>e);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)||S(173,e.getType())}}const f=(t,n,r)=>{(function(t,n,o,r){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const s=n.getTextContent();if(" "!==s[o-1])return!1;for(const{regExp:e,replace:i}of r){const r=s.match(e);if(r&&r[0].length===(r[0].endsWith(" ")?o:o-1)){const e=n.getNextSiblings(),[s,c]=n.splitText(o);if(!1!==i(t,c?[c,...e]:e,r,!1))return s.remove(),!0}}return!1})(t,n,r,o.element)||function(t,n,o,r){const i=t.getParent();if(!e.$isRootOrShadowRoot(i)||t.getFirstChild()!==n)return!1;const s=n.getTextContent();if(" "!==s[o-1])return!1;for(const{regExpStart:e,replace:i,regExpEnd:c}of r){if(c&&!("optional"in c)||c&&"optional"in c&&!c.optional)continue;const r=s.match(e);if(r&&r[0].length===(r[0].endsWith(" ")?o:o-1)){const e=n.getNextSiblings(),[s,c]=n.splitText(o);if(!1!==i(t,c?[c,...e]:e,r,null,null,!1))return s.remove(),!0}}return!1}(t,n,r,o.multilineElement)||function(e,t,n){let o=e.getTextContent();const r=n[o[t-1]];if(null==r)return!1;t<o.length&&(o=o.slice(0,t));for(const t of r){if(!t.replace||!t.regExp)continue;const n=o.match(t.regExp);if(null===n)continue;const r=n.index||0,i=r+n[0].length;let s;return 0===r?[s]=e.splitText(i):[,s]=e.splitText(r,i),s.selectNext(0,0),t.replace(s,n),!0}return!1}(n,r,a)||function(t,n,o){const r=t.getTextContent(),i=n-1,s=r[i],c=o[s];if(!c)return!1;for(const n of c){const{tag:o}=n,c=o.length,a=i-c+1;if(c>1&&!ie(r,a,o,0,c))continue;if(" "===r[a-1])continue;const f=r[i+1];if(!1===n.intraword&&f&&!l.test(f))continue;const d=t;let u=d,g=re(r,a,o),p=u;for(;g<0&&(p=p.getPreviousSibling())&&!e.$isLineBreakNode(p);)if(e.$isTextNode(p)){if(p.hasFormat("code"))continue;const e=p.getTextContent();u=p,g=re(e,e.length,o)}if(g<0)continue;if(u===d&&g+c===a)continue;const x=u.getTextContent();if(g>0&&x[g-1]===s)continue;const h=x[g-1];if(!1===n.intraword&&h&&!l.test(h))continue;const m=d.getTextContent(),$=m.slice(0,a)+m.slice(i+1);d.setTextContent($);const N=u===d?$:x;u.setTextContent(N.slice(0,g)+N.slice(g+c));const T=e.$getSelection(),E=e.$createRangeSelection();e.$setSelection(E);const S=i-c*(u===d?2:1)+1;E.anchor.set(u.__key,g,"text"),E.focus.set(d.__key,S,"text");for(const e of n.format)E.hasFormat(e)||E.formatText(e);E.anchor.set(E.focus.key,E.focus.offset,E.focus.type);for(const e of n.format)E.hasFormat(e)&&E.toggleFormat(e);return e.$isRangeSelection(T)&&(E.format=T.format),!0}}(n,r,i)};return t.registerUpdateListener(({tags:n,dirtyLeaves:o,editorState:i,prevEditorState:s})=>{if(n.has(e.COLLABORATION_TAG)||n.has(e.HISTORIC_TAG))return;if(t.isComposing())return;const c=i.read(e.$getSelection),l=s.read(e.$getSelection);if(!e.$isRangeSelection(l)||!e.$isRangeSelection(c)||!c.isCollapsed()||c.is(l))return;const a=c.anchor.key,d=c.anchor.offset,u=i._nodeMap.get(a);!e.$isTextNode(u)||!o.has(a)||1!==d&&d>l.anchor.offset+1||t.update(()=>{if(!m(u))return;const e=u.getParent();null===e||r.$isCodeNode(e)||f(e,u,c.anchor.offset)})})};
|
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,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||c<=o))&&(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};
|
|
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,createState as a,$setState as u,$getState as g,COLLABORATION_TAG as p,HISTORIC_TAG as d,$isRangeSelection as h,$isRootOrShadowRoot as m,$createRangeSelection as x,$setSelection as T}from"lexical";import{$isListNode as C,$isListItemNode as E,ListNode as y,ListItemNode as v,$createListItemNode as $,$createListNode as b}from"@lexical/list";import{$isQuoteNode as S,HeadingNode as F,QuoteNode as I,$createHeadingNode as N,$isHeadingNode as k,$createQuoteNode as w}from"@lexical/rich-text";import{$findMatchingParent as L}from"@lexical/utils";import{CodeNode as P,$createCodeNode as R,$isCodeNode as B}from"@lexical/code";import{LinkNode as _,$createLinkNode as M,$isLinkNode as j,$isAutoLinkNode as A}from"@lexical/link";function z(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 U(t){const e=z(t,t=>t.type);return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const W=/[!-/:-@[-`{-~\s]/,D=/^\s{0,3}$/;function O(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&D.test(o.getTextContent())}function q(t,e,n,i){for(const o of e){if(!o.export)continue;const e=o.export(t,t=>G(t,n,i));if(null!=e)return e}return o(t)?G(t,n,i):r(t)?t.getTextContent():null}function G(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=>G(t,n,s,l,[...c,...l]),(t,e)=>H(t,e,n,l,c));if(null!=o){f.push(o);continue t}}i(t)?f.push("\n"):e(t)?f.push(H(t,t.getTextContent(),n,l,c)):o(t)?f.push(G(t,n,s,l,c)):r(t)&&f.push(t.getTextContent())}return f.join("")}function H(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=J(t,!0),a=J(t,!1),u=new Set;for(const e of n){const n=e.format[0],r=e.tag;K(t,n)&&!u.has(n)&&(u.add(n),K(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=K(t,o[e].format),i=K(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 J(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 K(t,n){return e(t)&&t.hasFormat(n)}function Q(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||W.test(n))&&(!o||W.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 V(t){return e(t)&&!t.hasFormat("code")}function X(t,e,n){let o=Q(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||c<=o))&&(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);V(r.nodeAfter)&&X(r.nodeAfter,e,n),V(r.nodeBefore)&&X(r.nodeBefore,e,n),V(r.transformedNode)&&X(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;V(o.nodeAfter)&&X(o.nodeAfter,e,n),V(o.nodeBefore)&&X(o.nodeBefore,e,n),V(o.transformedNode)&&X(o.transformedNode,e,n)}const i=t.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,(t,e)=>String.fromCodePoint(e));t.setTextContent(i)}function Y(t,e=!1){const o=U(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]=Z(l,t,o.multilineElement,f);i?t=s:tt(n,f,o.element,r,o.textMatch,e)}const a=f.getChildren();for(const t of a)!e&&O(t)&&f.getChildrenSize()>1&&t.remove();null!==s()&&f.selectStart()}}function Z(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 tt(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(X(a,r,i),u.isAttached()&&e.length>0){const e=u.getPreviousSibling();if(!s&&(t(e)||S(e)||C(e))){let t=e;if(C(e)){const n=e.getLastDescendant();t=null==n?null:L(n,E)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[f(),...u.getChildren()]),u.remove())}}}function et(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 nt=/^(\s*)(\d{1,})\.\s/,ot=/^(\s*)[-*+]\s/,rt=/^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i,it=/^(#{1,6})\s/,st=/^>\s/,lt=/^[ \t]*```([\w-]+)?/,ct=/[ \t]*```$/,ft=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,at=/^(?:\|)(.+)(?:\|)\s?$/,ut=/^(\| ?:?-*:? ?)+\|\s?$/,gt=a("mdListMarker",{parse:t=>"string"==typeof t?t:"-"}),pt=t=>(e,n,o,r)=>{const i=t(o);i.append(...n),e.replace(i),r||i.select(0,0)};const dt=t=>(e,n,o,r)=>{const i=e.getPreviousSibling(),s=e.getNextSibling(),l=$("check"===t?"x"===o[3]:void 0),c="bullet"===t||"check"===t?o[0].trim()[0]:void 0;if(C(s)&&s.getListType()===t){c&&u(s,gt,c);const t=s.getFirstChild();null!==t?t.insertBefore(l):s.append(l),e.remove()}else if(C(i)&&i.getListType()===t)c&&u(i,gt,c),i.append(l),e.remove();else{const n=b(t,"number"===t?Number(o[2]):void 0);c&&u(n,gt,c),n.append(l),e.replace(n)}l.append(...n),r||l.select(0,0);const f=function(t){const e=t.match(/\t/g),n=t.match(/ /g);let o=0;return e&&(o+=e.length),n&&(o+=Math.floor(n.length/4)),o}(o[1]);f&&l.setIndent(f)},ht=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(E(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(C(t)){o.push(ht(t,e,n+1));continue}}const r=" ".repeat(4*n),l=t.getListType(),c=g(t,gt),f="number"===l?`${t.getStart()+i}. `:"check"===l?`${c} [${s.getChecked()?"x":" "}] `:c+" ";o.push(r+f+e(s)),i++}return o.join("\n")},mt={dependencies:[F],export:(t,e)=>{if(!k(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:it,replace:pt(t=>{const e="h"+t[1].length;return N(e)}),type:"element"},xt={dependencies:[I],export:(t,e)=>{if(!S(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:st,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(S(n))return n.splice(n.getChildrenSize(),0,[f(),...e]),void t.remove()}const r=w();r.append(...e),t.replace(r),o||r.select(0,0)},type:"element"},Tt={dependencies:[P],export:t=>{if(!B(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExpEnd:{optional:!0,regExp:ct},regExpStart:lt,replace:(t,e,n,o,r,i)=>{let s,c;if(!e&&r){if(1===r.length)o?(s=R(),c=n[1]+r[0]):(s=R(n[1]),c=r[0].startsWith(" ")?r[0].slice(1):r[0]);else{if(s=R(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&&pt(t=>R(t?t[1]:void 0))(t,e,n,i)},type:"multiline-element"},Ct={dependencies:[y,v],export:(t,e)=>C(t)?ht(t,e,0):null,regExp:ot,replace:dt("bullet"),type:"element"},Et={dependencies:[y,v],export:(t,e)=>C(t)?ht(t,e,0):null,regExp:rt,replace:dt("check"),type:"element"},yt={dependencies:[y,v],export:(t,e)=>C(t)?ht(t,e,0):null,regExp:nt,replace:dt("number"),type:"element"},vt={format:["code"],tag:"`",type:"text-format"},$t={format:["highlight"],tag:"==",type:"text-format"},bt={format:["bold","italic"],tag:"***",type:"text-format"},St={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},Ft={format:["bold"],tag:"**",type:"text-format"},It={format:["bold"],intraword:!1,tag:"__",type:"text-format"},Nt={format:["strikethrough"],tag:"~~",type:"text-format"},kt={format:["italic"],tag:"*",type:"text-format"},wt={format:["italic"],intraword:!1,tag:"_",type:"text-format"},Lt={dependencies:[_],export:(t,e,n)=>{if(!j(t)||A(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=M(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"},Pt=[mt,xt,Ct,yt],Rt=[Tt],Bt=[vt,bt,St,Ft,It,$t,kt,wt,Nt],_t=[Lt],Mt=[...Pt,...Rt,...Bt,..._t];function jt(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(At(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function At(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function zt(t,n=Mt){const o=U(n),r=z(o.textFormat,({tag:t})=>t[t.length-1]),l=z(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)||et(173,e.getType())}}const c=(t,n,c)=>{(function(t,e,n,o){const r=t.getParent();if(!m(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(!m(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&&!At(r,a,o,0,f))continue;if(" "===r[a-1])continue;const u=r[l+1];if(!1===n.intraword&&u&&!W.test(u))continue;const g=t;let p=g,d=jt(r,a,o),m=p;for(;d<0&&(m=m.getPreviousSibling())&&!i(m);)if(e(m)){if(m.hasFormat("code"))continue;const t=m.getTextContent();p=m,d=jt(t,t.length,o)}if(d<0)continue;if(p===g&&d+f===a)continue;const C=p.getTextContent();if(d>0&&C[d-1]===c)continue;const E=C[d-1];if(!1===n.intraword&&E&&!W.test(E))continue;const y=g.getTextContent(),v=y.slice(0,a)+y.slice(l+1);g.setTextContent(v);const $=p===g?v:C;p.setTextContent($.slice(0,d)+$.slice(d+f));const b=s(),S=x();T(S);const F=l-f*(p===g?2:1)+1;S.anchor.set(p.__key,d,"text"),S.focus.set(g.__key,F,"text");for(const t of n.format)S.hasFormat(t)||S.formatText(t);S.anchor.set(S.focus.key,S.focus.offset,S.focus.type);for(const t of n.format)S.hasFormat(t)&&S.toggleFormat(t);return h(b)&&(S.format=b.format),!0}}(n,c,r)};return t.registerUpdateListener(({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:i})=>{if(n.has(p)||n.has(d))return;if(t.isComposing())return;const l=r.read(s),f=i.read(s);if(!h(f)||!h(l)||!l.isCollapsed()||l.is(f))return;const a=l.anchor.key,u=l.anchor.offset,g=r._nodeMap.get(a);!e(g)||!o.has(a)||1!==u&&u>f.anchor.offset+1||t.update(()=>{if(!V(g))return;const t=g.getParent();null===t||B(t)||c(t,g,l.anchor.offset)})})}function Ut(t,e=Mt,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];ft.test(i)?r.push(i):lt.test(i)||ct.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||it.test(s)||it.test(i)||st.test(i)||nt.test(i)||ot.test(i)||rt.test(i)||at.test(i)||ut.test(i)||!e?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(t,r);return Y(e,o)(i,n)}function Wt(t=Mt,e,o=!1){const r=function(t,e=!1){const o=U(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=q(n,r,s,o.textMatch);null!=c&&e.push(i&&t>0&&!O(n)&&!O(l[t-1])?"\n".concat(c):c)}return e.join("\n")}}(t,o);return r(e)}export{Ut as $convertFromMarkdownString,Wt as $convertToMarkdownString,bt as BOLD_ITALIC_STAR,St as BOLD_ITALIC_UNDERSCORE,Ft as BOLD_STAR,It as BOLD_UNDERSCORE,Et as CHECK_LIST,Tt as CODE,Pt as ELEMENT_TRANSFORMERS,mt as HEADING,$t as HIGHLIGHT,vt as INLINE_CODE,kt as ITALIC_STAR,wt as ITALIC_UNDERSCORE,Lt as LINK,Rt as MULTILINE_ELEMENT_TRANSFORMERS,yt as ORDERED_LIST,xt as QUOTE,Nt as STRIKETHROUGH,Bt as TEXT_FORMAT_TRANSFORMERS,_t as TEXT_MATCH_TRANSFORMERS,Mt as TRANSFORMERS,Ct as UNORDERED_LIST,zt as registerMarkdownShortcuts};
|
|
@@ -129,6 +129,7 @@ export type TextMatchTransformer = Readonly<{
|
|
|
129
129
|
trigger?: string;
|
|
130
130
|
type: 'text-match';
|
|
131
131
|
}>;
|
|
132
|
+
export declare const listMarkerState: import("lexical").StateConfig<"mdListMarker", string>;
|
|
132
133
|
export declare const HEADING: ElementTransformer;
|
|
133
134
|
export declare const QUOTE: ElementTransformer;
|
|
134
135
|
export declare const CODE: MultilineElementTransformer;
|
package/package.json
CHANGED
|
@@ -8,17 +8,17 @@
|
|
|
8
8
|
"markdown"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.36.3-nightly.
|
|
11
|
+
"version": "0.36.3-nightly.20251006.0",
|
|
12
12
|
"main": "LexicalMarkdown.js",
|
|
13
13
|
"types": "index.d.ts",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@lexical/code": "0.36.3-nightly.
|
|
16
|
-
"@lexical/link": "0.36.3-nightly.
|
|
17
|
-
"@lexical/list": "0.36.3-nightly.
|
|
18
|
-
"@lexical/rich-text": "0.36.3-nightly.
|
|
19
|
-
"@lexical/text": "0.36.3-nightly.
|
|
20
|
-
"@lexical/utils": "0.36.3-nightly.
|
|
21
|
-
"lexical": "0.36.3-nightly.
|
|
15
|
+
"@lexical/code": "0.36.3-nightly.20251006.0",
|
|
16
|
+
"@lexical/link": "0.36.3-nightly.20251006.0",
|
|
17
|
+
"@lexical/list": "0.36.3-nightly.20251006.0",
|
|
18
|
+
"@lexical/rich-text": "0.36.3-nightly.20251006.0",
|
|
19
|
+
"@lexical/text": "0.36.3-nightly.20251006.0",
|
|
20
|
+
"@lexical/utils": "0.36.3-nightly.20251006.0",
|
|
21
|
+
"lexical": "0.36.3-nightly.20251006.0"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|