add maxTextLength for perf

This commit is contained in:
fantasticit 2022-11-19 12:22:04 +08:00
parent 325e794322
commit 3ca99f255a
1 changed files with 29 additions and 18 deletions

View File

@ -34,37 +34,41 @@ function getDecorations({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength,
}: { }: {
doc: ProsemirrorNode; doc: ProsemirrorNode;
name: string; name: string;
lowlight: any; lowlight: any;
defaultLanguage: string | null | undefined; defaultLanguage: string | null | undefined;
maxTextLength: number;
}) { }) {
const decorations: Decoration[] = []; const decorations: Decoration[] = [];
findChildren(doc, (node) => node.type.name === name).forEach((block) => { findChildren(doc, (node) => node.type.name === name)
let from = block.pos + 1; .filter((block) => block.node.nodeSize <= maxTextLength)
const language = block.node.attrs.language || defaultLanguage; .forEach((block) => {
const languages = lowlight.listLanguages(); let from = block.pos + 1;
const nodes = const language = block.node.attrs.language || defaultLanguage;
language && languages.includes(language) const languages = lowlight.listLanguages();
? getHighlightNodes(lowlight.highlight(language, block.node.textContent)) const nodes =
: getHighlightNodes(lowlight.highlightAuto(block.node.textContent)); language && languages.includes(language)
? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
: getHighlightNodes(lowlight.highlightAuto(block.node.textContent));
parseNodes(nodes).forEach((node) => { parseNodes(nodes).forEach((node) => {
const to = from + node.text.length; const to = from + node.text.length;
if (node.classes.length) { if (node.classes.length) {
const decoration = Decoration.inline(from, to, { const decoration = Decoration.inline(from, to, {
class: node.classes.join(' '), class: node.classes.join(' '),
}); });
decorations.push(decoration); decorations.push(decoration);
} }
from = to; from = to;
});
}); });
});
return DecorationSet.create(doc, decorations); return DecorationSet.create(doc, decorations);
} }
@ -77,10 +81,12 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength,
}: { }: {
name: string; name: string;
lowlight: any; lowlight: any;
defaultLanguage: string | null | undefined; defaultLanguage: string | null | undefined;
maxTextLength: 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');
@ -96,6 +102,7 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength,
}), }),
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;
@ -135,6 +142,7 @@ export function LowlightPlugin({
name, name,
lowlight, lowlight,
defaultLanguage, defaultLanguage,
maxTextLength,
}); });
} }
@ -155,6 +163,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;
} }
export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({ export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({
@ -165,6 +174,7 @@ export const CodeBlock = BuiltInCodeBlock.extend<CodeBlockLowlightOptions>({
...this.parent?.(), ...this.parent?.(),
lowlight: {}, lowlight: {},
defaultLanguage: null, defaultLanguage: null,
maxTextLength: 200,
}; };
}, },
@ -179,6 +189,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,
}), }),
]; ];
}, },