From fc2d788f2a30adf647cbdae3a400155b02b8a8fe Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 11 Mar 2023 19:44:22 +0800 Subject: [PATCH] close #237 --- .../src/tiptap/core/extensions/code-block.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/client/src/tiptap/core/extensions/code-block.ts b/packages/client/src/tiptap/core/extensions/code-block.ts index 1c957b44..649f7d08 100644 --- a/packages/client/src/tiptap/core/extensions/code-block.ts +++ b/packages/client/src/tiptap/core/extensions/code-block.ts @@ -34,18 +34,20 @@ function getDecorations({ name, lowlight, defaultLanguage, - maxTextLength, + maxHighlightLineNumber, }: { doc: ProsemirrorNode; name: string; lowlight: any; defaultLanguage: string | null | undefined; - maxTextLength: number; + maxHighlightLineNumber: number; }) { const decorations: Decoration[] = []; findChildren(doc, (node) => node.type.name === name) - .filter((block) => block.node.nodeSize <= maxTextLength) + .filter((block) => { + return block.node.textContent.split('\n').length <= maxHighlightLineNumber; + }) .forEach((block) => { let from = block.pos + 1; const language = block.node.attrs.language || defaultLanguage; @@ -81,12 +83,12 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage, - maxTextLength, + maxHighlightLineNumber, }: { name: string; lowlight: any; defaultLanguage: string | null | undefined; - maxTextLength: number; + maxHighlightLineNumber: number; }) { if (!['highlight', 'highlightAuto', 'listLanguages'].every((api) => isFunction(lowlight[api]))) { throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension'); @@ -102,7 +104,7 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage, - maxTextLength, + maxHighlightLineNumber, }), apply: (transaction, decorationSet, oldState, newState) => { const oldNodeName = oldState.selection.$head.parent.type.name; @@ -142,7 +144,7 @@ export function LowlightPlugin({ name, lowlight, defaultLanguage, - maxTextLength, + maxHighlightLineNumber, }); } @@ -163,7 +165,7 @@ export function LowlightPlugin({ export interface CodeBlockLowlightOptions extends CodeBlockOptions { lowlight: any; defaultLanguage: string | null | undefined; - maxTextLength?: number; + maxHighlightLineNumber?: number; } export const CodeBlock = BuiltInCodeBlock.extend({ @@ -174,7 +176,7 @@ export const CodeBlock = BuiltInCodeBlock.extend({ ...this.parent?.(), lowlight: {}, defaultLanguage: null, - maxTextLength: 200, + maxHighlightLineNumber: 200, }; }, @@ -189,7 +191,7 @@ export const CodeBlock = BuiltInCodeBlock.extend({ name: this.name, lowlight: this.options.lowlight, defaultLanguage: this.options.defaultLanguage, - maxTextLength: this.options.maxTextLength || 200, + maxHighlightLineNumber: this.options.maxHighlightLineNumber || 200, }), ]; },