Skip to content

Commit 291a5fc

Browse files
committed
refactor: 优化代码结构和模块导入
1 parent 25bce25 commit 291a5fc

File tree

6 files changed

+15
-111
lines changed

6 files changed

+15
-111
lines changed

IMPORT_GUIDE.md

Lines changed: 0 additions & 69 deletions
This file was deleted.

packages/cherry-markdown/src/CherryStream.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ export default class CherryStream extends CherryStatic {
7777
this.options.isPreviewOnly = true;
7878
this.options.editor.defaultModel = 'previewOnly';
7979
this.options.toolbars.showToolbar = false;
80-
// Stream 版本禁用 PreviewerBubble,避免引入 codemirror
81-
this.options.previewer.enablePreviewerBubble = false;
8280

8381
this.locales = locales;
8482
if (this.options.locales) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import createDOMPurify from 'dompurify';
2+
import { JSDOM } from 'jsdom';
3+
4+
// 为 Node.js 环境创建虚拟 window 对象
5+
// dompurify 需要 window 对象才能在 Node.js 环境中工作
6+
const { window } = new JSDOM('');
7+
8+
export const sanitizer = createDOMPurify(window);

packages/cherry-markdown/src/core/hooks/CodeBlock.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* limitations under the License.
1515
*/
1616
import ParagraphBase from '@/core/ParagraphBase';
17-
// Import Prism first to ensure it's initialized before importing language components
17+
// 先导入 Prism 核心(必须在注册语言之前导入)
1818
import Prism from 'prismjs';
19-
// Import Prism language components AFTER importing Prism
20-
// 这些模块会自动注册语言到 Prism.languages
19+
// 导入 Prism 语言组件
20+
// 注意:这些模块有副作用 - 它们会将语言注册到 Prism.languages 中
21+
// 必须单独导入,因为 Prism 核心包不包含任何语言定义
2122
import 'prismjs/components/prism-core';
2223
import 'prismjs/components/prism-clike';
2324
import 'prismjs/components/prism-markup';
@@ -280,21 +281,8 @@ export default class CodeBlock extends ParagraphBase {
280281
} else {
281282
// 默认使用prism渲染代码块
282283
if (!lang || !Prism.languages[lang]) lang = 'javascript'; // 如果没有写语言,默认用js样式渲染
283-
284-
// 确保语言组件已加载
285-
if (!Prism.languages[lang]) {
286-
console.warn(`Prism language "${lang}" is not available. Available languages:`, Object.keys(Prism.languages));
287-
// 尝试使用 javascript 作为备用
288-
lang = 'javascript';
289-
}
290-
291-
// 再次检查,如果 javascript 也不存在,则使用纯文本
292-
if (!Prism.languages[lang]) {
293-
cacheCode = cacheCode;
294-
} else {
295-
cacheCode = Prism.highlight(cacheCode, Prism.languages[lang], lang);
296-
cacheCode = this.renderLineNumber(cacheCode);
297-
}
284+
cacheCode = Prism.highlight(cacheCode, Prism.languages[lang], lang);
285+
cacheCode = this.renderLineNumber(cacheCode);
298286
}
299287
const needUnExpand = this.expandCode && $code.match(/\n/g)?.length > 10; // 是否需要收起代码块
300288
const codeHtml = `<pre class="language-${lang}">${this.wrapCode(cacheCode, lang)}</pre>`;

packages/cherry-markdown/src/core/hooks/Suggester.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
import escapeRegExp from 'lodash/escapeRegExp';
2323
import SyntaxBase from '@/core/SyntaxBase';
2424
import { allSuggestList, suggesterKeywords } from '@/core/hooks/SuggestList';
25+
import { Pass } from 'codemirror/src/util/misc';
2526
import { isLookbehindSupported } from '@/utils/regexp';
2627
import { replaceLookbehind } from '@/utils/lookbehind-replace';
2728
import { isBrowser } from '@/utils/env';
2829

29-
// 导入 CodeMirror Pass(仅在完整版本中使用)
30-
import { Pass } from 'codemirror/src/util/misc';
31-
3230
/**
3331
* @typedef {import('codemirror')} CodeMirror
3432
*/

packages/cherry-markdown/types/index.engine.d.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)