cherry-markdown 0.5.15 → 0.5.16

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.
@@ -19322,8 +19322,6 @@
19322
19322
  return Editor;
19323
19323
  }();
19324
19324
 
19325
- var getPrototypeOf$5 = getPrototypeOf$1;
19326
-
19327
19325
  var $findIndex = arrayIteration.findIndex;
19328
19326
 
19329
19327
 
@@ -21003,6 +21001,14 @@
21003
21001
  return NestedError;
21004
21002
  }( /*#__PURE__*/_wrapNativeSuper(Error));
21005
21003
 
21004
+ /**
21005
+ * @typedef {import('~types/cherry').CherryOptions} CherryOptions
21006
+ * @typedef {import('~types/cherry').CherryEngineOptions} CherryEngineOptions
21007
+ * @typedef {import('~types/cherry').CustomSyntaxRegConfig} CustomSyntaxRegConfig
21008
+ * @typedef { (SyntaxBase | ParagraphBase) & { Cherry$$CUSTOM: true } } CustomSyntax
21009
+ * @typedef { (typeof SyntaxBase | typeof ParagraphBase) & { Cherry$$CUSTOM: true } } CustomSyntaxClass
21010
+ */
21011
+
21006
21012
  var WARN_DUPLICATED = -1;
21007
21013
  var WARN_NOT_A_VALID_HOOK = -2;
21008
21014
  /**
@@ -21023,12 +21029,63 @@
21023
21029
  Logger.warn(concat$2(_context3 = "Hook [".concat(objClass.toString(), "] ")).call(_context3, isNaN(index) ? '' : "at index [".concat(index, "] "), "is not a valid hook, and will not take effect."));
21024
21030
  }
21025
21031
  }
21032
+ /**
21033
+ * 是否一个合法的 HookClass
21034
+ * @param {any} HookClass
21035
+ * @returns { HookClass is (typeof SyntaxBase | typeof ParagraphBase) }
21036
+ */
21037
+
21026
21038
 
21027
21039
  function isHookValid(HookClass) {
21028
- return getPrototypeOf$5(HookClass) === SyntaxBase || getPrototypeOf$5(HookClass) === ParagraphBase;
21040
+ return isProtoOfSyntaxBase(HookClass) || isProtoOfParagraphBase(HookClass);
21029
21041
  }
21030
- /** @typedef {import('~types/cherry').CherryOptions} CherryOptions */
21042
+ /**
21043
+ * 传入的类是否 SyntaxBase 的子类
21044
+ * @param {any} value
21045
+ * @returns { value is typeof SyntaxBase }
21046
+ */
21047
+
21048
+
21049
+ function isProtoOfSyntaxBase(value) {
21050
+ return Object.prototype.isPrototypeOf.call(SyntaxBase, value);
21051
+ }
21052
+ /**
21053
+ * 传入的类是否 ParagraphBase 的子类
21054
+ * @param {any} value
21055
+ * @returns { value is typeof ParagraphBase }
21056
+ */
21031
21057
 
21058
+
21059
+ function isProtoOfParagraphBase(value) {
21060
+ return Object.prototype.isPrototypeOf.call(ParagraphBase, value);
21061
+ }
21062
+ /**
21063
+ * 是否一个配置型的自定义语法
21064
+ * @param {any} value
21065
+ * @returns { value is CustomSyntaxRegConfig }
21066
+ */
21067
+
21068
+
21069
+ function isCustomSyntaxConfig(value) {
21070
+ var syntaxClass =
21071
+ /** @type {any} */
21072
+
21073
+ /** @type {CustomSyntaxRegConfig} */
21074
+ value === null || value === void 0 ? void 0 : value.syntaxClass;
21075
+ return isProtoOfSyntaxBase(syntaxClass) || isProtoOfParagraphBase(syntaxClass);
21076
+ }
21077
+ /**
21078
+ * 是否一个已注册的自定义语法hook类
21079
+ * @param {any} value
21080
+ * @returns { value is CustomSyntaxClass }
21081
+ */
21082
+
21083
+
21084
+ function isRegisteredCustomSyntaxClass(value) {
21085
+ return isHookValid(value) &&
21086
+ /** @type {CustomSyntaxClass} */
21087
+ (value === null || value === void 0 ? void 0 : value.Cherry$$CUSTOM) === true;
21088
+ }
21032
21089
  /**
21033
21090
  * 语法注册中心
21034
21091
  */
@@ -21043,7 +21100,18 @@
21043
21100
  function HookCenter(hooksConfig, editorConfig) {
21044
21101
  _classCallCheck(this, HookCenter);
21045
21102
 
21046
- this.hookList = {};
21103
+ /**
21104
+ * @property
21105
+ * @type {Record<import('./SyntaxBase').HookType, SyntaxBase[]>} hookList hook 名称 -> hook 类型的映射
21106
+ */
21107
+ this.hookList =
21108
+ /** @type {any} */
21109
+ {};
21110
+ /**
21111
+ * @property
21112
+ * @type {Record<string, { type: import('./SyntaxBase').HookType }>} hookNameList hook 名称 -> hook 类型的映射
21113
+ */
21114
+
21047
21115
  this.hookNameList = {};
21048
21116
  $expectTarget(hooksConfig, Array);
21049
21117
  this.registerInternalHooks(hooksConfig, editorConfig);
@@ -21075,37 +21143,40 @@
21075
21143
  }
21076
21144
  /**
21077
21145
  * 注册第三方的语法hook
21078
- * @param {CherryOptions['engine']['customSyntax']} customHooks 用户传入的配置
21146
+ * @param {CherryEngineOptions['customSyntax']} customHooks 用户传入的配置
21079
21147
  * @param {Partial<CherryOptions>} editorConfig 编辑器配置
21080
21148
  */
21081
21149
 
21082
21150
  }, {
21083
21151
  key: "registerCustomHooks",
21084
21152
  value: function registerCustomHooks(customHooks, editorConfig) {
21085
- var _context4,
21086
- _this2 = this;
21153
+ var _this2 = this;
21087
21154
 
21088
- if (!customHooks || !keys$3(customHooks)) {
21155
+ if (!customHooks) {
21089
21156
  return;
21090
21157
  }
21091
21158
 
21092
- forEach$2(_context4 = keys$3(customHooks)).call(_context4, function (CustomHook) {
21093
- var paramType = _typeof(customHooks[CustomHook]);
21159
+ var hookNames = keys$3(customHooks);
21094
21160
 
21161
+ forEach$2(hookNames).call(hookNames, function (hookName) {
21162
+ /** @type {number} */
21095
21163
  var result;
21164
+ /** @type {typeof SyntaxBase} */
21165
+
21096
21166
  var HookClass;
21097
21167
  var customHookConfig = {};
21098
-
21099
- if (paramType === 'function') {
21100
- HookClass = customHooks[CustomHook];
21101
- } else if (paramType === 'object') {
21102
- HookClass = customHooks[CustomHook].syntaxClass;
21103
- customHookConfig.force = !!customHooks[CustomHook].force;
21104
-
21105
- if (customHooks[CustomHook].before) {
21106
- customHookConfig.before = customHooks[CustomHook].before;
21107
- } else if (customHooks[CustomHook].after) {
21108
- customHookConfig.after = customHooks[CustomHook].after;
21168
+ var hookClassOrConfig = customHooks[hookName];
21169
+
21170
+ if (isProtoOfSyntaxBase(hookClassOrConfig)) {
21171
+ HookClass = hookClassOrConfig;
21172
+ } else if (isCustomSyntaxConfig(hookClassOrConfig)) {
21173
+ HookClass = hookClassOrConfig.syntaxClass;
21174
+ customHookConfig.force = Boolean(hookClassOrConfig.force);
21175
+
21176
+ if (hookClassOrConfig.before) {
21177
+ customHookConfig.before = hookClassOrConfig.before;
21178
+ } else if (hookClassOrConfig.after) {
21179
+ customHookConfig.after = hookClassOrConfig.after;
21109
21180
  }
21110
21181
  } else {
21111
21182
  return;
@@ -21142,7 +21213,7 @@
21142
21213
  *
21143
21214
  * @param {((...args: any[]) => any) | typeof SyntaxBase} HookClass
21144
21215
  * @param {Partial<CherryOptions>} editorConfig
21145
- * @param {Record<string, any>} [customHookConfig]
21216
+ * @param {Omit<CustomSyntaxRegConfig, 'syntaxClass'>} [customHookConfig]
21146
21217
  * @returns
21147
21218
  */
21148
21219
 
@@ -21152,17 +21223,18 @@
21152
21223
  // filter Configs Here
21153
21224
  var externals = editorConfig.externals,
21154
21225
  engine = editorConfig.engine;
21155
- var syntax = engine.syntax,
21156
- customSyntax = engine.customSyntax;
21226
+ var syntax = engine.syntax;
21227
+ /** @type {SyntaxBase | CustomSyntax} */
21228
+
21157
21229
  var instance;
21230
+ /** @type {string} */
21231
+
21158
21232
  var hookName; // 首先校验Hook是否合法
21159
21233
 
21160
21234
  if (!isHookValid(HookClass)) {
21161
21235
  // 可能是一个function hook
21162
21236
  if (typeof HookClass === 'function') {
21163
- var funcHook =
21164
- /** @type {((...args: any[]) => any)}*/
21165
- HookClass;
21237
+ var funcHook = HookClass;
21166
21238
  instance = funcHook(editorConfig);
21167
21239
 
21168
21240
  if (!instance || !isHookValid(instance.constructor)) {
@@ -21174,36 +21246,33 @@
21174
21246
  return WARN_NOT_A_VALID_HOOK;
21175
21247
  }
21176
21248
  } else {
21177
- hookName =
21178
- /** @type {typeof SyntaxBase}*/
21179
- HookClass.HOOK_NAME;
21180
- var config = syntax[hookName] ? syntax[hookName] : customSyntax[hookName] ? customSyntax[hookName] : {};
21181
- instance = new
21182
- /** @type {typeof SyntaxBase}*/
21183
- HookClass({
21249
+ hookName = HookClass.HOOK_NAME; // TODO: 需要考虑自定义 hook 配置的传入方式
21250
+
21251
+ var config = (syntax === null || syntax === void 0 ? void 0 : syntax[hookName]) || {};
21252
+ instance = new HookClass({
21184
21253
  externals: externals,
21185
21254
  config: config,
21186
21255
  globalConfig: engine.global
21187
21256
  });
21188
- } // Skip Internal Hook
21257
+ } // TODO: 待校验是否需要跳过禁用的自定义 hook
21258
+ // Skip Disabled Internal Hooks
21189
21259
 
21190
21260
 
21191
- if (syntax[hookName] === false &&
21192
- /** @type {any}*/
21193
- HookClass.Cherry$$CUSTOM !== true) {
21261
+ if (syntax[hookName] === false && !isRegisteredCustomSyntaxClass(HookClass)) {
21194
21262
  return;
21195
- }
21263
+ } // 下面处理的都是 CustomSyntax
21264
+
21196
21265
 
21197
21266
  var hookType = instance.getType();
21198
21267
 
21199
21268
  if (this.hookNameList[hookName]) {
21200
- var _context5;
21269
+ var _context4;
21201
21270
 
21202
- if (!
21203
- /** @type {any}*/
21204
- HookClass.Cherry$$CUSTOM) {
21271
+ // 内置 hook 重名
21272
+ if (!isRegisteredCustomSyntaxClass(HookClass)) {
21205
21273
  return WARN_DUPLICATED;
21206
- }
21274
+ } // 自定义 hook 重名且没有开启覆盖的选项
21275
+
21207
21276
 
21208
21277
  if (!customHookConfig.force) {
21209
21278
  return WARN_DUPLICATED;
@@ -21211,7 +21280,7 @@
21211
21280
 
21212
21281
 
21213
21282
  var duplicateHookType = this.hookNameList[hookName].type;
21214
- this.hookList[duplicateHookType] = filter$2(_context5 = this.hookList[duplicateHookType]).call(_context5, function (hook) {
21283
+ this.hookList[duplicateHookType] = filter$2(_context4 = this.hookList[duplicateHookType]).call(_context4, function (hook) {
21215
21284
  return hook.getName() !== hookName;
21216
21285
  });
21217
21286
  }
@@ -21221,9 +21290,7 @@
21221
21290
  };
21222
21291
  this.hookList[hookType] = this.hookList[hookType] || []; // 内置Hook直接push到结尾
21223
21292
 
21224
- if (
21225
- /** @type {any}*/
21226
- HookClass.Cherry$$CUSTOM !== true) {
21293
+ if (!isRegisteredCustomSyntaxClass(HookClass)) {
21227
21294
  this.hookList[hookType].push(instance);
21228
21295
  return;
21229
21296
  } // 插入自定义Hook
@@ -21232,33 +21299,33 @@
21232
21299
  var insertIndex = -1;
21233
21300
 
21234
21301
  if (customHookConfig.before) {
21235
- var _context6;
21302
+ var _context5;
21236
21303
 
21237
- insertIndex = findIndex$2(_context6 = this.hookList[hookType]).call(_context6, function (hook) {
21304
+ insertIndex = findIndex$2(_context5 = this.hookList[hookType]).call(_context5, function (hook) {
21238
21305
  return hook.getName() === customHookConfig.before;
21239
21306
  });
21240
21307
 
21241
21308
  if (insertIndex === -1) {
21242
- var _context7;
21309
+ var _context6;
21243
21310
 
21244
- Logger.warn(concat$2(_context7 = "Cannot find hook named [".concat(customHookConfig.before, "],\n custom hook [")).call(_context7, hookName, "] will append to the end of the hooks."));
21311
+ Logger.warn(concat$2(_context6 = "Cannot find hook named [".concat(customHookConfig.before, "],\n custom hook [")).call(_context6, hookName, "] will append to the end of the hooks."));
21245
21312
  }
21246
21313
  } else if (customHookConfig.after) {
21247
- var _context8, _context9;
21314
+ var _context7, _context8;
21248
21315
 
21249
- insertIndex = findIndex$2(_context8 = this.hookList[hookType]).call(_context8, function (hook) {
21316
+ insertIndex = findIndex$2(_context7 = this.hookList[hookType]).call(_context7, function (hook) {
21250
21317
  return hook.getName() === customHookConfig.after;
21251
21318
  });
21252
- insertIndex === -1 ? Logger.warn(concat$2(_context9 = "Cannot find hook named [".concat(customHookConfig.after, "],\n custom hook [")).call(_context9, hookName, "] will append to the end of the hooks.")) : insertIndex += 1; // 统一处理往前插入的逻辑,所以要插入某Hook之后,索引需要加一
21319
+ insertIndex === -1 ? Logger.warn(concat$2(_context8 = "Cannot find hook named [".concat(customHookConfig.after, "],\n custom hook [")).call(_context8, hookName, "] will append to the end of the hooks.")) : insertIndex += 1; // 统一处理往前插入的逻辑,所以要插入某Hook之后,索引需要加一
21253
21320
  } // 无需插入或目标索引为数组结尾
21254
21321
 
21255
21322
 
21256
21323
  if (insertIndex < 0 || insertIndex >= this.hookList[hookType].length) {
21257
21324
  this.hookList[hookType].push(instance);
21258
21325
  } else {
21259
- var _context10;
21326
+ var _context9;
21260
21327
 
21261
- splice$3(_context10 = this.hookList[hookType]).call(_context10, insertIndex, 0, instance);
21328
+ splice$3(_context9 = this.hookList[hookType]).call(_context9, insertIndex, 0, instance);
21262
21329
  } // console.log(this.hookList[hookType]);
21263
21330
 
21264
21331
  }
@@ -82026,7 +82093,7 @@
82026
82093
  });
82027
82094
  }
82028
82095
 
82029
- var VERSION = "0.5.15-ff105f6c";
82096
+ var VERSION = "0.5.16-8d6bde02";
82030
82097
  var CherryStatic = /*#__PURE__*/function () {
82031
82098
  function CherryStatic() {
82032
82099
  _classCallCheck(this, CherryStatic);