Skip to content

Commit 8a37698

Browse files
authored
fix(code-block): handle unknown languages better (#1626)
1 parent f202292 commit 8a37698

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

packages/core/src/blocks/CodeBlockContent/CodeBlockContent.ts

+29-17
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
126126
return null;
127127
}
128128

129-
return getLanguageId(options.editor.settings.codeBlock, language);
129+
return (
130+
getLanguageId(options.editor.settings.codeBlock, language) ??
131+
language
132+
);
130133
},
131134
renderHTML: (attributes) => {
132135
return attributes.language
@@ -251,7 +254,7 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
251254
if (process.env.NODE_ENV === "development" && !hasWarned) {
252255
// eslint-disable-next-line no-console
253256
console.log(
254-
"For syntax highlighting of code blocks, you must provide a highlighter function"
257+
"For syntax highlighting of code blocks, you must provide a `codeBlock.createHighlighter` function"
255258
);
256259
hasWarned = true;
257260
}
@@ -268,15 +271,22 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
268271
}
269272
);
270273
}
271-
272-
const language = parserOptions.language;
274+
const language = getLanguageId(
275+
options.editor.settings.codeBlock,
276+
parserOptions.language!
277+
);
273278

274279
if (
275-
language &&
276-
language !== "text" &&
277-
!highlighter.getLoadedLanguages().includes(language) &&
278-
language in options.editor.settings.codeBlock.supportedLanguages
280+
!language ||
281+
language === "text" ||
282+
language === "none" ||
283+
language === "plaintext" ||
284+
language === "txt"
279285
) {
286+
return [];
287+
}
288+
289+
if (!highlighter.getLoadedLanguages().includes(language)) {
280290
return highlighter.loadLanguage(language);
281291
}
282292

@@ -308,10 +318,9 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
308318
const $start = state.doc.resolve(range.from);
309319
const languageName = match[1].trim();
310320
const attributes = {
311-
language: getLanguageId(
312-
options.editor.settings.codeBlock,
313-
languageName
314-
),
321+
language:
322+
getLanguageId(options.editor.settings.codeBlock, languageName) ??
323+
languageName,
315324
};
316325

317326
if (
@@ -422,10 +431,13 @@ export const CodeBlock = createBlockSpecFromStronglyTypedTiptapNode(
422431
defaultCodeBlockPropSchema
423432
);
424433

425-
function getLanguageId(options: CodeBlockOptions, languageName: string) {
426-
return (
427-
Object.entries(options.supportedLanguages).find(([id, { aliases }]) => {
434+
function getLanguageId(
435+
options: CodeBlockOptions,
436+
languageName: string
437+
): string | undefined {
438+
return Object.entries(options.supportedLanguages).find(
439+
([id, { aliases }]) => {
428440
return aliases?.includes(languageName) || id === languageName;
429-
})?.[0] || languageName
430-
);
441+
}
442+
)?.[0];
431443
}

0 commit comments

Comments
 (0)