Skip to content

Commit 15b8fc3

Browse files
committed
refactor: unify hooks-config and trim stream build
1 parent 49bcceb commit 15b8fc3

File tree

9 files changed

+520
-55
lines changed

9 files changed

+520
-55
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
},
3131
"devDependencies": {
3232
"@babel/eslint-parser": "^7.28.0",
33+
"@babel/plugin-proposal-optional-catch-binding": "^7.18.6",
3334
"@changesets/cli": "^2.29.5",
3435
"@cherry-markdown/changesets-changelog-github": "^0.0.1",
3536
"@commitlint/config-conventional": "^19.6.0",
37+
"@types/node": "^20.10.6",
3638
"@typescript-eslint/eslint-plugin": "^6.21.0",
3739
"@typescript-eslint/parser": "^6.21.0",
38-
"@types/node": "^20.10.6",
3940
"commitlint": "^19.6.1",
4041
"eslint": "^8.57.1",
4142
"eslint-config-prettier": "^10.1.5",

packages/cherry-markdown/build/rollup.stream.config.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ const umdOutputConfig = {
3737
sourcemap: true,
3838
compact: true,
3939
plugins: [terserPlugin()],
40-
globals: {
41-
mermaid: 'mermaid',
42-
codemirror: 'CodeMirror',
43-
'codemirror/src/util/misc': 'CodeMirror',
44-
},
40+
// 禁用 manualChunks 以启用更好的 tree-shake
41+
manualChunks: undefined,
4542
};
4643

4744
const esmOutputConfig = {
@@ -51,6 +48,7 @@ const esmOutputConfig = {
5148
name: 'Cherry',
5249
sourcemap: true,
5350
compact: true,
51+
manualChunks: undefined,
5452
plugins: [
5553
terserPlugin({
5654
module: true,
@@ -63,14 +61,14 @@ const options = {
6361
...baseConfig,
6462
input: 'src/index.stream.js',
6563
output: [umdOutputConfig, esmOutputConfig],
64+
// Stream 模式下排除 default hooks-config,使用 stream 版本
65+
external: (id) => {
66+
return id.includes('hooks-config/default.js');
67+
},
6668
};
6769

68-
if (!Array.isArray(options.external)) {
69-
options.external = [];
70-
}
71-
// 流式渲染包不需要mermaid和codemirror
72-
options.external.push('mermaid');
73-
options.external.push('codemirror');
74-
options.external.push(/^codemirror\/.*/); // 排除所有codemirror子模块
70+
// Stream 模式下排除完整版 hooks-config
71+
// 因为 Engine.js 默认导入它,但我们通过 CherryStream 传入了 stream 版本的配置
72+
// 这里通过 external 配置排除它,防止被打包
7573

7674
export default options;

packages/cherry-markdown/src/CherryStream.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import locales from '@/locales/index';
3333

3434
import { urlProcessorProxy } from './UrlCache';
3535
import { CherryStatic } from './CherryStatic';
36+
import hooksConfig from './core/hooks-config/stream';
3637

3738
/**
3839
* @typedef {import('~types/cherry').CherryOptions} CherryOptions
@@ -78,6 +79,19 @@ export default class CherryStream extends CherryStatic {
7879
this.options.editor.defaultModel = 'previewOnly';
7980
this.options.toolbars.showToolbar = false;
8081

82+
// Stream 模式下禁用 mermaid,避免引入 mermaid 依赖
83+
if (!this.options.engine || !this.options.engine.syntax) {
84+
this.options.engine = {};
85+
}
86+
if (!this.options.engine.syntax) {
87+
this.options.engine.syntax = {};
88+
}
89+
if (!this.options.engine.syntax.codeBlock) {
90+
this.options.engine.syntax.codeBlock = {};
91+
}
92+
// 设置 mermaid 为 false,禁用 mermaid 功能
93+
this.options.engine.syntax.codeBlock.mermaid = false;
94+
8195
this.locales = locales;
8296
if (this.options.locales) {
8397
this.locales = {
@@ -118,7 +132,7 @@ export default class CherryStream extends CherryStatic {
118132
/**
119133
* @type {import('./Engine').default}
120134
*/
121-
this.engine = new Engine(this.options, this);
135+
this.engine = new Engine(this.options, this, hooksConfig);
122136
this.init();
123137
}
124138

packages/cherry-markdown/src/Engine.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616
import HookCenter from './core/HookCenter';
17-
import hooksConfig from './core/HooksConfig';
17+
// 默认导入完整版 HooksConfig
18+
// 注意:Stream 版本通过 CherryStream 传入 customHooksConfig 来覆盖
19+
import hooksConfig from './core/hooks-config';
1820
import NestedError, { $expectTarget, $expectInherit, $expectInstance } from './utils/error';
1921
import CryptoJS from 'crypto-js';
2022
import SyntaxBase from './core/SyntaxBase';
@@ -36,8 +38,9 @@ export default class Engine {
3638
*
3739
* @param {Partial<import('./Cherry').CherryOptions>} markdownParams 初始化Cherry时传入的选项
3840
* @param {import('./Cherry').default} cherry Cherry实例
41+
* @param {(typeof SyntaxBase | typeof ParagraphBase)[]} [customHooksConfig] 自定义的hooks配置(可选)
3942
*/
40-
constructor(markdownParams, cherry) {
43+
constructor(markdownParams, cherry, customHooksConfig) {
4144
this.$cherry = cherry;
4245
// Deprecated
4346
Object.defineProperty(this, '_cherry', {
@@ -48,7 +51,9 @@ export default class Engine {
4851
});
4952
this.initMath(markdownParams);
5053
this.$configInit(markdownParams);
51-
this.hookCenter = new HookCenter(hooksConfig, markdownParams, cherry);
54+
// 使用传入的 hooksConfig,如果没有传入则使用默认配置
55+
const actualHooksConfig = customHooksConfig || hooksConfig;
56+
this.hookCenter = new HookCenter(actualHooksConfig, markdownParams, cherry);
5257
this.hooks = this.hookCenter.getHookList();
5358
this.asyncRenderHandler = new AsyncRenderHandler(cherry);
5459
// 使用LRU缓存替代普通对象
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright (C) 2021 Tencent.
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+
// 不引入 Suggester,因为它依赖 codemirror,Stream 版本不需要
49+
// import Suggester from './hooks/Suggester';
50+
import Ruby from './hooks/Ruby';
51+
import Panel from './hooks/Panel';
52+
import Detail from './hooks/Detail';
53+
import FrontMatter from './hooks/FrontMatter';
54+
import Space from './hooks/Space';
55+
import AiFlowAutoClose from './hooks/AiFlowAutoClose';
56+
/**
57+
* 引擎各语法的配置 (Stream 版本)
58+
* 主要决定支持哪些语法,以及各语法的执行顺序
59+
*
60+
* 与完整版的区别:
61+
* - 不包含 Suggester(依赖 codemirror,用于编辑器输入建议)
62+
*/
63+
const hooksConfig = [
64+
// 段落级 Hook
65+
// 引擎会按当前排序顺序执行beforeMake、makeHtml方法
66+
// 引擎会按当前排序逆序执行afterMake方法
67+
FrontMatter,
68+
CodeBlock,
69+
InlineCode,
70+
/**
71+
* 理论上行内公式(InlineMath)应该在段落公式(MathBlock)的后面,否则行内公式会破坏段落公式的渲染
72+
* 但实际交换顺序后,发现没啥问题,还顺带解决了[这个issue #1090](https://github.com/Tencent/cherry-markdown/issues/1090)的问题
73+
* 没问题的原因是对于段落公式(比如$$(a+b)^2=a^2+2ab+b^2$$),行内公式的确会命中,会识别出来两个'$$',所以无事发生
74+
*/
75+
InlineMath,
76+
MathBlock,
77+
AiFlowAutoClose,
78+
HtmlBlock,
79+
Footnote,
80+
CommentReference,
81+
Transfer,
82+
Br,
83+
Table,
84+
Toc,
85+
Blockquote,
86+
Header, // 处理标题, 传入strict属性严格要求ATX风格标题#后带空格
87+
Hr,
88+
List,
89+
Detail,
90+
Panel,
91+
Paragraph, // 普通段落
92+
93+
// 行内Hook
94+
// 引擎会按当前顺序执行makeHtml方法
95+
Emoji,
96+
Image,
97+
Link,
98+
AutoLink,
99+
Emphasis,
100+
BackgroundColor,
101+
Color,
102+
Size,
103+
Sub,
104+
Sup,
105+
Ruby,
106+
Strikethrough,
107+
Underline,
108+
HighLight,
109+
// 不包含 Suggester,Stream 版本不需要编辑器输入建议功能
110+
// Suggester,
111+
Space,
112+
];
113+
114+
export default hooksConfig;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright (C) 2021 Tencent.
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+
import Space from '../hooks/Space';
54+
import AiFlowAutoClose from '../hooks/AiFlowAutoClose';
55+
56+
/**
57+
* 引擎各语法的配置(完整版)
58+
* 主要决定支持哪些语法,以及各语法的执行顺序
59+
*
60+
* 包含所有 hooks,包括编辑器相关的 Suggester
61+
*/
62+
const defaultHooksConfig = [
63+
// 段落级 Hook
64+
// 引擎会按当前排序顺序执行beforeMake、makeHtml方法
65+
// 引擎会按当前排序逆序执行afterMake方法
66+
FrontMatter,
67+
CodeBlock,
68+
InlineCode,
69+
/**
70+
* 理论上行内公式(InlineMath)应该在段落公式(MathBlock)的后面,否则行内公式会破坏段落公式的渲染
71+
* 但实际交换顺序后,发现没啥问题,还顺带解决了[这个issue #1090](https://github.com/Tencent/cherry-markdown/issues/1090)的问题
72+
* 没问题的原因是对于段落公式(比如$$(a+b)^2=a^2+2ab+b^2$$),行内公式的确会命中,会识别出来两个'$$',所以无事发生
73+
*/
74+
InlineMath,
75+
MathBlock,
76+
AiFlowAutoClose,
77+
HtmlBlock,
78+
Footnote,
79+
CommentReference,
80+
Transfer,
81+
Br,
82+
Table,
83+
Toc,
84+
Blockquote,
85+
Header, // 处理标题, 传入strict属性严格要求ATX风格标题#后带空格
86+
Hr,
87+
List,
88+
Detail,
89+
Panel,
90+
Paragraph, // 段落级 Hook
91+
92+
// 行内Hook
93+
// 引擎会按当前顺序执行makeHtml方法
94+
Emoji,
95+
Image,
96+
Link,
97+
AutoLink,
98+
Emphasis,
99+
BackgroundColor,
100+
Color,
101+
Size,
102+
Sub,
103+
Sup,
104+
Ruby,
105+
Strikethrough,
106+
Underline,
107+
HighLight,
108+
Suggester,
109+
Space,
110+
];
111+
112+
export default defaultHooksConfig;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (C) 2021 Tencent.
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+
17+
/**
18+
* Hooks 配置统一导出
19+
*
20+
* 提供不同场景下的 hooks 配置:
21+
* - default: 完整版,包含所有 hooks(包括编辑器相关的 Suggester)
22+
* - stream: Stream 版本,精简版,不包含编辑器依赖(Suggester/codemirror/mermaid)
23+
*/
24+
25+
export { default as default } from './default.js';
26+
export { default as stream } from './stream.js';

0 commit comments

Comments
 (0)