@cherry-markdown/cherry-markdown-dev 0.8.58-dev
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/package.json +149 -0
- package/src/Cherry.config.js +625 -0
- package/src/Cherry.js +1104 -0
- package/src/CherryStatic.js +70 -0
- package/src/Editor.js +748 -0
- package/src/Engine.js +381 -0
- package/src/Event.js +140 -0
- package/src/Factory.js +180 -0
- package/src/Logger.js +31 -0
- package/src/Previewer.js +1183 -0
- package/src/Sanitizer.js +4 -0
- package/src/Sanitizer.node.js +7 -0
- package/src/UrlCache.js +98 -0
- package/src/addons/advance/cherry-table-echarts-plugin.js +170 -0
- package/src/addons/cherry-code-block-mermaid-plugin.js +158 -0
- package/src/addons/cherry-code-block-plantuml-plugin.js +106 -0
- package/src/core/HookCenter.js +297 -0
- package/src/core/HooksConfig.js +100 -0
- package/src/core/ParagraphBase.js +332 -0
- package/src/core/SentenceBase.js +65 -0
- package/src/core/SyntaxBase.js +194 -0
- package/src/core/hooks/AutoLink.js +232 -0
- package/src/core/hooks/BackgroundColor.js +46 -0
- package/src/core/hooks/Blockquote.js +70 -0
- package/src/core/hooks/Br.js +85 -0
- package/src/core/hooks/CodeBlock.js +446 -0
- package/src/core/hooks/Color.js +46 -0
- package/src/core/hooks/CommentReference.js +96 -0
- package/src/core/hooks/Detail.js +108 -0
- package/src/core/hooks/Emoji.config.js +1825 -0
- package/src/core/hooks/Emoji.js +119 -0
- package/src/core/hooks/Emphasis.js +113 -0
- package/src/core/hooks/Footnote.js +125 -0
- package/src/core/hooks/FrontMatter.js +51 -0
- package/src/core/hooks/Header.js +234 -0
- package/src/core/hooks/HighLight.js +37 -0
- package/src/core/hooks/Hr.js +52 -0
- package/src/core/hooks/HtmlBlock.js +184 -0
- package/src/core/hooks/Image.js +174 -0
- package/src/core/hooks/InlineCode.js +48 -0
- package/src/core/hooks/InlineMath.js +107 -0
- package/src/core/hooks/Link.js +160 -0
- package/src/core/hooks/List.js +264 -0
- package/src/core/hooks/MathBlock.js +103 -0
- package/src/core/hooks/Panel.js +145 -0
- package/src/core/hooks/Paragraph.js +84 -0
- package/src/core/hooks/Ruby.js +34 -0
- package/src/core/hooks/Size.js +51 -0
- package/src/core/hooks/Strikethrough.js +54 -0
- package/src/core/hooks/Sub.js +47 -0
- package/src/core/hooks/SuggestList.js +333 -0
- package/src/core/hooks/Suggester.js +707 -0
- package/src/core/hooks/Sup.js +47 -0
- package/src/core/hooks/Table.js +275 -0
- package/src/core/hooks/Toc.js +292 -0
- package/src/core/hooks/Transfer.js +47 -0
- package/src/core/hooks/Underline.js +37 -0
- package/src/index.core.js +29 -0
- package/src/index.engine.core.js +68 -0
- package/src/index.engine.js +28 -0
- package/src/index.js +32 -0
- package/src/libs/mermaidAPI.8.4.8.js +1 -0
- package/src/libs/mermaidAPI.8.5.2.js +42 -0
- package/src/libs/rawdeflate.js +1663 -0
- package/src/locales/en_US.js +139 -0
- package/src/locales/index.js +25 -0
- package/src/locales/ru_RU.js +139 -0
- package/src/locales/zh_CN.js +142 -0
- package/src/sass/base.scss +26 -0
- package/src/sass/bubble_formula.scss +166 -0
- package/src/sass/ch-icon.scss +118 -0
- package/src/sass/cherry.scss +1116 -0
- package/src/sass/components/bubble.scss +173 -0
- package/src/sass/components/shortcut_key_config.scss +108 -0
- package/src/sass/formula_utils_bubble.scss +82 -0
- package/src/sass/icon_template.scss +24 -0
- package/src/sass/icons/uEA03-list.svg +19 -0
- package/src/sass/icons/uEA04-check.svg +14 -0
- package/src/sass/icons/uEA09-square.svg +10 -0
- package/src/sass/icons/uEA0A-bold.svg +20 -0
- package/src/sass/icons/uEA0B-code.svg +18 -0
- package/src/sass/icons/uEA0C-color.svg +13 -0
- package/src/sass/icons/uEA0D-header.svg +8 -0
- package/src/sass/icons/uEA0E-image.svg +15 -0
- package/src/sass/icons/uEA0F-italic.svg +8 -0
- package/src/sass/icons/uEA10-link.svg +16 -0
- package/src/sass/icons/uEA11-ol.svg +21 -0
- package/src/sass/icons/uEA12-size.svg +11 -0
- package/src/sass/icons/uEA13-strike.svg +16 -0
- package/src/sass/icons/uEA14-table.svg +12 -0
- package/src/sass/icons/uEA15-ul.svg +17 -0
- package/src/sass/icons/uEA16-underline.svg +13 -0
- package/src/sass/icons/uEA17-word.svg +16 -0
- package/src/sass/icons/uEA18-blockquote.svg +11 -0
- package/src/sass/icons/uEA19-font.svg +10 -0
- package/src/sass/icons/uEA1F-insertClass.svg +39 -0
- package/src/sass/icons/uEA20-insertFlow.svg +8 -0
- package/src/sass/icons/uEA21-insertFormula.svg +23 -0
- package/src/sass/icons/uEA22-insertGantt.svg +13 -0
- package/src/sass/icons/uEA23-insertGraph.svg +13 -0
- package/src/sass/icons/uEA24-insertPie.svg +19 -0
- package/src/sass/icons/uEA25-insertSeq.svg +20 -0
- package/src/sass/icons/uEA26-insertState.svg +35 -0
- package/src/sass/icons/uEA27-line.svg +11 -0
- package/src/sass/icons/uEA28-preview.svg +18 -0
- package/src/sass/icons/uEA29-previewClose.svg +24 -0
- package/src/sass/icons/uEA2A-toc.svg +24 -0
- package/src/sass/icons/uEA2D-sub.svg +15 -0
- package/src/sass/icons/uEA2E-sup.svg +15 -0
- package/src/sass/icons/uEA2F-h1.svg +16 -0
- package/src/sass/icons/uEA30-h2.svg +20 -0
- package/src/sass/icons/uEA31-h3.svg +23 -0
- package/src/sass/icons/uEA32-h4.svg +16 -0
- package/src/sass/icons/uEA33-h5.svg +20 -0
- package/src/sass/icons/uEA34-h6.svg +17 -0
- package/src/sass/icons/uEA35-video.svg +20 -0
- package/src/sass/icons/uEA36-insert.svg +25 -0
- package/src/sass/icons/uEA37-little_table.svg +30 -0
- package/src/sass/icons/uEA38-pdf.svg +27 -0
- package/src/sass/icons/uEA39-checklist.svg +22 -0
- package/src/sass/icons/uEA40-close.svg +12 -0
- package/src/sass/icons/uEA41-fullscreen.svg +81 -0
- package/src/sass/icons/uEA42-minscreen.svg +77 -0
- package/src/sass/icons/uEA43-insertChart.svg +23 -0
- package/src/sass/icons/uEA44-question.svg +25 -0
- package/src/sass/icons/uEA45-settings.svg +32 -0
- package/src/sass/icons/uEA46-ok.svg +7 -0
- package/src/sass/icons/uEA47-br.svg +22 -0
- package/src/sass/icons/uEA48-normal.svg +15 -0
- package/src/sass/icons/uEA49-undo.svg +19 -0
- package/src/sass/icons/uEA50-redo.svg +21 -0
- package/src/sass/icons/uEA51-copy.svg +6 -0
- package/src/sass/icons/uEA52-phone.svg +5 -0
- package/src/sass/icons/uEA53-cherry-table-delete.svg +17 -0
- package/src/sass/icons/uEA54-cherry-table-insert-bottom.svg +16 -0
- package/src/sass/icons/uEA55-cherry-table-insert-left.svg +15 -0
- package/src/sass/icons/uEA56-cherry-table-insert-right.svg +16 -0
- package/src/sass/icons/uEA57-cherry-table-insert-top.svg +16 -0
- package/src/sass/icons/uEA58-sort-s.svg +13 -0
- package/src/sass/icons/uEA59-pinyin.svg +1 -0
- package/src/sass/icons/uEA5A-create.svg +24 -0
- package/src/sass/icons/uEA5B-download.svg +34 -0
- package/src/sass/icons/uEA5C-edit.svg +3 -0
- package/src/sass/icons/uEA5D-export.svg +53 -0
- package/src/sass/icons/uEA5E-folder-open.svg +3 -0
- package/src/sass/icons/uEA5F-folder.svg +3 -0
- package/src/sass/icons/uEA60-help.svg +5 -0
- package/src/sass/icons/uEA61-pen-fill.svg +13 -0
- package/src/sass/icons/uEA62-pen.svg +3 -0
- package/src/sass/icons/uEA64-tips.svg +5 -0
- package/src/sass/icons/uEA65-warn.svg +5 -0
- package/src/sass/icons/uEA66-mistake.svg +4 -0
- package/src/sass/icons/uEA67-success.svg +4 -0
- package/src/sass/icons/uEA68-danger.svg +4 -0
- package/src/sass/icons/uEA69-info.svg +5 -0
- package/src/sass/icons/uEA6A-primary.svg +5 -0
- package/src/sass/icons/uEA6B-warning.svg +5 -0
- package/src/sass/icons/uEA6C-justify.svg +19 -0
- package/src/sass/icons/uEA6D-justifyCenter.svg +19 -0
- package/src/sass/icons/uEA6E-justifyLeft.svg +19 -0
- package/src/sass/icons/uEA6F-justifyRight.svg +19 -0
- package/src/sass/icons/uEA70-chevronsLeft.svg +1 -0
- package/src/sass/icons/uEA71-chevronsRight.svg +1 -0
- package/src/sass/icons/uEA72-trendingUp.svg +1 -0
- package/src/sass/icons/uEA74-codeBlock.svg +1 -0
- package/src/sass/icons/uEA75-expand.svg +3 -0
- package/src/sass/icons/uEA76-unExpand.svg +3 -0
- package/src/sass/icons/uEA77-swap-vert.svg +1 -0
- package/src/sass/icons/uEA78-swap.svg +1 -0
- package/src/sass/icons/uEA79-keyboard.svg +1 -0
- package/src/sass/icons/uEA7A-command.svg +1 -0
- package/src/sass/icons/uEA7B-search.svg +1 -0
- package/src/sass/index.scss +3 -0
- package/src/sass/markdown.scss +668 -0
- package/src/sass/markdown_pure.scss +9 -0
- package/src/sass/prettyprint/prettyprint.scss +118 -0
- package/src/sass/previewer.scss +179 -0
- package/src/sass/print.scss +13 -0
- package/src/sass/prism/coy.scss +220 -0
- package/src/sass/prism/dark.scss +132 -0
- package/src/sass/prism/default.scss +143 -0
- package/src/sass/prism/funky.scss +133 -0
- package/src/sass/prism/okaidia.scss +126 -0
- package/src/sass/prism/one-dark.scss +440 -0
- package/src/sass/prism/one-light.scss +428 -0
- package/src/sass/prism/solarized-light.scss +153 -0
- package/src/sass/prism/tomorrow-night.scss +125 -0
- package/src/sass/prism/twilight.scss +202 -0
- package/src/sass/prism/vs-dark.scss +275 -0
- package/src/sass/prism/vs-light.scss +168 -0
- package/src/sass/themes/blue.scss +411 -0
- package/src/sass/themes/dark.scss +517 -0
- package/src/sass/themes/default.scss +255 -0
- package/src/sass/themes/green.scss +395 -0
- package/src/sass/themes/light.scss +368 -0
- package/src/sass/themes/red.scss +397 -0
- package/src/sass/themes/violet.scss +410 -0
- package/src/sass/variable.scss +84 -0
- package/src/toolbars/Bubble.js +234 -0
- package/src/toolbars/BubbleFormula.js +298 -0
- package/src/toolbars/BubbleTable.js +147 -0
- package/src/toolbars/FloatMenu.js +131 -0
- package/src/toolbars/HiddenToolbar.js +36 -0
- package/src/toolbars/HookCenter.js +234 -0
- package/src/toolbars/MenuBase.js +569 -0
- package/src/toolbars/PreviewerBubble.js +608 -0
- package/src/toolbars/ShortcutKeyConfigPanel.js +345 -0
- package/src/toolbars/Sidebar.js +36 -0
- package/src/toolbars/Toc.js +242 -0
- package/src/toolbars/Toolbar.js +449 -0
- package/src/toolbars/ToolbarRight.js +37 -0
- package/src/toolbars/hooks/Audio.js +79 -0
- package/src/toolbars/hooks/BarTable.js +41 -0
- package/src/toolbars/hooks/Bold.js +73 -0
- package/src/toolbars/hooks/Br.js +34 -0
- package/src/toolbars/hooks/ChangeLocale.js +62 -0
- package/src/toolbars/hooks/ChatGpt.js +182 -0
- package/src/toolbars/hooks/CheckList.js +41 -0
- package/src/toolbars/hooks/Code.js +49 -0
- package/src/toolbars/hooks/CodeTheme.js +66 -0
- package/src/toolbars/hooks/Color.js +298 -0
- package/src/toolbars/hooks/Copy.js +141 -0
- package/src/toolbars/hooks/Detail.js +69 -0
- package/src/toolbars/hooks/DrawIo.js +57 -0
- package/src/toolbars/hooks/Export.js +49 -0
- package/src/toolbars/hooks/File.js +79 -0
- package/src/toolbars/hooks/Formula.js +69 -0
- package/src/toolbars/hooks/FullScreen.js +50 -0
- package/src/toolbars/hooks/Graph.js +263 -0
- package/src/toolbars/hooks/H1.js +71 -0
- package/src/toolbars/hooks/H2.js +71 -0
- package/src/toolbars/hooks/H3.js +71 -0
- package/src/toolbars/hooks/Header.js +118 -0
- package/src/toolbars/hooks/Hr.js +35 -0
- package/src/toolbars/hooks/Image.js +91 -0
- package/src/toolbars/hooks/InlineCode.js +53 -0
- package/src/toolbars/hooks/Insert.js +193 -0
- package/src/toolbars/hooks/Italic.js +72 -0
- package/src/toolbars/hooks/Justify.js +49 -0
- package/src/toolbars/hooks/LineTable.js +41 -0
- package/src/toolbars/hooks/Link.js +49 -0
- package/src/toolbars/hooks/List.js +55 -0
- package/src/toolbars/hooks/MobilePreview.js +44 -0
- package/src/toolbars/hooks/Ol.js +41 -0
- package/src/toolbars/hooks/Panel.js +140 -0
- package/src/toolbars/hooks/Pdf.js +78 -0
- package/src/toolbars/hooks/Publish.js +123 -0
- package/src/toolbars/hooks/QuickTable.js +43 -0
- package/src/toolbars/hooks/Quote.js +45 -0
- package/src/toolbars/hooks/Redo.js +33 -0
- package/src/toolbars/hooks/Ruby.js +59 -0
- package/src/toolbars/hooks/Search.js +53 -0
- package/src/toolbars/hooks/Settings.js +220 -0
- package/src/toolbars/hooks/ShortcutKey.js +62 -0
- package/src/toolbars/hooks/Size.js +118 -0
- package/src/toolbars/hooks/Split.js +37 -0
- package/src/toolbars/hooks/Strikethrough.js +71 -0
- package/src/toolbars/hooks/Sub.js +58 -0
- package/src/toolbars/hooks/Sup.js +58 -0
- package/src/toolbars/hooks/SwitchModel.js +56 -0
- package/src/toolbars/hooks/Table.js +56 -0
- package/src/toolbars/hooks/Theme.js +62 -0
- package/src/toolbars/hooks/Toc.js +35 -0
- package/src/toolbars/hooks/TogglePreview.js +91 -0
- package/src/toolbars/hooks/Ul.js +41 -0
- package/src/toolbars/hooks/Underline.js +68 -0
- package/src/toolbars/hooks/Undo.js +30 -0
- package/src/toolbars/hooks/Video.js +79 -0
- package/src/toolbars/hooks/Word.js +78 -0
- package/src/toolbars/hooks/WordCount.js +106 -0
- package/src/utils/autoindent.js +58 -0
- package/src/utils/cm-search-replace.js +794 -0
- package/src/utils/code-preview-language-setting.js +180 -0
- package/src/utils/codeBlockContentHandler.js +400 -0
- package/src/utils/config.js +174 -0
- package/src/utils/copy.js +55 -0
- package/src/utils/dialog.js +214 -0
- package/src/utils/dom.js +163 -0
- package/src/utils/downloadUtil.js +23 -0
- package/src/utils/env.js +22 -0
- package/src/utils/error.js +61 -0
- package/src/utils/event.js +38 -0
- package/src/utils/export.js +166 -0
- package/src/utils/file.js +164 -0
- package/src/utils/formulaUtilsHandler.js +232 -0
- package/src/utils/htmlparser.js +976 -0
- package/src/utils/image.js +99 -0
- package/src/utils/imgSizeHandler.js +279 -0
- package/src/utils/lazyLoadImg.js +327 -0
- package/src/utils/lineFeed.js +49 -0
- package/src/utils/listContentHandler.js +227 -0
- package/src/utils/lookbehind-replace.js +81 -0
- package/src/utils/mathjax.js +89 -0
- package/src/utils/myersDiff.js +211 -0
- package/src/utils/pasteHelper.js +253 -0
- package/src/utils/platformTransform.js +71 -0
- package/src/utils/recount-pos.js +59 -0
- package/src/utils/regexp.js +295 -0
- package/src/utils/sanitize.js +477 -0
- package/src/utils/selection.js +50 -0
- package/src/utils/shortcutKey.js +291 -0
- package/src/utils/svgUtils.js +96 -0
- package/src/utils/tableContentHandler.js +876 -0
- package/test/core/CommonMark.spec.ts +62 -0
- package/test/core/hooks/AutoLink.spec.ts +28 -0
- package/test/core/hooks/List.spec.ts +79 -0
- package/test/core/hooks/__snapshots__/List.spec.ts.snap +11 -0
- package/test/example.md +778 -0
- package/test/node.js +10 -0
- package/test/suites/commonmark.spec.json +5218 -0
- package/test/tsconfig.test.json +6 -0
- package/test/utils/regexp.spec.ts +28 -0
- package/types/cherry.d.ts +675 -0
- package/types/codemirror.d.ts +22 -0
- package/types/editor.d.ts +72 -0
- package/types/global.d.ts +16 -0
- package/types/menus.d.ts +24 -0
- package/types/previewer.d.ts +53 -0
- package/types/syntax.d.ts +52 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (C) 2021 THL A29 Limited, a Tencent company.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import SyntaxBase from './SyntaxBase';
|
|
17
|
+
import ParagraphBase from './ParagraphBase';
|
|
18
|
+
import { $expectTarget } from '@/utils/error';
|
|
19
|
+
import Logger from '@/Logger';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {import('~types/cherry').CherryOptions} CherryOptions
|
|
23
|
+
* @typedef {import('~types/cherry').CherryEngineOptions} CherryEngineOptions
|
|
24
|
+
* @typedef {import('~types/cherry').CustomSyntaxRegConfig} CustomSyntaxRegConfig
|
|
25
|
+
* @typedef { (SyntaxBase | ParagraphBase) & { Cherry$$CUSTOM: true } } CustomSyntax
|
|
26
|
+
* @typedef { (typeof SyntaxBase | typeof ParagraphBase) & { Cherry$$CUSTOM: true } } CustomSyntaxClass
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const WARN_DUPLICATED = -1;
|
|
30
|
+
const WARN_NOT_A_VALID_HOOK = -2;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 处理报错信息,在dev模式下才会输出报错信息
|
|
34
|
+
* @param {number} type
|
|
35
|
+
* @param {any} objClass
|
|
36
|
+
* @param {number} index
|
|
37
|
+
*/
|
|
38
|
+
function processWarning(type, objClass, index) {
|
|
39
|
+
if (type === WARN_DUPLICATED) {
|
|
40
|
+
Logger.warn(
|
|
41
|
+
`Duplicate hook name [${objClass.HOOK_NAME}] found, hook [${objClass.toString()}] ${
|
|
42
|
+
isNaN(index) ? '' : `at index [${index}] `
|
|
43
|
+
}will not take effect.`,
|
|
44
|
+
);
|
|
45
|
+
} else if (type === WARN_NOT_A_VALID_HOOK) {
|
|
46
|
+
Logger.warn(
|
|
47
|
+
`Hook [${objClass.toString()}] ${
|
|
48
|
+
isNaN(index) ? '' : `at index [${index}] `
|
|
49
|
+
}is not a valid hook, and will not take effect.`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 是否一个合法的 HookClass
|
|
56
|
+
* @param {any} HookClass
|
|
57
|
+
* @returns { HookClass is (typeof SyntaxBase | typeof ParagraphBase) }
|
|
58
|
+
*/
|
|
59
|
+
function isHookValid(HookClass) {
|
|
60
|
+
return isProtoOfSyntaxBase(HookClass) || isProtoOfParagraphBase(HookClass);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 传入的类是否 SyntaxBase 的子类
|
|
65
|
+
* @param {any} value
|
|
66
|
+
* @returns { value is typeof SyntaxBase }
|
|
67
|
+
*/
|
|
68
|
+
function isProtoOfSyntaxBase(value) {
|
|
69
|
+
return Object.prototype.isPrototypeOf.call(SyntaxBase, value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 传入的类是否 ParagraphBase 的子类
|
|
74
|
+
* @param {any} value
|
|
75
|
+
* @returns { value is typeof ParagraphBase }
|
|
76
|
+
*/
|
|
77
|
+
function isProtoOfParagraphBase(value) {
|
|
78
|
+
return Object.prototype.isPrototypeOf.call(ParagraphBase, value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 是否一个配置型的自定义语法
|
|
83
|
+
* @param {any} value
|
|
84
|
+
* @returns { value is CustomSyntaxRegConfig }
|
|
85
|
+
*/
|
|
86
|
+
function isCustomSyntaxConfig(value) {
|
|
87
|
+
const syntaxClass = /** @type {any} */ (/** @type {CustomSyntaxRegConfig} */ (value)?.syntaxClass);
|
|
88
|
+
return isProtoOfSyntaxBase(syntaxClass) || isProtoOfParagraphBase(syntaxClass);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 是否一个已注册的自定义语法hook类
|
|
93
|
+
* @param {any} value
|
|
94
|
+
* @returns { value is CustomSyntaxClass }
|
|
95
|
+
*/
|
|
96
|
+
function isRegisteredCustomSyntaxClass(value) {
|
|
97
|
+
return isHookValid(value) && /** @type {CustomSyntaxClass} */ (value)?.Cherry$$CUSTOM === true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 语法注册中心
|
|
102
|
+
*/
|
|
103
|
+
export default class HookCenter {
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
* @param {(typeof SyntaxBase)[]} hooksConfig
|
|
107
|
+
* @param {CherryOptions} editorConfig
|
|
108
|
+
*/
|
|
109
|
+
constructor(hooksConfig, editorConfig, cherry) {
|
|
110
|
+
this.$locale = cherry.locale;
|
|
111
|
+
this.$cherry = cherry;
|
|
112
|
+
/**
|
|
113
|
+
* @property
|
|
114
|
+
* @type {Record<import('./SyntaxBase').HookType, SyntaxBase[]>} hookList hook 名称 -> hook 类型的映射
|
|
115
|
+
*/
|
|
116
|
+
this.hookList = /** @type {any} */ ({});
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @property
|
|
120
|
+
* @type {Record<string, { type: import('./SyntaxBase').HookType }>} hookNameList hook 名称 -> hook 类型的映射
|
|
121
|
+
*/
|
|
122
|
+
this.hookNameList = {};
|
|
123
|
+
|
|
124
|
+
$expectTarget(hooksConfig, Array);
|
|
125
|
+
this.registerInternalHooks(hooksConfig, editorConfig);
|
|
126
|
+
this.registerCustomHooks(editorConfig.engine.customSyntax, editorConfig);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 注册系统默认的语法hook
|
|
131
|
+
* @param {any[]} hooksConfig 在hookconfig.js里定义的配置
|
|
132
|
+
* @param {CherryOptions} editorConfig 编辑器配置
|
|
133
|
+
*/
|
|
134
|
+
registerInternalHooks(hooksConfig, editorConfig) {
|
|
135
|
+
hooksConfig.forEach(
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* @param {typeof SyntaxBase} HookClass
|
|
139
|
+
* @param {number} index
|
|
140
|
+
*/
|
|
141
|
+
(HookClass, index) => {
|
|
142
|
+
const result = this.register(HookClass, editorConfig);
|
|
143
|
+
processWarning(result, HookClass, index);
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 注册第三方的语法hook
|
|
150
|
+
* @param {CherryEngineOptions['customSyntax']} customHooks 用户传入的配置
|
|
151
|
+
* @param {CherryOptions} editorConfig 编辑器配置
|
|
152
|
+
*/
|
|
153
|
+
registerCustomHooks(customHooks, editorConfig) {
|
|
154
|
+
if (!customHooks) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const hookNames = Object.keys(customHooks);
|
|
158
|
+
hookNames.forEach((hookName) => {
|
|
159
|
+
/** @type {number} */
|
|
160
|
+
let result;
|
|
161
|
+
/** @type {typeof SyntaxBase} */
|
|
162
|
+
let HookClass;
|
|
163
|
+
const customHookConfig = {};
|
|
164
|
+
const hookClassOrConfig = customHooks[hookName];
|
|
165
|
+
if (isProtoOfSyntaxBase(hookClassOrConfig)) {
|
|
166
|
+
HookClass = hookClassOrConfig;
|
|
167
|
+
} else if (isCustomSyntaxConfig(hookClassOrConfig)) {
|
|
168
|
+
HookClass = hookClassOrConfig.syntaxClass;
|
|
169
|
+
customHookConfig.force = Boolean(hookClassOrConfig.force);
|
|
170
|
+
if (hookClassOrConfig.before) {
|
|
171
|
+
customHookConfig.before = hookClassOrConfig.before;
|
|
172
|
+
} else if (hookClassOrConfig.after) {
|
|
173
|
+
customHookConfig.after = hookClassOrConfig.after;
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (isHookValid(HookClass)) {
|
|
179
|
+
// 自定义Hook标识
|
|
180
|
+
Object.defineProperty(HookClass, 'Cherry$$CUSTOM', {
|
|
181
|
+
enumerable: false,
|
|
182
|
+
configurable: false,
|
|
183
|
+
writable: false,
|
|
184
|
+
value: true,
|
|
185
|
+
});
|
|
186
|
+
result = this.register(HookClass, editorConfig, customHookConfig);
|
|
187
|
+
} else {
|
|
188
|
+
result = WARN_NOT_A_VALID_HOOK;
|
|
189
|
+
}
|
|
190
|
+
processWarning(result, HookClass, undefined);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
getHookList() {
|
|
195
|
+
return this.hookList;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
getHookNameList() {
|
|
199
|
+
return this.hookNameList;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
* @param {((...args: any[]) => any) | typeof SyntaxBase} HookClass
|
|
205
|
+
* @param {CherryOptions} editorConfig
|
|
206
|
+
* @param {Omit<CustomSyntaxRegConfig, 'syntaxClass'>} [customHookConfig]
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
register(HookClass, editorConfig, customHookConfig) {
|
|
210
|
+
// filter Configs Here
|
|
211
|
+
const { externals, engine } = editorConfig;
|
|
212
|
+
const { syntax } = engine;
|
|
213
|
+
const { $cherry } = this;
|
|
214
|
+
|
|
215
|
+
/** @type {SyntaxBase | CustomSyntax} */
|
|
216
|
+
let instance;
|
|
217
|
+
|
|
218
|
+
/** @type {string} */
|
|
219
|
+
let hookName;
|
|
220
|
+
|
|
221
|
+
// 首先校验Hook是否合法
|
|
222
|
+
if (!isHookValid(HookClass)) {
|
|
223
|
+
// 可能是一个function hook
|
|
224
|
+
if (typeof HookClass === 'function') {
|
|
225
|
+
const funcHook = HookClass;
|
|
226
|
+
instance = funcHook(editorConfig);
|
|
227
|
+
if (!instance || !isHookValid(instance.constructor)) {
|
|
228
|
+
return WARN_NOT_A_VALID_HOOK;
|
|
229
|
+
}
|
|
230
|
+
hookName = instance.getName();
|
|
231
|
+
} else {
|
|
232
|
+
return WARN_NOT_A_VALID_HOOK;
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
hookName = HookClass.HOOK_NAME;
|
|
236
|
+
// TODO: 需要考虑自定义 hook 配置的传入方式
|
|
237
|
+
const config = syntax?.[hookName] || {};
|
|
238
|
+
instance = new HookClass({ externals, config, globalConfig: engine.global, cherry: $cherry });
|
|
239
|
+
instance.afterInit(() => {
|
|
240
|
+
instance.setLocale(this.$locale);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
// TODO: 待校验是否需要跳过禁用的自定义 hook
|
|
244
|
+
// Skip Disabled Internal Hooks
|
|
245
|
+
if (syntax[hookName] === false && !isRegisteredCustomSyntaxClass(HookClass)) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
// 下面处理的都是 CustomSyntax
|
|
249
|
+
const hookType = instance.getType();
|
|
250
|
+
if (this.hookNameList[hookName]) {
|
|
251
|
+
// 内置 hook 重名
|
|
252
|
+
if (!isRegisteredCustomSyntaxClass(HookClass)) {
|
|
253
|
+
return WARN_DUPLICATED;
|
|
254
|
+
}
|
|
255
|
+
// 自定义 hook 重名且没有开启覆盖的选项
|
|
256
|
+
if (!customHookConfig.force) {
|
|
257
|
+
return WARN_DUPLICATED;
|
|
258
|
+
}
|
|
259
|
+
// 强制覆盖以前的Hook,所以需要移除
|
|
260
|
+
const duplicateHookType = this.hookNameList[hookName].type;
|
|
261
|
+
this.hookList[duplicateHookType] = this.hookList[duplicateHookType].filter((hook) => hook.getName() !== hookName);
|
|
262
|
+
}
|
|
263
|
+
this.hookNameList[hookName] = { type: hookType };
|
|
264
|
+
this.hookList[hookType] = this.hookList[hookType] || [];
|
|
265
|
+
// 内置Hook直接push到结尾
|
|
266
|
+
if (!isRegisteredCustomSyntaxClass(HookClass)) {
|
|
267
|
+
this.hookList[hookType].push(instance);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
// 插入自定义Hook
|
|
271
|
+
let insertIndex = -1;
|
|
272
|
+
if (customHookConfig.before) {
|
|
273
|
+
insertIndex = this.hookList[hookType].findIndex((hook) => hook.getName() === customHookConfig.before);
|
|
274
|
+
if (insertIndex === -1) {
|
|
275
|
+
Logger.warn(
|
|
276
|
+
`Cannot find hook named [${customHookConfig.before}],
|
|
277
|
+
custom hook [${hookName}] will append to the end of the hooks.`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
} else if (customHookConfig.after) {
|
|
281
|
+
insertIndex = this.hookList[hookType].findIndex((hook) => hook.getName() === customHookConfig.after);
|
|
282
|
+
insertIndex === -1
|
|
283
|
+
? Logger.warn(
|
|
284
|
+
`Cannot find hook named [${customHookConfig.after}],
|
|
285
|
+
custom hook [${hookName}] will append to the end of the hooks.`,
|
|
286
|
+
)
|
|
287
|
+
: (insertIndex += 1); // 统一处理往前插入的逻辑,所以要插入某Hook之后,索引需要加一
|
|
288
|
+
}
|
|
289
|
+
// 无需插入或目标索引为数组结尾
|
|
290
|
+
if (insertIndex < 0 || insertIndex >= this.hookList[hookType].length) {
|
|
291
|
+
this.hookList[hookType].push(instance);
|
|
292
|
+
} else {
|
|
293
|
+
this.hookList[hookType].splice(insertIndex, 0, instance);
|
|
294
|
+
}
|
|
295
|
+
// console.log(this.hookList[hookType]);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (C) 2021 THL A29 Limited, a Tencent company.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
// import Bold from './hooks/Bold';
|
|
17
|
+
// import Italic from './hooks/Italic';
|
|
18
|
+
// import Underline from './hooks/Underline';
|
|
19
|
+
import Color from './hooks/Color';
|
|
20
|
+
import BackgroundColor from './hooks/BackgroundColor';
|
|
21
|
+
import Size from './hooks/Size';
|
|
22
|
+
import Strikethrough from './hooks/Strikethrough';
|
|
23
|
+
import Sup from './hooks/Sup';
|
|
24
|
+
import Sub from './hooks/Sub';
|
|
25
|
+
import InlineCode from './hooks/InlineCode';
|
|
26
|
+
import CodeBlock from './hooks/CodeBlock';
|
|
27
|
+
import Link from './hooks/Link';
|
|
28
|
+
import Emphasis from './hooks/Emphasis';
|
|
29
|
+
import Paragraph from './hooks/Paragraph';
|
|
30
|
+
import Header from './hooks/Header';
|
|
31
|
+
import Transfer from './hooks/Transfer';
|
|
32
|
+
import Table from './hooks/Table';
|
|
33
|
+
import Br from './hooks/Br';
|
|
34
|
+
import Hr from './hooks/Hr';
|
|
35
|
+
import Image from './hooks/Image';
|
|
36
|
+
import List from './hooks/List';
|
|
37
|
+
import Blockquote from './hooks/Blockquote';
|
|
38
|
+
import AutoLink from './hooks/AutoLink';
|
|
39
|
+
import MathBlock from './hooks/MathBlock';
|
|
40
|
+
import InlineMath from './hooks/InlineMath';
|
|
41
|
+
import Toc from './hooks/Toc';
|
|
42
|
+
import Footnote from './hooks/Footnote';
|
|
43
|
+
import CommentReference from './hooks/CommentReference';
|
|
44
|
+
import HtmlBlock from './hooks/HtmlBlock';
|
|
45
|
+
import Emoji from './hooks/Emoji';
|
|
46
|
+
import Underline from './hooks/Underline';
|
|
47
|
+
import HighLight from './hooks/HighLight';
|
|
48
|
+
import Suggester from './hooks/Suggester';
|
|
49
|
+
import Ruby from './hooks/Ruby';
|
|
50
|
+
import Panel from './hooks/Panel';
|
|
51
|
+
import Detail from './hooks/Detail';
|
|
52
|
+
import FrontMatter from './hooks/FrontMatter';
|
|
53
|
+
/**
|
|
54
|
+
* 引擎各语法的配置
|
|
55
|
+
* 主要决定支持哪些语法,以及各语法的执行顺序
|
|
56
|
+
*/
|
|
57
|
+
const hooksConfig = [
|
|
58
|
+
// 段落级 Hook
|
|
59
|
+
// 引擎会按当前排序顺序执行beforeMake、makeHtml方法
|
|
60
|
+
// 引擎会按当前排序逆序执行afterMake方法
|
|
61
|
+
FrontMatter,
|
|
62
|
+
CodeBlock,
|
|
63
|
+
InlineCode,
|
|
64
|
+
MathBlock,
|
|
65
|
+
InlineMath,
|
|
66
|
+
HtmlBlock,
|
|
67
|
+
Footnote,
|
|
68
|
+
CommentReference,
|
|
69
|
+
Transfer,
|
|
70
|
+
Br,
|
|
71
|
+
Table,
|
|
72
|
+
Blockquote,
|
|
73
|
+
Toc,
|
|
74
|
+
Header, // 处理标题, 传入strict属性严格要求ATX风格标题#后带空格
|
|
75
|
+
Hr,
|
|
76
|
+
List,
|
|
77
|
+
Detail,
|
|
78
|
+
Panel,
|
|
79
|
+
Paragraph, // 普通段落
|
|
80
|
+
|
|
81
|
+
// 行内Hook
|
|
82
|
+
// 引擎会按当前顺序执行makeHtml方法
|
|
83
|
+
Emoji,
|
|
84
|
+
Image,
|
|
85
|
+
Link,
|
|
86
|
+
AutoLink,
|
|
87
|
+
Emphasis,
|
|
88
|
+
BackgroundColor,
|
|
89
|
+
Color,
|
|
90
|
+
Size,
|
|
91
|
+
Sub,
|
|
92
|
+
Sup,
|
|
93
|
+
Ruby,
|
|
94
|
+
Strikethrough,
|
|
95
|
+
Underline,
|
|
96
|
+
HighLight,
|
|
97
|
+
Suggester,
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
export default hooksConfig;
|