Skip to content

Commit 4dcd7cf

Browse files
committed
feat: add optional lang label
1 parent 8e9e187 commit 4dcd7cf

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ import "./style.css";
7777
```
7878

7979
## API
80-
| Property | Description | Type | Default |
81-
|-----------------|----------------------------------------------------------|-----------|---------------------------------------------|
82-
| block | Receives render code content from `NotionRenderer` | CodeBlock | - |
83-
| className | Additional class for Code | string | - |
84-
| defaultLanguage | Default programming language if not specified in `block` | string | typescript |
80+
81+
| Property | Description | Type | Default |
82+
| --------------- | -------------------------------------------------------- | --------- | -------------------------------------------- |
83+
| block | Receives render code content from `NotionRenderer` | CodeBlock | - |
84+
| className | Additional class for Code | string | - |
85+
| defaultLanguage | Default programming language if not specified in `block` | string | typescript |
8586
| themes | Themes for rendering code | object | {light: "catppuccin-latte", dark: "dracula"} |
86-
| showCopy | Whether to show the copy button | boolean | true |
87+
| showCopy | Whether to show the copy button | boolean | true |
8788

8889
## Run the Example
8990

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.1.0",
3+
"version": "0.2.0",
44
"description": "",
55
"type": "module",
66
"module": "dist/index.js",

src/code.module.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
font-size: 85%;
1111
margin: 2px;
1212
position: relative;
13+
--appear-animation: opacity 0.1s linear;
1314
}
1415

1516
/*
@@ -50,3 +51,22 @@
5051
.codeBlock:hover .codeCopyButton {
5152
opacity: 1;
5253
}
54+
55+
.codeLanLabel {
56+
position: absolute;
57+
display: inline-block;
58+
left: 10px;
59+
top: 15px;
60+
text-align: center;
61+
padding: 0 12px;
62+
border: none;
63+
background-color: var(--bg-color);
64+
color: var(--fg-color);
65+
border-radius: 4px;
66+
opacity: 0;
67+
transition: opacity 0.1s linear;
68+
}
69+
70+
.codeBlock:hover .codeLanLabel {
71+
opacity: 1;
72+
}

src/code.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const Code: React.FC<{
1313
defaultLanguage?: string;
1414
className?: string;
1515
showCopy?: boolean;
16+
showLangLabel?: boolean;
1617
themes?: {
1718
light: BundledTheme;
1819
dark: BundledTheme;
@@ -25,29 +26,32 @@ export const Code: React.FC<{
2526
light: "catppuccin-latte",
2627
dark: "dracula"
2728
},
28-
showCopy = true
29+
showCopy = true,
30+
showLangLabel = true
2931
}) => {
3032
const { recordMap } = useNotionContext();
31-
const [isCopied, setIsCopied] = useState(false);
33+
3234
const content = getBlockTitle(block, recordMap);
3335
const [code, setCode] = useState<string | undefined>(undefined);
36+
const [isCopied, setIsCopied] = useState(false);
37+
3438
const timer = useRef<null | number>(null);
3539

36-
const language = (
37-
block.properties?.language?.[0]?.[0] || defaultLanguage
38-
).toLowerCase();
3940
const caption = block.properties.caption;
4041

42+
const providedLangType = block.properties?.language?.[0]?.[0];
43+
const lang = (providedLangType || defaultLanguage).toLowerCase();
44+
4145
useEffect(() => {
4246
async function renderCodeToHtml() {
4347
const htmlCode = await codeToHtml(content, {
44-
lang: language,
48+
lang,
4549
themes
4650
});
4751
setCode(htmlCode);
4852
}
4953
content && renderCodeToHtml();
50-
}, [content, language, themes]);
54+
}, [content, lang, themes]);
5155

5256
const clickCopy = useCallback(() => {
5357
navigator.clipboard.writeText(content).then(() => {
@@ -65,11 +69,11 @@ export const Code: React.FC<{
6569

6670
return (
6771
<figure className={cs(styles.codeBlock, className)}>
72+
{showLangLabel && providedLangType ? (
73+
<span className={styles.codeLanLabel}>{providedLangType}</span>
74+
) : null}
6875
{showCopy ? (
69-
<button
70-
onClick={clickCopy}
71-
className={styles.codeCopyButton}
72-
>
76+
<button onClick={clickCopy} className={styles.codeCopyButton}>
7377
<IoMdCopy />
7478
{isCopied ? "Copied" : "Copy"}
7579
</button>

0 commit comments

Comments
 (0)