Skip to content

Commit 263319c

Browse files
committed
fix: handle “plain text” language label from notion
Notion code blocks using “plain text” lang label, we map to Shiki’s “plaintext”.
1 parent c6ad5c0 commit 263319c

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

example/src/record-map.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"21e33102-d54d-4154-acff-7c4e94c0785f",
1616
"57019bed-4e18-4950-a9a8-e966fb9c864c",
1717
"5f27d995-d073-4fa1-9682-dff187d02058",
18-
"6a3d1f3e-cb24-48c6-b7c0-b54279ef0f13"
18+
"6a3d1f3e-cb24-48c6-b7c0-b54279ef0f13",
19+
"4553fb9e-3909-4d2c-84a9-ff3b23ec6d7b"
1920
],
2021
"format": {
2122
"page_icon": "🕐",
@@ -209,6 +210,28 @@
209210
"space_id": "3f542fdc-ce07-4179-bf6e-7b7f20f9a6cf"
210211
},
211212
"role": "reader"
213+
},
214+
"4553fb9e-3909-4d2c-84a9-ff3b23ec6d7b": {
215+
"value": {
216+
"id": "4553fb9e-3909-4d2c-84a9-ff3b23ec6d7b",
217+
"version": 21,
218+
"type": "code",
219+
"properties": {
220+
"title": [["This is a plain text"]],
221+
"language": [["Plain text"]]
222+
},
223+
"created_time": 1704864013720,
224+
"last_edited_time": 1712449127438,
225+
"parent_id": "ec6b6e08-7ab5-4b08-b9a6-606eb5e1a02d",
226+
"parent_table": "block",
227+
"alive": true,
228+
"created_by_table": "notion_user",
229+
"created_by_id": "72f0d253-3ba6-48b5-8a2c-c66b08a20326",
230+
"last_edited_by_table": "notion_user",
231+
"last_edited_by_id": "72f0d253-3ba6-48b5-8a2c-c66b08a20326",
232+
"space_id": "3f542fdc-ce07-4179-bf6e-7b7f20f9a6cf"
233+
},
234+
"role": "reader"
212235
}
213236
},
214237
"space": {},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-notion-x-code-block",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Enhance your React-Notion-X projects with a code block component. This component offers out-of-the-box support for multiple programming languages and automatically adapts to light and dark themes",
55
"homepage": "https://react-notion-x-code-block.vercel.app",
66
"type": "module",

rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import commonjs from "@rollup/plugin-commonjs";
33
import babel from "@rollup/plugin-babel";
44
import externals from "rollup-plugin-node-externals";
55
import del from "rollup-plugin-delete";
6-
import pkg from "./package.json" assert { type: "json" };
76
import styles from "rollup-plugin-styles";
87

98
export default [

src/code.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { ObserverManager, type ObserverManagerProps } from "./manager";
1414
import styles from "./code.module.css";
1515

1616
import type { CodeBlock } from "notion-types";
17-
import type { BundledTheme } from "shiki/themes";
17+
import type { BundledTheme } from "shiki";
18+
import { langLabelConverter } from "./utils";
1819

1920
const _Code: React.FC<{
2021
block: CodeBlock;
@@ -55,10 +56,12 @@ const _Code: React.FC<{
5556
}, [IntersectionObserverOptions]);
5657

5758
const renderCodeToHtml = useCallback(async () => {
59+
const lang = langLabelConverter(
60+
(block.properties?.language?.[0]?.[0] || defaultLanguage).toLowerCase()
61+
);
62+
5863
const htmlCode = await codeToHtml(content, {
59-
lang: (
60-
block.properties?.language?.[0]?.[0] || defaultLanguage
61-
).toLowerCase(),
64+
lang,
6265
themes
6366
});
6467

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const ALIASES: Record<string, string> = {
2+
"plain text": "plaintext"
3+
};
4+
5+
/**
6+
* Normalize language labels from Notion to Shiki-recognized language name
7+
*/
8+
export function langLabelConverter(lang: string) {
9+
if (lang in ALIASES) return ALIASES[lang];
10+
return lang;
11+
}

0 commit comments

Comments
 (0)