Skip to content

Commit ada149b

Browse files
add katex for equation
fix image caption
1 parent f8e2404 commit ada149b

File tree

6 files changed

+70
-46
lines changed

6 files changed

+70
-46
lines changed

typescript/src/renderer/components/blocks/EquationBlockRenderer.tsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React from "react";
1+
import React, { useEffect, useRef } from "react";
2+
import katex from "katex";
23

34
import { EquationBlock } from "@/models/generated";
45

@@ -14,6 +15,25 @@ export const EquationBlockRenderer: React.FC<EquationBlockRendererProps> = ({
1415
...props
1516
}) => {
1617
const equationData = block.equation;
18+
const containerRef = useRef<HTMLDivElement>(null);
19+
20+
useEffect(() => {
21+
if (containerRef.current && equationData?.expression) {
22+
try {
23+
katex.render(equationData.expression, containerRef.current, {
24+
displayMode: true,
25+
throwOnError: false,
26+
errorColor: '#cc0000',
27+
strict: 'warn'
28+
});
29+
} catch (error) {
30+
console.warn('KaTeX render error:', error);
31+
if (containerRef.current) {
32+
containerRef.current.textContent = equationData.expression;
33+
}
34+
}
35+
}
36+
}, [equationData?.expression]);
1737

1838
return (
1939
<div
@@ -23,9 +43,10 @@ export const EquationBlockRenderer: React.FC<EquationBlockRendererProps> = ({
2343
>
2444
<div>
2545
<div className="notion-equation-display">
26-
<div className="notion-equation-content">
27-
{equationData?.expression || ""}
28-
</div>
46+
<div
47+
className="notion-equation-content"
48+
ref={containerRef}
49+
/>
2950
</div>
3051
</div>
3152
</div>

typescript/src/renderer/components/blocks/ToggleBlockRenderer.tsx

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,40 @@ export const ToggleBlockRenderer: React.FC<ToggleBlockRendererProps> = ({
3535
<div
3636
aria-expanded={isOpen}
3737
aria-label={isOpen ? "Close" : "Open"}
38-
role="button"
3938
tabIndex={0}
40-
onClick={handleToggle}
4139
onKeyDown={(e) => {
4240
if (e.key === "Enter" || e.key === " ") {
4341
e.preventDefault();
4442
handleToggle();
4543
}
4644
}}
47-
style={{ cursor: "pointer", display: "flex", alignItems: "center" }}
45+
style={{
46+
display: "flex",
47+
alignItems: "center",
48+
gap: 4,
49+
cursor: "pointer",
50+
}}
4851
>
49-
{/* <svg
50-
aria-hidden="true"
51-
className="arrowCaretDownFillSmall"
52-
role="graphics-symbol"
53-
viewBox="0 0 16 16"
54-
style={{
55-
width: 16,
56-
height: 16,
57-
transform: isOpen ? "rotate(90deg)" : "rotate(0deg)",
58-
transition: "transform 0.2s ease",
59-
}}
60-
>
61-
<path d="M6 12l6-6H6z" fill="currentColor" />
62-
</svg> */}
52+
<div role="button" onClick={handleToggle}>
53+
<svg
54+
aria-hidden="true"
55+
role="graphics-symbol"
56+
viewBox="0 0 16 16"
57+
className="arrowCaretDownFillSmall"
58+
style={{
59+
width: "1.2em",
60+
height: "1.2em",
61+
display: "block",
62+
flexShrink: 0,
63+
transition: "transform 200ms ease-out",
64+
transform: isOpen ? "rotate(360deg)" : "rotate(270deg)",
65+
userSelect: "none",
66+
}}
67+
>
68+
<path d="M2.835 3.25a.8.8 0 0 0-.69 1.203l5.164 8.854a.8.8 0 0 0 1.382 0l5.165-8.854a.8.8 0 0 0-.691-1.203z"></path>
69+
</svg>
70+
</div>
6371

64-
<svg
65-
aria-hidden="true"
66-
role="graphics-symbol"
67-
viewBox="0 0 16 16"
68-
className="arrowCaretDownFillSmall"
69-
style={{
70-
width: "0.8em",
71-
height: "0.8em",
72-
display: "block",
73-
flexShrink: 0,
74-
transition: "transform 200ms ease-out",
75-
// transform: "rotateZ(0deg)",
76-
opacity: 1,
77-
transform: isOpen ? "rotate(360deg)" : "rotate(270deg)",
78-
marginRight: 6,
79-
// tran: "transform 0.2s ease",
80-
}}
81-
>
82-
<path d="M2.835 3.25a.8.8 0 0 0-.69 1.203l5.164 8.854a.8.8 0 0 0 1.382 0l5.165-8.854a.8.8 0 0 0-.691-1.203z"></path>
83-
</svg>
8472
<div className="notranslate">
8573
<RichTextRenderer richText={toggleData?.rich_text || []} />
8674
</div>

typescript/src/renderer/styles/blocks.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@
111111
flex: 1;
112112
}
113113

114+
.notion-toggle-block div[role="button"] {
115+
display: flex;
116+
justify-content: center;
117+
align-items: center;
118+
padding: 4px;
119+
}
120+
.notion-toggle-block div[role="button"]:hover {
121+
background-color: var(--jsondoc-bg);
122+
}
114123
/* Equation Block */
115124
.notion-equation-block {
116125
padding: var(--jsondoc-spacing-sm) var(--jsondoc-spacing-xs);
@@ -120,7 +129,6 @@
120129
.notion-equation-display {
121130
text-align: center;
122131
padding: var(--jsondoc-spacing-lg);
123-
background: var(--jsondoc-bg-code);
124132
border-radius: var(--jsondoc-radius-sm);
125133
}
126134

typescript/src/renderer/styles/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* JSON-DOC Renderer Styles */
22

3+
/* Import KaTeX styles for equations */
4+
@import "katex/dist/katex.min.css";
5+
36
/* Import design tokens first */
47
@import "./variables.css";
58

typescript/src/renderer/styles/variables.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
--jsondoc-bg-code: rgb(247, 246, 243);
1212
--jsondoc-bg-inline-code: rgba(135, 131, 120, 0.15);
1313
--jsondoc-bg-unsupported: rgba(255, 0, 0, 0.1);
14-
15-
/* Text Colors */
16-
--jsondoc-color-gray: rgba(120, 119, 116, 1);
14+
--jsondoc-bg: rgba(55, 53, 47, 0.45);
15+
--jsondoc-foreground: #37352f /* Text Colors */
16+
--jsondoc-color-gray: rgba(120, 119, 116, 1);
1717
--jsondoc-color-brown: rgba(159, 107, 83, 1);
1818
--jsondoc-color-orange: rgba(217, 115, 13, 1);
1919
--jsondoc-color-yellow: rgba(203, 145, 47, 1);
@@ -109,6 +109,8 @@
109109
--jsondoc-border-medium: rgb(233, 233, 231);
110110
--jsondoc-bg-code: rgb(247, 246, 243);
111111
--jsondoc-bg-inline-code: rgba(135, 131, 120, 0.15);
112+
--jsondoc-bg: rgba(255, 255, 255, 0.8);
113+
--jsondoc-foreground: #37352f;
112114
}
113115

114116
.jsondoc-theme-dark {
@@ -119,4 +121,6 @@
119121
--jsondoc-border-medium: rgba(255, 255, 255, 0.2);
120122
--jsondoc-bg-code: rgba(255, 255, 255, 0.05);
121123
--jsondoc-bg-inline-code: rgba(255, 255, 255, 0.1);
124+
--jsondoc-bg: rgba(55, 53, 47, 0.45);
125+
--jsondoc-foreground: #37352f;
122126
}

typescript/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
format: ["cjs", "esm"],
66
dts: true,
77
splitting: false,
8-
sourcemap: true,
8+
// sourcemap: true,
99
clean: true,
1010
external: ["react", "react-dom"],
1111
esbuildOptions(options) {

0 commit comments

Comments
 (0)