Skip to content

Commit 6c89ca5

Browse files
areknawomatthewlipskiYousefED
authored
fix: CodeBlock fixes (select menu, bundled languages, paste from VS Code) (#1219)
* fix: Import only from a single Shiki bundle * fix: Select options styling on Chrome, Linux * fix: Detect proper language ID when parsing from HTML * fix: Support "typescriptreact" alias for tsx * minor code change * remove setting cursor position when inserting code block --------- Co-authored-by: matthewlipski <[email protected]> Co-authored-by: yousefed <[email protected]>
1 parent 65f15bb commit 6c89ca5

File tree

5 files changed

+63
-35
lines changed

5 files changed

+63
-35
lines changed

packages/core/src/api/clipboard/fromClipboard/handleVSCodePaste.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ export async function handleVSCodePaste<
1818
}
1919

2020
const text = event.clipboardData!.getData("text/plain");
21-
const vscode = event.clipboardData!.getData("vscode-editor-data");
22-
const vscodeData = vscode ? JSON.parse(vscode) : undefined;
23-
const language = vscodeData?.mode;
2421

2522
if (!text) {
2623
return false;
2724
}
2825

2926
if (!schema.nodes.codeBlock) {
3027
view.pasteText(text);
31-
3228
return true;
3329
}
3430

31+
const vscode = event.clipboardData!.getData("vscode-editor-data");
32+
const vscodeData = vscode ? JSON.parse(vscode) : undefined;
33+
const language = vscodeData?.mode;
34+
3535
if (!language) {
3636
return false;
3737
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
4747
};
4848
},
4949
addAttributes() {
50+
const supportedLanguages = this.options
51+
.supportedLanguages as SupportedLanguageConfig[];
52+
5053
return {
5154
language: {
5255
default: this.options.defaultLanguage,
5356
parseHTML: (inputElement) => {
5457
let element = inputElement as HTMLElement | null;
58+
let language: string | null = null;
5559

5660
if (
5761
element?.tagName === "DIV" &&
@@ -67,20 +71,26 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
6771
const dataLanguage = element?.getAttribute("data-language");
6872

6973
if (dataLanguage) {
70-
return dataLanguage.toLowerCase();
74+
language = dataLanguage.toLowerCase();
75+
} else {
76+
const classNames = [...(element?.className.split(" ") || [])];
77+
const languages = classNames
78+
.filter((className) => className.startsWith("language-"))
79+
.map((className) => className.replace("language-", ""));
80+
const [classLanguage] = languages;
81+
82+
language = classLanguage.toLowerCase();
7183
}
7284

73-
const classNames = [...(element?.className.split(" ") || [])];
74-
const languages = classNames
75-
.filter((className) => className.startsWith("language-"))
76-
.map((className) => className.replace("language-", ""));
77-
const [language] = languages;
78-
7985
if (!language) {
8086
return null;
8187
}
8288

83-
return language.toLowerCase();
89+
return (
90+
supportedLanguages.find(({ match }) => {
91+
return match.includes(language);
92+
})?.id || this.options.defaultLanguage
93+
);
8494
},
8595
renderHTML: (attributes) => {
8696
return attributes.language && attributes.language !== "text"

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bundledLanguagesInfo } from "shiki/bundle/web";
1+
import { bundledLanguagesInfo } from "shiki";
22

33
export type SupportedLanguageConfig = {
44
id: string;
@@ -14,30 +14,50 @@ export const defaultSupportedLanguages: SupportedLanguageConfig[] = [
1414
},
1515
...bundledLanguagesInfo
1616
.filter((lang) => {
17-
return ![
18-
"angular-html",
19-
"angular-ts",
20-
"astro",
21-
"blade",
22-
"coffee",
23-
"handlebars",
24-
"html-derivative",
25-
"http",
26-
"imba",
27-
"jinja",
28-
"jison",
29-
"json5",
30-
"marko",
31-
"mdc",
32-
"stylus",
33-
"ts-tags",
17+
return [
18+
"c",
19+
"cpp",
20+
"css",
21+
"glsl",
22+
"graphql",
23+
"haml",
24+
"html",
25+
"java",
26+
"javascript",
27+
"json",
28+
"jsonc",
29+
"jsonl",
30+
"jsx",
31+
"julia",
32+
"less",
33+
"markdown",
34+
"mdx",
35+
"php",
36+
"postcss",
37+
"pug",
38+
"python",
39+
"r",
40+
"regexp",
41+
"sass",
42+
"scss",
43+
"shellscript",
44+
"sql",
45+
"svelte",
46+
"typescript",
47+
"vue",
48+
"vue-html",
49+
"wasm",
50+
"wgsl",
51+
"xml",
52+
"yaml",
3453
].includes(lang.id);
3554
})
3655
.map((lang) => ({
3756
match: [lang.id, ...(lang.aliases || [])],
3857
id: lang.id,
3958
name: lang.name,
4059
})),
60+
{ id: "tsx", name: "TSX", match: ["tsx", "typescriptreact"] },
4161
{
4262
id: "haskell",
4363
name: "Haskell",

packages/core/src/editor/Block.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ NESTED BLOCKS
299299
transition: opacity 0.3s;
300300
transition-delay: 1s;
301301
}
302+
.bn-block-content[data-content-type="codeBlock"] > div > select > option {
303+
color: black;
304+
}
302305
.bn-block-content[data-content-type="codeBlock"]:hover > div > select,
303306
.bn-block-content[data-content-type="codeBlock"] > div > select:focus {
304307
opacity: 0.5;

packages/core/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,9 @@ export function getDefaultSlashMenuItems<
176176
if (checkDefaultBlockTypeInSchema("codeBlock", editor)) {
177177
items.push({
178178
onItemClick: () => {
179-
const pos = editor._tiptapEditor.state.selection.from;
180-
181179
insertOrUpdateBlock(editor, {
182180
type: "codeBlock",
183181
});
184-
185-
// Move the cursor inside the code block
186-
editor._tiptapEditor.commands.setTextSelection(pos);
187182
},
188183
badge: formatKeyboardShortcut("Mod-Alt-c"),
189184
key: "code_block",

0 commit comments

Comments
 (0)