@lobehub/editor 1.10.0 → 1.12.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.
Files changed (48) hide show
  1. package/es/editor-kernel/inode/helper.d.ts +9 -6
  2. package/es/editor-kernel/inode/helper.js +27 -0
  3. package/es/editor-kernel/inode/text-node.d.ts +2 -9
  4. package/es/plugins/code/plugin/index.d.ts +1 -1
  5. package/es/plugins/code/plugin/index.js +12 -1
  6. package/es/plugins/codeblock/command/index.d.ts +6 -0
  7. package/es/plugins/codeblock/command/index.js +1 -0
  8. package/es/plugins/codeblock/plugin/CodeHighlighterShiki.d.ts +7 -0
  9. package/es/plugins/codeblock/plugin/CodeHighlighterShiki.js +59 -2
  10. package/es/plugins/codeblock/plugin/FacadeShiki.d.ts +8 -1
  11. package/es/plugins/codeblock/plugin/FacadeShiki.js +95 -6
  12. package/es/plugins/codeblock/plugin/index.js +74 -29
  13. package/es/plugins/common/data-source/json-data-source.d.ts +2 -2
  14. package/es/plugins/common/data-source/json-data-source.js +2 -10
  15. package/es/plugins/common/index.d.ts +1 -1
  16. package/es/plugins/common/index.js +1 -1
  17. package/es/plugins/common/node/cursor.d.ts +3 -1
  18. package/es/plugins/common/node/cursor.js +9 -0
  19. package/es/plugins/common/plugin/index.d.ts +1 -1
  20. package/es/plugins/common/plugin/index.js +28 -1
  21. package/es/plugins/common/plugin/mdReader.d.ts +2 -0
  22. package/es/plugins/common/plugin/mdReader.js +59 -0
  23. package/es/plugins/common/react/ReactPlainText.d.ts +1 -1
  24. package/es/plugins/common/utils/index.d.ts +2 -2
  25. package/es/plugins/hr/plugin/index.js +26 -22
  26. package/es/plugins/link/plugin/index.js +42 -26
  27. package/es/plugins/list/plugin/index.js +121 -63
  28. package/es/plugins/list/utils/index.d.ts +3 -3
  29. package/es/plugins/markdown/data-source/markdown/parse.d.ts +10 -0
  30. package/es/plugins/markdown/data-source/markdown/parse.js +82 -0
  31. package/es/plugins/markdown/data-source/markdown/supersub.d.ts +1 -0
  32. package/es/plugins/markdown/data-source/markdown/supersub.js +14 -0
  33. package/es/plugins/markdown/data-source/markdown-data-source.d.ts +4 -4
  34. package/es/plugins/markdown/data-source/markdown-data-source.js +8 -2
  35. package/es/plugins/markdown/plugin/index.js +135 -2
  36. package/es/plugins/markdown/service/shortcut.d.ts +15 -85
  37. package/es/plugins/markdown/service/shortcut.js +34 -293
  38. package/es/plugins/markdown/service/transformers.d.ts +60 -0
  39. package/es/plugins/markdown/service/transformers.js +286 -0
  40. package/es/plugins/markdown/utils/index.d.ts +45 -1
  41. package/es/plugins/markdown/utils/index.js +147 -1
  42. package/es/plugins/markdown/utils/logger.d.ts +7 -0
  43. package/es/plugins/markdown/utils/logger.js +2 -0
  44. package/es/plugins/math/plugin/index.js +64 -45
  45. package/es/plugins/table/plugin/index.js +71 -26
  46. package/es/react/hooks/useEditorState/index.js +43 -21
  47. package/es/types/global.d.ts +64 -0
  48. package/package.json +2 -1
@@ -1,4 +1,4 @@
1
- import { IEditorPluginConstructor } from "../../../types";
1
+ import type { IEditorPluginConstructor } from "../../../types";
2
2
  export interface CommonPluginOptions {
3
3
  enableHotkey?: boolean;
4
4
  theme?: {
@@ -34,6 +34,7 @@ import { patchBreakLine, registerBreakLineClick } from "../node/ElementDOMSlot";
34
34
  import { CursorNode, registerCursorNode } from "../node/cursor";
35
35
  import { createBlockNode } from "../utils";
36
36
  import { registerHeaderBackspace, registerLastElement, registerRichKeydown } from "./register";
37
+ import { registerMDReader } from "./mdReader";
37
38
  patchBreakLine();
38
39
  export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
39
40
  _inherits(CommonPlugin, _KernelPlugin);
@@ -67,7 +68,6 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
67
68
  }
68
69
  });
69
70
  }
70
- _this.registerMarkdown(kernel);
71
71
  return _this;
72
72
  }
73
73
  _createClass(CommonPlugin, [{
@@ -130,6 +130,14 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
130
130
  intraword: false,
131
131
  tag: '_',
132
132
  type: 'text-format'
133
+ }, {
134
+ format: ['superscript'],
135
+ tag: '^',
136
+ type: 'text-format'
137
+ }, {
138
+ format: ['subscript'],
139
+ tag: '~',
140
+ type: 'text-format'
133
141
  }]);
134
142
  markdownService.registerMarkdownWriter('paragraph', function (ctx) {
135
143
  ctx.wrap('', '\n\n');
@@ -188,6 +196,8 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
188
196
  var isItalic = node.hasFormat('italic');
189
197
  var isUnderline = node.hasFormat('underline');
190
198
  var isStrikethrough = node.hasFormat('strikethrough');
199
+ var isSuperscript = node.hasFormat('superscript');
200
+ var isSubscript = node.hasFormat('subscript');
191
201
  if (isBold) {
192
202
  ctx.appendLine('**');
193
203
  }
@@ -200,6 +210,12 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
200
210
  if (isUnderline) {
201
211
  ctx.appendLine('<ins>');
202
212
  }
213
+ if (isSuperscript) {
214
+ ctx.appendLine('^');
215
+ }
216
+ if (isSubscript) {
217
+ ctx.appendLine('~');
218
+ }
203
219
  var textContent = node.getTextContent();
204
220
  var res = textContent.match(/\s+$/);
205
221
  var tailSpace = '';
@@ -209,6 +225,12 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
209
225
  var append = textContent.trimEnd();
210
226
  var lastChar = append.at(-1);
211
227
  ctx.appendLine(append);
228
+ if (isSubscript) {
229
+ ctx.appendLine('~');
230
+ }
231
+ if (isSuperscript) {
232
+ ctx.appendLine('^');
233
+ }
212
234
  if (isUnderline) {
213
235
  ctx.appendLine('</ins>');
214
236
  }
@@ -233,6 +255,10 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
233
255
  // In markdown, soft line breaks are represented as two spaces followed by a newline
234
256
  ctx.appendLine(' \n');
235
257
  });
258
+
259
+ // 注册 markdown reader
260
+ //
261
+ registerMDReader(markdownService);
236
262
  }
237
263
  }, {
238
264
  key: "onInit",
@@ -241,6 +267,7 @@ export var CommonPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
241
267
  this.registerClears(registerRichText(editor), registerDragonSupport(editor), registerHistory(editor, createEmptyHistoryState(), 300), registerHeaderBackspace(editor), registerRichKeydown(editor, this.kernel, {
242
268
  enableHotkey: (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.enableHotkey
243
269
  }), registerCommands(editor), registerBreakLineClick(editor), registerCursorNode(editor), registerLastElement(editor));
270
+ this.registerMarkdown(this.kernel);
244
271
  }
245
272
  }, {
246
273
  key: "destroy",
@@ -0,0 +1,2 @@
1
+ import type { IMarkdownShortCutService } from "../../markdown";
2
+ export declare function registerMDReader(markdownService: IMarkdownShortCutService): void;
@@ -0,0 +1,59 @@
1
+ import { IS_BOLD, IS_ITALIC, IS_STRIKETHROUGH, IS_SUBSCRIPT, IS_SUPERSCRIPT } from 'lexical';
2
+ import { INodeHelper } from "../../../editor-kernel/inode/helper";
3
+ export function registerMDReader(markdownService) {
4
+ markdownService.registerMarkdownReader('blockquote', function (node, children) {
5
+ return INodeHelper.createElementNode('quote', {
6
+ children: children,
7
+ direction: 'ltr',
8
+ format: '',
9
+ indent: 0,
10
+ version: 1
11
+ });
12
+ });
13
+
14
+ // strong italic del
15
+ markdownService.registerMarkdownReader('strong', function (node, children) {
16
+ return children.map(function (child) {
17
+ if (INodeHelper.isTextNode(child)) {
18
+ child.format = (child.format || 0) | IS_BOLD;
19
+ }
20
+ return child;
21
+ });
22
+ });
23
+
24
+ // strong italic del
25
+ markdownService.registerMarkdownReader('emphasis', function (node, children) {
26
+ return children.map(function (child) {
27
+ if (INodeHelper.isTextNode(child)) {
28
+ child.format = (child.format || 0) | IS_ITALIC;
29
+ }
30
+ return child;
31
+ });
32
+ });
33
+
34
+ // strong italic del
35
+ markdownService.registerMarkdownReader('delete', function (node, children) {
36
+ return children.map(function (child) {
37
+ if (INodeHelper.isTextNode(child)) {
38
+ child.format = (child.format || 0) | IS_STRIKETHROUGH;
39
+ }
40
+ return child;
41
+ });
42
+ });
43
+ markdownService.registerMarkdownReader('subscript', function (node, children) {
44
+ return children.map(function (child) {
45
+ if (INodeHelper.isTextNode(child)) {
46
+ child.format = (child.format || 0) | IS_SUBSCRIPT;
47
+ }
48
+ return child;
49
+ });
50
+ });
51
+ markdownService.registerMarkdownReader('superscript', function (node, children) {
52
+ return children.map(function (child) {
53
+ if (INodeHelper.isTextNode(child)) {
54
+ child.format = (child.format || 0) | IS_SUPERSCRIPT;
55
+ }
56
+ return child;
57
+ });
58
+ });
59
+ }
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
- import { ReactPlainTextProps } from './type';
2
+ import type { ReactPlainTextProps } from './type';
3
3
  declare const ReactPlainText: import("react").NamedExoticComponent<ReactPlainTextProps>;
4
4
  export default ReactPlainText;
@@ -1,5 +1,5 @@
1
- import { ElementNode } from 'lexical';
2
- import { ElementTransformer } from "../../markdown/service/shortcut";
1
+ import type { ElementNode } from 'lexical';
2
+ import type { ElementTransformer } from "../../markdown/service/transformers";
3
3
  export declare const createBlockNode: (createNode: (match: Array<string>, parentNode: ElementNode) => ElementNode) => ElementTransformer['replace'];
4
4
  /**
5
5
  * Returns the root's text content.
@@ -13,6 +13,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
13
13
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
14
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
15
15
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
16
+ import { INodeHelper } from "../../../editor-kernel/inode/helper";
16
17
  import { KernelPlugin } from "../../../editor-kernel/plugin";
17
18
  import { IMarkdownShortCutService } from "../../markdown";
18
19
  import { registerHorizontalRuleCommand } from "../command";
@@ -24,7 +25,6 @@ export var HRPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
24
25
  _inherits(HRPlugin, _KernelPlugin);
25
26
  var _super = _createSuper(HRPlugin);
26
27
  function HRPlugin(kernel, config) {
27
- var _kernel$requireServic, _kernel$requireServic2;
28
28
  var _this;
29
29
  _classCallCheck(this, HRPlugin);
30
30
  _this = _super.call(this);
@@ -37,33 +37,37 @@ export var HRPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
37
37
  _this.registerDecorator(kernel, 'horizontalrule', function (node, editor) {
38
38
  return config !== null && config !== void 0 && config.decorator ? config.decorator(node, editor) : null;
39
39
  });
40
- (_kernel$requireServic = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic === void 0 || _kernel$requireServic.registerMarkdownShortCut({
41
- regExp: /^(---|\*\*\*|___)$/,
42
- replace: function replace(parentNode, _1, _2, isImport) {
43
- var line = $createHorizontalRuleNode();
44
-
45
- // TODO: Get rid of isImport flag
46
- if (isImport || parentNode.getNextSibling()) {
47
- parentNode.replace(line);
48
- } else {
49
- parentNode.insertBefore(line);
50
- }
51
- line.selectNext();
52
- },
53
- trigger: 'enter',
54
- type: 'element'
55
- });
56
- (_kernel$requireServic2 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic2 === void 0 || _kernel$requireServic2.registerMarkdownWriter(HorizontalRuleNode.getType(), function (ctx, node) {
57
- if ($isHorizontalRuleNode(node)) {
58
- ctx.appendLine('---\n\n');
59
- }
60
- });
61
40
  return _this;
62
41
  }
63
42
  _createClass(HRPlugin, [{
64
43
  key: "onInit",
65
44
  value: function onInit(editor) {
45
+ var _this$kernel$requireS, _this$kernel$requireS2, _this$kernel$requireS3;
66
46
  this.register(registerHorizontalRuleCommand(editor));
47
+ (_this$kernel$requireS = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS === void 0 || _this$kernel$requireS.registerMarkdownShortCut({
48
+ regExp: /^(---|\*\*\*|___)$/,
49
+ replace: function replace(parentNode, _1, _2, isImport) {
50
+ var line = $createHorizontalRuleNode();
51
+
52
+ // TODO: Get rid of isImport flag
53
+ if (isImport || parentNode.getNextSibling()) {
54
+ parentNode.replace(line);
55
+ } else {
56
+ parentNode.insertBefore(line);
57
+ }
58
+ line.selectNext();
59
+ },
60
+ trigger: 'enter',
61
+ type: 'element'
62
+ });
63
+ (_this$kernel$requireS2 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS2 === void 0 || _this$kernel$requireS2.registerMarkdownWriter(HorizontalRuleNode.getType(), function (ctx, node) {
64
+ if ($isHorizontalRuleNode(node)) {
65
+ ctx.appendLine('---\n\n');
66
+ }
67
+ });
68
+ (_this$kernel$requireS3 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS3 === void 0 || _this$kernel$requireS3.registerMarkdownReader('thematicBreak', function () {
69
+ return INodeHelper.createElementNode('horizontalrule', {});
70
+ });
67
71
  }
68
72
  }]);
69
73
  return HRPlugin;
@@ -20,6 +20,7 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
20
20
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
21
21
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
22
22
  import { $createTextNode, COMMAND_PRIORITY_NORMAL, PASTE_COMMAND } from 'lexical';
23
+ import { INodeHelper } from "../../../editor-kernel/inode/helper";
23
24
  import { KernelPlugin } from "../../../editor-kernel/plugin";
24
25
  import { IMarkdownShortCutService } from "../../markdown";
25
26
  import { INSERT_LINK_COMMAND, registerLinkCommand } from "../command";
@@ -29,7 +30,6 @@ export var LinkPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
29
30
  _inherits(LinkPlugin, _KernelPlugin);
30
31
  var _super = _createSuper(LinkPlugin);
31
32
  function LinkPlugin(kernel, config) {
32
- var _kernel$requireServic, _kernel$requireServic2;
33
33
  var _this;
34
34
  _classCallCheck(this, LinkPlugin);
35
35
  _this = _super.call(this);
@@ -44,30 +44,6 @@ export var LinkPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
44
44
  if (config !== null && config !== void 0 && config.linkRegex) {
45
45
  _this.linkRegex = config.linkRegex;
46
46
  }
47
- (_kernel$requireServic = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic === void 0 || _kernel$requireServic.registerMarkdownShortCut({
48
- regExp: /\[([^[]+)]\(([^\s()]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)\s?$/,
49
- replace: function replace(textNode, match) {
50
- var _match = _slicedToArray(match, 4),
51
- linkText = _match[1],
52
- linkUrl = _match[2],
53
- linkTitle = _match[3];
54
- var linkNode = $createLinkNode(linkUrl, {
55
- title: linkTitle
56
- });
57
- var linkTextNode = $createTextNode(linkText);
58
- linkTextNode.setFormat(textNode.getFormat());
59
- linkNode.append(linkTextNode);
60
- textNode.replace(linkNode);
61
- return linkTextNode;
62
- },
63
- trigger: ')',
64
- type: 'text-match'
65
- });
66
- (_kernel$requireServic2 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic2 === void 0 || _kernel$requireServic2.registerMarkdownWriter(LinkNode.getType(), function (ctx, node) {
67
- if ($isLinkNode(node)) {
68
- ctx.wrap('[', "](".concat(node.getURL(), ")"));
69
- }
70
- });
71
47
  return _this;
72
48
  }
73
49
  _createClass(LinkPlugin, [{
@@ -76,7 +52,10 @@ export var LinkPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
76
52
  var _this$config,
77
53
  _this$config2,
78
54
  _this$config3,
79
- _this2 = this;
55
+ _this2 = this,
56
+ _this$kernel$requireS,
57
+ _this$kernel$requireS2,
58
+ _this$kernel$requireS3;
80
59
  this.register(registerLinkCommand(editor));
81
60
  this.register(registerLinkCommands(editor, this.kernel, {
82
61
  attributes: (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.attributes,
@@ -98,6 +77,43 @@ export var LinkPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
98
77
  }
99
78
  return false;
100
79
  }, COMMAND_PRIORITY_NORMAL));
80
+ (_this$kernel$requireS = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS === void 0 || _this$kernel$requireS.registerMarkdownShortCut({
81
+ regExp: /\[([^[]+)]\(([^\s()]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)\s?$/,
82
+ replace: function replace(textNode, match) {
83
+ var _match = _slicedToArray(match, 4),
84
+ linkText = _match[1],
85
+ linkUrl = _match[2],
86
+ linkTitle = _match[3];
87
+ var linkNode = $createLinkNode(linkUrl, {
88
+ title: linkTitle
89
+ });
90
+ var linkTextNode = $createTextNode(linkText);
91
+ linkTextNode.setFormat(textNode.getFormat());
92
+ linkNode.append(linkTextNode);
93
+ textNode.replace(linkNode);
94
+ return linkTextNode;
95
+ },
96
+ trigger: ')',
97
+ type: 'text-match'
98
+ });
99
+ (_this$kernel$requireS2 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS2 === void 0 || _this$kernel$requireS2.registerMarkdownWriter(LinkNode.getType(), function (ctx, node) {
100
+ if ($isLinkNode(node)) {
101
+ ctx.wrap('[', "](".concat(node.getURL(), ")"));
102
+ }
103
+ });
104
+ (_this$kernel$requireS3 = this.kernel.requireService(IMarkdownShortCutService)) === null || _this$kernel$requireS3 === void 0 || _this$kernel$requireS3.registerMarkdownReader('link', function (node, children) {
105
+ var linkNode = INodeHelper.createElementNode('link', {
106
+ children: children,
107
+ direction: 'ltr',
108
+ format: '',
109
+ indent: 0,
110
+ title: node.title || undefined,
111
+ type: 'link',
112
+ url: node.url || '',
113
+ version: 1
114
+ });
115
+ return [linkNode];
116
+ });
101
117
  }
102
118
  }]);
103
119
  return LinkPlugin;
@@ -17,6 +17,7 @@ import { $isListItemNode, $isListNode, ListItemNode, ListNode, registerList, reg
17
17
  import { $getNearestNodeOfType } from '@lexical/utils';
18
18
  import { cx } from 'antd-style';
19
19
  import { $isRootNode } from 'lexical';
20
+ import { INodeHelper } from "../../../editor-kernel/inode/helper";
20
21
  import { KernelPlugin } from "../../../editor-kernel/plugin";
21
22
  import { IMarkdownShortCutService } from "../../markdown";
22
23
  import { listReplace } from "../utils";
@@ -29,7 +30,6 @@ export var ListPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
29
30
  _inherits(ListPlugin, _KernelPlugin);
30
31
  var _super = _createSuper(ListPlugin);
31
32
  function ListPlugin(kernel, config) {
32
- var _kernel$requireServic, _kernel$requireServic2, _kernel$requireServic3, _kernel$requireServic4, _kernel$requireServic5;
33
33
  var _this;
34
34
  _classCallCheck(this, ListPlugin);
35
35
  _this = _super.call(this);
@@ -53,68 +53,6 @@ export var ListPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
53
53
  ul: cx('editor_listUnordered', config === null || config === void 0 ? void 0 : config.theme)
54
54
  }
55
55
  });
56
- (_kernel$requireServic = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic === void 0 || _kernel$requireServic.registerMarkdownShortCut({
57
- regExp: UNORDERED_LIST_REGEX,
58
- replace: listReplace('bullet'),
59
- type: 'element'
60
- });
61
- (_kernel$requireServic2 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic2 === void 0 || _kernel$requireServic2.registerMarkdownShortCut({
62
- regExp: ORDERED_LIST_REGEX,
63
- replace: listReplace('number'),
64
- type: 'element'
65
- });
66
- (_kernel$requireServic3 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic3 === void 0 || _kernel$requireServic3.registerMarkdownShortCut({
67
- regExp: CHECK_LIST_REGEX,
68
- replace: listReplace('check'),
69
- type: 'element'
70
- });
71
- (_kernel$requireServic4 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic4 === void 0 || _kernel$requireServic4.registerMarkdownWriter(ListNode.getType(), function (ctx, node) {
72
- if ($isListNode(node)) {
73
- ctx.wrap('', '\n');
74
- }
75
- });
76
- var getLevel = function getLevel(node) {
77
- if (!node) return 0;
78
- if ($isRootNode(node.getParent())) {
79
- return 0;
80
- }
81
- var parent = node.getParent();
82
- if (!parent) {
83
- return 0;
84
- }
85
- return getLevel($getNearestNodeOfType(parent, ListNode)) + 1;
86
- };
87
- (_kernel$requireServic5 = kernel.requireService(IMarkdownShortCutService)) === null || _kernel$requireServic5 === void 0 || _kernel$requireServic5.registerMarkdownWriter(ListItemNode.getType(), function (ctx, node) {
88
- var parent = node.getParent();
89
- if ($isListItemNode(node) && $isListNode(parent)) {
90
- if ($isListNode(node.getFirstChild())) {
91
- return;
92
- }
93
- var level = getLevel(parent);
94
- var prefix = ' '.repeat(level);
95
- switch (parent.getListType()) {
96
- case 'bullet':
97
- {
98
- ctx.wrap(prefix + '- ', '\n');
99
- break;
100
- }
101
- case 'number':
102
- {
103
- ctx.wrap("".concat(prefix).concat(node.getValue(), ". "), '\n');
104
- break;
105
- }
106
- case 'check':
107
- {
108
- ctx.wrap("".concat(prefix, "- [").concat(node.getChecked() ? 'x' : ' ', "] "), '\n');
109
- break;
110
- }
111
- default:
112
- {
113
- break;
114
- }
115
- }
116
- }
117
- });
118
56
  return _this;
119
57
  }
120
58
  _createClass(ListPlugin, [{
@@ -127,6 +65,126 @@ export var ListPlugin = (_class = /*#__PURE__*/function (_KernelPlugin) {
127
65
  this.register(registerListCommands(editor, this.kernel, {
128
66
  enableHotkey: (_this$config = this.config) === null || _this$config === void 0 ? void 0 : _this$config.enableHotkey
129
67
  }));
68
+ this.registerMarkdown();
69
+ }
70
+ }, {
71
+ key: "registerMarkdown",
72
+ value: function registerMarkdown() {
73
+ var markdownService = this.kernel.requireService(IMarkdownShortCutService);
74
+ if (!markdownService) {
75
+ return;
76
+ }
77
+ markdownService.registerMarkdownShortCut({
78
+ regExp: UNORDERED_LIST_REGEX,
79
+ replace: listReplace('bullet'),
80
+ type: 'element'
81
+ });
82
+ markdownService.registerMarkdownShortCut({
83
+ regExp: ORDERED_LIST_REGEX,
84
+ replace: listReplace('number'),
85
+ type: 'element'
86
+ });
87
+ markdownService.registerMarkdownShortCut({
88
+ regExp: CHECK_LIST_REGEX,
89
+ replace: listReplace('check'),
90
+ type: 'element'
91
+ });
92
+ markdownService.registerMarkdownWriter(ListNode.getType(), function (ctx, node) {
93
+ if ($isListNode(node)) {
94
+ ctx.wrap('', '\n');
95
+ }
96
+ });
97
+ var getLevel = function getLevel(node) {
98
+ if (!node) return 0;
99
+ if ($isRootNode(node.getParent())) {
100
+ return 0;
101
+ }
102
+ var parent = node.getParent();
103
+ if (!parent) {
104
+ return 0;
105
+ }
106
+ return getLevel($getNearestNodeOfType(parent, ListNode)) + 1;
107
+ };
108
+ markdownService.registerMarkdownWriter(ListItemNode.getType(), function (ctx, node) {
109
+ var parent = node.getParent();
110
+ if ($isListItemNode(node) && $isListNode(parent)) {
111
+ if ($isListNode(node.getFirstChild())) {
112
+ return;
113
+ }
114
+ var level = getLevel(parent);
115
+ var prefix = ' '.repeat(level);
116
+ switch (parent.getListType()) {
117
+ case 'bullet':
118
+ {
119
+ ctx.wrap(prefix + '- ', '\n');
120
+ break;
121
+ }
122
+ case 'number':
123
+ {
124
+ ctx.wrap("".concat(prefix).concat(node.getValue(), ". "), '\n');
125
+ break;
126
+ }
127
+ case 'check':
128
+ {
129
+ ctx.wrap("".concat(prefix, "- [").concat(node.getChecked() ? 'x' : ' ', "] "), '\n');
130
+ break;
131
+ }
132
+ default:
133
+ {
134
+ break;
135
+ }
136
+ }
137
+ }
138
+ });
139
+ markdownService.registerMarkdownReader('list', function (node, children) {
140
+ var _node$children, _node$children2;
141
+ var isCheck = ((_node$children = node.children) === null || _node$children === void 0 || (_node$children = _node$children[0]) === null || _node$children === void 0 ? void 0 : _node$children.type) === 'listItem' && typeof ((_node$children2 = node.children) === null || _node$children2 === void 0 || (_node$children2 = _node$children2[0]) === null || _node$children2 === void 0 ? void 0 : _node$children2.checked) === 'boolean';
142
+ var start = node.start || 1;
143
+ return INodeHelper.createElementNode('list', {
144
+ children: children.map(function (v) {
145
+ if (v.type === 'listitem') {
146
+ // @ts-expect-error not error
147
+ v.value = start++;
148
+ }
149
+ return v;
150
+ }),
151
+ direction: 'ltr',
152
+ format: '',
153
+ indent: 0,
154
+ listType: isCheck ? 'check' : node.ordered ? 'number' : 'bullet',
155
+ start: node.start || 1,
156
+ tag: node.ordered ? 'ol' : 'ul',
157
+ version: 1
158
+ });
159
+ });
160
+ markdownService.registerMarkdownReader('listItem', function (node, children, index) {
161
+ return children.map(function (v) {
162
+ if (v.type === 'paragraph') {
163
+ return INodeHelper.createElementNode('listitem', {
164
+ checked: typeof node.checked === 'boolean' ? node.checked : undefined,
165
+ // @ts-expect-error not error
166
+ children: v.children,
167
+ direction: 'ltr',
168
+ format: '',
169
+ indent: 0,
170
+ type: 'listitem',
171
+ value: index + 1,
172
+ version: 1
173
+ });
174
+ } else if (v.type === 'list') {
175
+ return INodeHelper.createElementNode('listitem', {
176
+ children: [v],
177
+ direction: 'ltr',
178
+ format: '',
179
+ indent: 0,
180
+ type: 'listitem',
181
+ value: index + 1,
182
+ version: 1
183
+ });
184
+ }
185
+ throw new Error('ListItem node children must be paragraph or list node ' + v.type);
186
+ });
187
+ });
130
188
  }
131
189
  }]);
132
190
  return ListPlugin;
@@ -1,5 +1,5 @@
1
- import { ListType } from '@lexical/list';
2
- import { RangeSelection } from 'lexical';
3
- import { ElementTransformer } from "../../markdown/service/shortcut";
1
+ import type { ListType } from '@lexical/list';
2
+ import type { RangeSelection } from 'lexical';
3
+ import type { ElementTransformer } from "../../markdown/service/transformers";
4
4
  export declare const listReplace: (listType: ListType) => ElementTransformer['replace'];
5
5
  export declare function $indentOverTab(selection: RangeSelection): boolean;
@@ -0,0 +1,10 @@
1
+ import type { PhrasingContent, RootContent } from 'mdast';
2
+ import type { IElementNode, INode, IRootNode, ITextNode } from "../../../../editor-kernel/inode";
3
+ export type MarkdownReadNode = INode | ITextNode | IElementNode;
4
+ export type MarkdownNode = RootContent | PhrasingContent;
5
+ export type TransformerRecord = {
6
+ [K in MarkdownNode['type']]?: (node: Extract<MarkdownNode, {
7
+ type: K;
8
+ }>, children: MarkdownReadNode[], index: number) => MarkdownReadNode | MarkdownReadNode[];
9
+ };
10
+ export declare function parseMarkdownToLexical(markdown: string, markdownReaders?: TransformerRecord): IRootNode;
@@ -0,0 +1,82 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
+ import { remark } from 'remark';
8
+ import remarkGfm from 'remark-gfm';
9
+ import remarkMath from 'remark-math';
10
+ import { INodeHelper } from "../../../../editor-kernel/inode/helper";
11
+ import { logger } from "../../utils/logger";
12
+ import remarkSupersub from "./supersub";
13
+
14
+ // 使用条件类型确保类型匹配
15
+
16
+ function convertMdastToLexical(node, index) {
17
+ var markdownReaders = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
18
+ switch (node.type) {
19
+ case 'root':
20
+ {
21
+ return _objectSpread(_objectSpread({}, INodeHelper.createRootNode()), {}, {
22
+ children: node.children.map(function (child, index) {
23
+ return convertMdastToLexical(child, index, markdownReaders);
24
+ }).filter(Boolean).flat()
25
+ });
26
+ }
27
+ case 'paragraph':
28
+ {
29
+ var paragraph = INodeHelper.createParagraph();
30
+ return _objectSpread(_objectSpread({}, paragraph), {}, {
31
+ children: node.children.map(function (child, index) {
32
+ return convertMdastToLexical(child, index, markdownReaders);
33
+ }).filter(Boolean).flat()
34
+ });
35
+ }
36
+ case 'heading':
37
+ {
38
+ // Create heading based on depth (h1-h6)
39
+ var headingType = "h".concat(Math.min(Math.max(node.depth, 1), 6));
40
+ return INodeHelper.createElementNode('heading', {
41
+ children: node.children.map(function (child, index) {
42
+ return convertMdastToLexical(child, index, markdownReaders);
43
+ }).filter(Boolean).flat(),
44
+ direction: 'ltr',
45
+ format: '',
46
+ indent: 0,
47
+ tag: headingType
48
+ });
49
+ }
50
+ case 'text':
51
+ {
52
+ return INodeHelper.createTextNode(node.value);
53
+ }
54
+ default:
55
+ {
56
+ if (markdownReaders[node.type]) {
57
+ var _markdownReaders$node;
58
+ var _children = [];
59
+ if ('children' in node && Array.isArray(node.children)) {
60
+ _children = node.children.map(function (child, index) {
61
+ return convertMdastToLexical(child, index, markdownReaders);
62
+ }).filter(Boolean).flat();
63
+ }
64
+ var inode = (_markdownReaders$node = markdownReaders[node.type]) === null || _markdownReaders$node === void 0 ? void 0 : _markdownReaders$node.call(markdownReaders, node, _children, index);
65
+ if (inode) {
66
+ return inode;
67
+ }
68
+ }
69
+
70
+ // Fallback for unsupported nodes
71
+ return null;
72
+ }
73
+ }
74
+ }
75
+ export function parseMarkdownToLexical(markdown) {
76
+ var markdownReaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
77
+ var ast = remark().use(remarkMath).use(remarkSupersub).use([[remarkGfm, {
78
+ singleTilde: false
79
+ }]]).parse(markdown);
80
+ logger.debug('Parsed MDAST:', ast);
81
+ return convertMdastToLexical(ast, 0, markdownReaders);
82
+ }
@@ -0,0 +1 @@
1
+ export default function remarkSupersub(): void;