Merge pull request #238 from fantasticit/fix/237

This commit is contained in:
fantasticit 2023-03-11 19:45:06 +08:00 committed by GitHub
commit 9a91dd2f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -34,18 +34,20 @@ function getDecorations({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength, maxHighlightLineNumber,
}: { }: {
doc: ProsemirrorNode; doc: ProsemirrorNode;
name: string; name: string;
lowlight: any; lowlight: any;
defaultLanguage: string | null | undefined; defaultLanguage: string | null | undefined;
maxTextLength: number; maxHighlightLineNumber: number;
}) { }) {
const decorations: Decoration[] = []; const decorations: Decoration[] = [];
findChildren(doc, (node) => node.type.name === name) 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) => { .forEach((block) => {
let from = block.pos + 1; let from = block.pos + 1;
const language = block.node.attrs.language || defaultLanguage; const language = block.node.attrs.language || defaultLanguage;
@ -81,12 +83,12 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength, maxHighlightLineNumber,
}: { }: {
name: string; name: string;
lowlight: any; lowlight: any;
defaultLanguage: string | null | undefined; defaultLanguage: string | null | undefined;
maxTextLength: number; maxHighlightLineNumber: number;
}) { }) {
if (!['highlight', 'highlightAuto', 'listLanguages'].every((api) => isFunction(lowlight[api]))) { 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'); throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension');
@ -102,7 +104,7 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength, maxHighlightLineNumber,
}), }),
apply: (transaction, decorationSet, oldState, newState) => { apply: (transaction, decorationSet, oldState, newState) => {
const oldNodeName = oldState.selection.$head.parent.type.name; const oldNodeName = oldState.selection.$head.parent.type.name;
@ -142,7 +144,7 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength, maxHighlightLineNumber,
}); });
} }
@ -163,7 +165,7 @@ export function LowlightPlugin({
export interface CodeBlockLowlightOptions extends CodeBlockOptions { export interface CodeBlockLowlightOptions extends CodeBlockOptions {
lowlight: any; lowlight: any;
defaultLanguage: string | null | undefined; defaultLanguage: string | null | undefined;
maxTextLength?: number; maxHighlightLineNumber?: number;
} }
export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({ export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({
@ -174,7 +176,7 @@ export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({
...this.parent?.(), ...this.parent?.(),
lowlight: {}, lowlight: {},
defaultLanguage: null, defaultLanguage: null,
maxTextLength: 200, maxHighlightLineNumber: 200,
}; };
}, },
@ -189,7 +191,7 @@ export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({
name: this.name, name: this.name,
lowlight: this.options.lowlight, lowlight: this.options.lowlight,
defaultLanguage: this.options.defaultLanguage, defaultLanguage: this.options.defaultLanguage,
maxTextLength: this.options.maxTextLength || 200, maxHighlightLineNumber: this.options.maxHighlightLineNumber || 200,
}), }),
]; ];
}, },