Skip to content

Commit 7696b0c

Browse files
committed
feat: Implement captions display below images
1 parent 7e9d9ea commit 7696b0c

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

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

src/code.module.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@
99
width: 100%;
1010
font-size: 85%;
1111
}
12+
13+
/*
14+
There will use attributes variables have already been set in react-notion-x
15+
*/
16+
.codeCaption {
17+
padding: 6px 0 6px 2px;
18+
white-space: pre-wrap;
19+
word-break: break-word;
20+
caret-color: var(--fg-color);
21+
font-size: 14px;
22+
line-height: 1.4;
23+
color: var(--fg-color-3);
24+
}

src/code.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getBlockTitle } from "notion-utils";
22
import React, { useEffect, useState } from "react";
3-
import { cs, useNotionContext } from "react-notion-x";
3+
import { cs, useNotionContext, Text } from "react-notion-x";
44
import { codeToHtml } from "shiki";
55
import { BundledTheme } from "shiki/themes";
66

@@ -12,6 +12,7 @@ export const Code: React.FC<{
1212
block: CodeBlock;
1313
defaultLanguage?: string;
1414
className?: string;
15+
captionClassName?: string;
1516
themes?: {
1617
light: BundledTheme;
1718
dark: BundledTheme;
@@ -23,16 +24,17 @@ export const Code: React.FC<{
2324
light: "catppuccin-latte",
2425
dark: "dracula"
2526
},
26-
className
27+
className,
28+
captionClassName
2729
}) => {
2830
const { recordMap } = useNotionContext();
2931
const content = getBlockTitle(block, recordMap);
30-
3132
const [code, setCode] = useState<string | undefined>(undefined);
3233

3334
const language = (
3435
block.properties?.language?.[0]?.[0] || defaultLanguage
3536
).toLowerCase();
37+
const caption = block.properties.caption;
3638

3739
useEffect(() => {
3840
async function renderCodeToHtml() {
@@ -45,14 +47,18 @@ export const Code: React.FC<{
4547
content && renderCodeToHtml();
4648
}, [content, language, themes]);
4749

48-
return code ? (
49-
<div
50-
className={cs(styles.codeBlock, className)}
51-
dangerouslySetInnerHTML={{ __html: code }}
52-
/>
53-
) : (
54-
<div className={cs(styles.codeBlock, className)}>
55-
<pre>{content}</pre>
56-
</div>
50+
return (
51+
<figure className={cs(styles.codeBlock, className)}>
52+
{code == undefined ? (
53+
<pre>{content}</pre>
54+
) : (
55+
<div dangerouslySetInnerHTML={{ __html: code }} />
56+
)}
57+
{caption && (
58+
<figcaption className={cs(styles.codeCaption, captionClassName)}>
59+
<Text value={caption} block={block} />
60+
</figcaption>
61+
)}
62+
</figure>
5763
);
5864
};

0 commit comments

Comments
 (0)