@haklex/rich-renderer-codeblock 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +48 -0
- package/dist/CodeBlockCard.d.ts +9 -0
- package/dist/CodeBlockCard.d.ts.map +1 -0
- package/dist/CodeBlockEditRenderer.d.ts +4 -0
- package/dist/CodeBlockEditRenderer.d.ts.map +1 -0
- package/dist/CodeBlockRenderer.d.ts +5 -0
- package/dist/CodeBlockRenderer.d.ts.map +1 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +338 -0
- package/dist/rich-renderer-codeblock.css +267 -0
- package/dist/shiki.d.ts +6 -0
- package/dist/shiki.d.ts.map +1 -0
- package/dist/styles.css.d.ts +2 -0
- package/dist/styles.css.d.ts.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Innei
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Additional Terms and Conditions
|
|
25
|
+
|
|
26
|
+
----------------
|
|
27
|
+
|
|
28
|
+
Use of this software is governed by the terms of MIT and, in addition, by the terms and conditions described in the additional file (ADDITIONAL_TERMS.md). By using this software, you agree to abide by these additional terms and conditions.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @haklex/rich-renderer-codeblock
|
|
2
|
+
|
|
3
|
+
基于 Shiki 的代码块渲染器。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @haklex/rich-renderer-codeblock @haklex/rich-editor shiki
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 导出
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
export { CodeBlockRenderer } from './CodeBlockRenderer'
|
|
15
|
+
export { CodeBlockEditRenderer } from './CodeBlockEditRenderer'
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 功能
|
|
19
|
+
|
|
20
|
+
- Shiki 语法高亮
|
|
21
|
+
- 自动检测语言
|
|
22
|
+
- 行号显示
|
|
23
|
+
- 复制按钮
|
|
24
|
+
- 自动适配深浅主题
|
|
25
|
+
|
|
26
|
+
## 使用
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { CodeBlockRenderer } from '@haklex/rich-renderer-codeblock'
|
|
30
|
+
import type { RendererConfig } from '@haklex/rich-editor'
|
|
31
|
+
|
|
32
|
+
const config: RendererConfig = {
|
|
33
|
+
CodeBlock: CodeBlockRenderer,
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 依赖
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"@haklex/rich-editor": "workspace:*",
|
|
42
|
+
"shiki": "^3.0.0"
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
interface CodeBlockCardProps {
|
|
3
|
+
code: string;
|
|
4
|
+
language: string;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare function CodeBlockCard({ code, language, children, }: CodeBlockCardProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=CodeBlockCard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeBlockCard.d.ts","sourceRoot":"","sources":["../src/CodeBlockCard.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AAGrB,OAAO,KAAK,EAAiB,SAAS,EAAE,MAAM,OAAO,CAAA;AAcrD,UAAU,kBAAkB;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED,wBAAgB,aAAa,CAAC,EAC5B,IAAI,EACJ,QAAQ,EACR,QAAQ,GACT,EAAE,kBAAkB,2CAwFpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeBlockEditRenderer.d.ts","sourceRoot":"","sources":["../src/CodeBlockEditRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AAEjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAO1C,eAAO,MAAM,qBAAqB,EAAE,aAAa,CAAC,sBAAsB,CA4KvE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeBlockRenderer.d.ts","sourceRoot":"","sources":["../src/CodeBlockRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AAEjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAO1C,eAAO,MAAM,iBAAiB,EAAE,aAAa,CAAC,sBAAsB,CA8DnE,CAAA;AAED,eAAe,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const languageToIconMap: Record<string, string>;
|
|
2
|
+
export declare const languageToColorMap: Record<string, string>;
|
|
3
|
+
export declare function normalizeLanguage(lang: string | undefined): string;
|
|
4
|
+
export declare function getLanguageDisplayName(lang: string): string;
|
|
5
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CA8BpD,CAAA;AAED,eAAO,MAAM,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CA8BrD,CAAA;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAGlE;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG3D"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,MAAM,qBAAqB,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useColorScheme } from "@haklex/rich-editor";
|
|
3
|
+
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
|
4
|
+
import { Check, Copy, ChevronDown } from "lucide-react";
|
|
5
|
+
const languageToIconMap = {
|
|
6
|
+
javascript: "JS",
|
|
7
|
+
js: "JS",
|
|
8
|
+
typescript: "TS",
|
|
9
|
+
ts: "TS",
|
|
10
|
+
jsx: "JSX",
|
|
11
|
+
tsx: "TSX",
|
|
12
|
+
html: "HTML",
|
|
13
|
+
css: "CSS",
|
|
14
|
+
scss: "SCSS",
|
|
15
|
+
json: "JSON",
|
|
16
|
+
markdown: "MD",
|
|
17
|
+
md: "MD",
|
|
18
|
+
bash: "SH",
|
|
19
|
+
sh: "SH",
|
|
20
|
+
shell: "SH",
|
|
21
|
+
zsh: "SH",
|
|
22
|
+
python: "PY",
|
|
23
|
+
py: "PY",
|
|
24
|
+
rust: "RS",
|
|
25
|
+
go: "GO",
|
|
26
|
+
java: "JAVA",
|
|
27
|
+
c: "C",
|
|
28
|
+
cpp: "C++",
|
|
29
|
+
"c++": "C++",
|
|
30
|
+
swift: "SW",
|
|
31
|
+
kotlin: "KT",
|
|
32
|
+
yaml: "YAML",
|
|
33
|
+
yml: "YAML",
|
|
34
|
+
sql: "SQL"
|
|
35
|
+
};
|
|
36
|
+
const languageToColorMap = {
|
|
37
|
+
javascript: "#f7df1e",
|
|
38
|
+
js: "#f7df1e",
|
|
39
|
+
typescript: "#3178c6",
|
|
40
|
+
ts: "#3178c6",
|
|
41
|
+
jsx: "#61dafb",
|
|
42
|
+
tsx: "#61dafb",
|
|
43
|
+
html: "#e34f26",
|
|
44
|
+
css: "#1572b6",
|
|
45
|
+
scss: "#cc6699",
|
|
46
|
+
json: "#f59e0b",
|
|
47
|
+
markdown: "#737373",
|
|
48
|
+
md: "#737373",
|
|
49
|
+
bash: "#4eaa25",
|
|
50
|
+
sh: "#4eaa25",
|
|
51
|
+
shell: "#4eaa25",
|
|
52
|
+
zsh: "#4eaa25",
|
|
53
|
+
python: "#3776ab",
|
|
54
|
+
py: "#3776ab",
|
|
55
|
+
rust: "#dea584",
|
|
56
|
+
go: "#00add8",
|
|
57
|
+
java: "#b07219",
|
|
58
|
+
c: "#a8b9cc",
|
|
59
|
+
cpp: "#00599c",
|
|
60
|
+
"c++": "#00599c",
|
|
61
|
+
swift: "#fa7343",
|
|
62
|
+
kotlin: "#7f52ff",
|
|
63
|
+
yaml: "#cb171e",
|
|
64
|
+
yml: "#cb171e",
|
|
65
|
+
sql: "#e38c00"
|
|
66
|
+
};
|
|
67
|
+
function normalizeLanguage(lang) {
|
|
68
|
+
if (!lang) return "text";
|
|
69
|
+
return lang.trim().toLowerCase() || "text";
|
|
70
|
+
}
|
|
71
|
+
function getLanguageDisplayName(lang) {
|
|
72
|
+
if (lang === "text") return "TEXT";
|
|
73
|
+
return lang.toUpperCase();
|
|
74
|
+
}
|
|
75
|
+
const CopyIcon = /* @__PURE__ */ jsx(Copy, { size: 16 });
|
|
76
|
+
const CheckIcon = /* @__PURE__ */ jsx(Check, { size: 16 });
|
|
77
|
+
const ExpandIcon = /* @__PURE__ */ jsx(ChevronDown, { size: 14 });
|
|
78
|
+
function CodeBlockCard({
|
|
79
|
+
code,
|
|
80
|
+
language,
|
|
81
|
+
children
|
|
82
|
+
}) {
|
|
83
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
84
|
+
const [copied, setCopied] = useState(false);
|
|
85
|
+
const copyTimerRef = useRef(void 0);
|
|
86
|
+
const [isCollapsed, setIsCollapsed] = useState(true);
|
|
87
|
+
const [isOverflow, setIsOverflow] = useState(false);
|
|
88
|
+
const scrollRef = useRef(null);
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
const el = scrollRef.current;
|
|
91
|
+
if (!el) return;
|
|
92
|
+
const check = () => {
|
|
93
|
+
const halfVh = window.innerHeight / 2;
|
|
94
|
+
setIsOverflow(el.scrollHeight >= halfVh);
|
|
95
|
+
};
|
|
96
|
+
const raf = requestAnimationFrame(check);
|
|
97
|
+
return () => cancelAnimationFrame(raf);
|
|
98
|
+
}, [code]);
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
return () => clearTimeout(copyTimerRef.current);
|
|
101
|
+
}, []);
|
|
102
|
+
const handleCopy = useCallback(() => {
|
|
103
|
+
navigator.clipboard.writeText(code).then(() => {
|
|
104
|
+
setCopied(true);
|
|
105
|
+
clearTimeout(copyTimerRef.current);
|
|
106
|
+
copyTimerRef.current = setTimeout(() => setCopied(false), 2e3);
|
|
107
|
+
}).catch(() => {
|
|
108
|
+
});
|
|
109
|
+
}, [code]);
|
|
110
|
+
const languageIcon = languageToIconMap[normalizedLanguage] || "";
|
|
111
|
+
const languageLabel = getLanguageDisplayName(normalizedLanguage);
|
|
112
|
+
const accent = languageToColorMap[normalizedLanguage] || "#737373";
|
|
113
|
+
const cardStyle = useMemo(
|
|
114
|
+
() => ({ "--rr-code-accent": accent }),
|
|
115
|
+
[accent]
|
|
116
|
+
);
|
|
117
|
+
const scrollClassName = [
|
|
118
|
+
"rr-code-scroll",
|
|
119
|
+
isCollapsed && isOverflow && "rr-code-scroll-collapsed"
|
|
120
|
+
].filter(Boolean).join(" ");
|
|
121
|
+
return /* @__PURE__ */ jsxs("div", { className: "rr-code-card", style: cardStyle, children: [
|
|
122
|
+
normalizedLanguage !== "text" && /* @__PURE__ */ jsx("div", { className: "rr-code-lang", "aria-hidden": true, children: languageIcon || languageLabel }),
|
|
123
|
+
/* @__PURE__ */ jsx(
|
|
124
|
+
"button",
|
|
125
|
+
{
|
|
126
|
+
type: "button",
|
|
127
|
+
className: "rr-code-copy",
|
|
128
|
+
onClick: handleCopy,
|
|
129
|
+
"aria-label": copied ? "Copied" : "Copy code",
|
|
130
|
+
children: copied ? CheckIcon : CopyIcon
|
|
131
|
+
}
|
|
132
|
+
),
|
|
133
|
+
/* @__PURE__ */ jsxs("div", { className: "rr-code-bg", children: [
|
|
134
|
+
/* @__PURE__ */ jsx("div", { ref: scrollRef, className: scrollClassName, children }),
|
|
135
|
+
isOverflow && isCollapsed && /* @__PURE__ */ jsx("div", { className: "rr-code-expand-wrap", children: /* @__PURE__ */ jsxs(
|
|
136
|
+
"button",
|
|
137
|
+
{
|
|
138
|
+
type: "button",
|
|
139
|
+
className: "rr-code-expand",
|
|
140
|
+
onClick: () => setIsCollapsed(false),
|
|
141
|
+
children: [
|
|
142
|
+
ExpandIcon,
|
|
143
|
+
/* @__PURE__ */ jsx("span", { children: "展开" })
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
) })
|
|
147
|
+
] })
|
|
148
|
+
] });
|
|
149
|
+
}
|
|
150
|
+
let highlighterPromise = null;
|
|
151
|
+
function getHighlighter() {
|
|
152
|
+
if (!highlighterPromise) {
|
|
153
|
+
highlighterPromise = import("shiki/bundle/web").then(
|
|
154
|
+
(mod) => mod.createHighlighter({
|
|
155
|
+
langs: [],
|
|
156
|
+
themes: ["github-light", "github-dark"]
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return highlighterPromise;
|
|
161
|
+
}
|
|
162
|
+
async function getHighlighterWithLang(language) {
|
|
163
|
+
const highlighter = await getHighlighter();
|
|
164
|
+
if (language && language !== "text" && language !== "plaintext") {
|
|
165
|
+
const loaded = highlighter.getLoadedLanguages();
|
|
166
|
+
if (!loaded.includes(language)) {
|
|
167
|
+
try {
|
|
168
|
+
await highlighter.loadLanguage(language);
|
|
169
|
+
} catch {
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return highlighter;
|
|
174
|
+
}
|
|
175
|
+
const SHIKI_THEMES = {
|
|
176
|
+
light: "github-light",
|
|
177
|
+
dark: "github-dark"
|
|
178
|
+
};
|
|
179
|
+
const CodeBlockEditRenderer = ({
|
|
180
|
+
code,
|
|
181
|
+
language,
|
|
182
|
+
showLineNumbers = true,
|
|
183
|
+
editable = false,
|
|
184
|
+
onCodeChange
|
|
185
|
+
}) => {
|
|
186
|
+
const colorScheme = useColorScheme();
|
|
187
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
188
|
+
const [loaded, setLoaded] = useState(false);
|
|
189
|
+
const containerRef = useRef(null);
|
|
190
|
+
const editorRef = useRef(null);
|
|
191
|
+
const onCodeChangeRef = useRef(onCodeChange);
|
|
192
|
+
onCodeChangeRef.current = onCodeChange;
|
|
193
|
+
const latestPropsRef = useRef({
|
|
194
|
+
code,
|
|
195
|
+
normalizedLanguage,
|
|
196
|
+
colorScheme,
|
|
197
|
+
showLineNumbers,
|
|
198
|
+
editable
|
|
199
|
+
});
|
|
200
|
+
latestPropsRef.current = {
|
|
201
|
+
code,
|
|
202
|
+
normalizedLanguage,
|
|
203
|
+
colorScheme,
|
|
204
|
+
showLineNumbers,
|
|
205
|
+
editable
|
|
206
|
+
};
|
|
207
|
+
useEffect(() => {
|
|
208
|
+
const container = containerRef.current;
|
|
209
|
+
if (!container) return;
|
|
210
|
+
let disposed = false;
|
|
211
|
+
let inputCleanup = null;
|
|
212
|
+
(async () => {
|
|
213
|
+
const props = latestPropsRef.current;
|
|
214
|
+
const [highlighter, { shikiCode }] = await Promise.all([
|
|
215
|
+
getHighlighterWithLang(props.normalizedLanguage),
|
|
216
|
+
import("shikicode")
|
|
217
|
+
]);
|
|
218
|
+
if (disposed) return;
|
|
219
|
+
const theme = SHIKI_THEMES[props.colorScheme];
|
|
220
|
+
const loaded2 = highlighter.getLoadedLanguages();
|
|
221
|
+
const lang = loaded2.includes(props.normalizedLanguage) ? props.normalizedLanguage : "text";
|
|
222
|
+
const editor = shikiCode().withOptions({
|
|
223
|
+
readOnly: !props.editable,
|
|
224
|
+
lineNumbers: props.showLineNumbers ? "on" : "off"
|
|
225
|
+
}).create(container, highlighter, {
|
|
226
|
+
value: props.code,
|
|
227
|
+
language: lang,
|
|
228
|
+
theme
|
|
229
|
+
});
|
|
230
|
+
if (!props.editable) {
|
|
231
|
+
editor.input.tabIndex = -1;
|
|
232
|
+
}
|
|
233
|
+
const handleInput = () => {
|
|
234
|
+
onCodeChangeRef.current?.(editor.input.value);
|
|
235
|
+
};
|
|
236
|
+
editor.input.addEventListener("input", handleInput);
|
|
237
|
+
inputCleanup = () => editor.input.removeEventListener("input", handleInput);
|
|
238
|
+
editorRef.current = editor;
|
|
239
|
+
setLoaded(true);
|
|
240
|
+
})();
|
|
241
|
+
return () => {
|
|
242
|
+
disposed = true;
|
|
243
|
+
inputCleanup?.();
|
|
244
|
+
editorRef.current?.dispose();
|
|
245
|
+
editorRef.current = null;
|
|
246
|
+
setLoaded(false);
|
|
247
|
+
};
|
|
248
|
+
}, []);
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
const editor = editorRef.current;
|
|
251
|
+
if (!editor) return;
|
|
252
|
+
let cancelled = false;
|
|
253
|
+
(async () => {
|
|
254
|
+
const { highlighter } = editor;
|
|
255
|
+
const loaded2 = highlighter.getLoadedLanguages();
|
|
256
|
+
if (normalizedLanguage !== "text" && normalizedLanguage !== "plaintext" && !loaded2.includes(normalizedLanguage)) {
|
|
257
|
+
try {
|
|
258
|
+
await highlighter.loadLanguage(normalizedLanguage);
|
|
259
|
+
} catch {
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (cancelled) return;
|
|
263
|
+
const resolvedLang = highlighter.getLoadedLanguages().includes(normalizedLanguage) ? normalizedLanguage : "text";
|
|
264
|
+
if (editor.input.value !== code) {
|
|
265
|
+
editor.input.value = code;
|
|
266
|
+
}
|
|
267
|
+
editor.updateOptions({
|
|
268
|
+
language: resolvedLang,
|
|
269
|
+
theme: SHIKI_THEMES[colorScheme],
|
|
270
|
+
lineNumbers: showLineNumbers ? "on" : "off",
|
|
271
|
+
readOnly: !editable
|
|
272
|
+
});
|
|
273
|
+
})();
|
|
274
|
+
return () => {
|
|
275
|
+
cancelled = true;
|
|
276
|
+
};
|
|
277
|
+
}, [code, normalizedLanguage, colorScheme, showLineNumbers, editable]);
|
|
278
|
+
const fallbackLines = useMemo(() => code.split("\n"), [code]);
|
|
279
|
+
const fallbackClassName = [
|
|
280
|
+
"rr-code-lined",
|
|
281
|
+
showLineNumbers && "rr-code-lined-ln"
|
|
282
|
+
].filter(Boolean).join(" ");
|
|
283
|
+
const bodyClassName = ["rr-code-body", !editable && "rr-code-readonly"].filter(Boolean).join(" ");
|
|
284
|
+
return /* @__PURE__ */ jsxs(CodeBlockCard, { code, language, children: [
|
|
285
|
+
!loaded && /* @__PURE__ */ jsx("pre", { className: fallbackClassName, children: /* @__PURE__ */ jsx("code", { children: fallbackLines.map((line, i) => /* @__PURE__ */ jsx("span", { className: "line", children: line }, i)) }) }),
|
|
286
|
+
/* @__PURE__ */ jsx(
|
|
287
|
+
"div",
|
|
288
|
+
{
|
|
289
|
+
ref: containerRef,
|
|
290
|
+
className: bodyClassName,
|
|
291
|
+
style: !loaded ? { height: 0, overflow: "hidden", visibility: "hidden" } : void 0
|
|
292
|
+
}
|
|
293
|
+
)
|
|
294
|
+
] });
|
|
295
|
+
};
|
|
296
|
+
const CodeBlockRenderer = ({
|
|
297
|
+
code,
|
|
298
|
+
language,
|
|
299
|
+
showLineNumbers = true
|
|
300
|
+
}) => {
|
|
301
|
+
const colorScheme = useColorScheme();
|
|
302
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
303
|
+
const [html, setHtml] = useState(null);
|
|
304
|
+
useEffect(() => {
|
|
305
|
+
let cancelled = false;
|
|
306
|
+
(async () => {
|
|
307
|
+
const highlighter = await getHighlighterWithLang(normalizedLanguage);
|
|
308
|
+
if (cancelled) return;
|
|
309
|
+
const loaded = highlighter.getLoadedLanguages();
|
|
310
|
+
const lang = loaded.includes(normalizedLanguage) ? normalizedLanguage : "text";
|
|
311
|
+
const result = highlighter.codeToHtml(code, {
|
|
312
|
+
lang,
|
|
313
|
+
theme: SHIKI_THEMES[colorScheme]
|
|
314
|
+
});
|
|
315
|
+
if (!cancelled) setHtml(result);
|
|
316
|
+
})();
|
|
317
|
+
return () => {
|
|
318
|
+
cancelled = true;
|
|
319
|
+
};
|
|
320
|
+
}, [code, normalizedLanguage, colorScheme]);
|
|
321
|
+
const fallbackLines = useMemo(() => code.split("\n"), [code]);
|
|
322
|
+
const linedClassName = [
|
|
323
|
+
"rr-code-lined",
|
|
324
|
+
showLineNumbers && "rr-code-lined-ln"
|
|
325
|
+
].filter(Boolean).join(" ");
|
|
326
|
+
return /* @__PURE__ */ jsx(CodeBlockCard, { code, language, children: html ? /* @__PURE__ */ jsx(
|
|
327
|
+
"div",
|
|
328
|
+
{
|
|
329
|
+
className: linedClassName,
|
|
330
|
+
dangerouslySetInnerHTML: { __html: html }
|
|
331
|
+
}
|
|
332
|
+
) : /* @__PURE__ */ jsx("pre", { className: linedClassName, children: /* @__PURE__ */ jsx("code", { children: fallbackLines.map((line, i) => /* @__PURE__ */ jsx("span", { className: "line", children: line }, i)) }) }) });
|
|
333
|
+
};
|
|
334
|
+
export {
|
|
335
|
+
CodeBlockEditRenderer,
|
|
336
|
+
CodeBlockRenderer,
|
|
337
|
+
CodeBlockRenderer as default
|
|
338
|
+
};
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
.rr-code-card {
|
|
2
|
+
--rr-code-accent: #737373;
|
|
3
|
+
position: relative;
|
|
4
|
+
margin: 1.5rem 0;
|
|
5
|
+
border-radius: 0.5rem;
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
font-size: 14px;
|
|
8
|
+
}
|
|
9
|
+
.rr-code-lang {
|
|
10
|
+
position: absolute;
|
|
11
|
+
bottom: 0.75rem;
|
|
12
|
+
right: 0.75rem;
|
|
13
|
+
z-index: 2;
|
|
14
|
+
font-size: 0.875rem;
|
|
15
|
+
opacity: 0.6;
|
|
16
|
+
pointer-events: none;
|
|
17
|
+
}
|
|
18
|
+
.rr-code-copy {
|
|
19
|
+
appearance: none;
|
|
20
|
+
position: absolute;
|
|
21
|
+
right: 0.5rem;
|
|
22
|
+
top: 0.5rem;
|
|
23
|
+
z-index: 3;
|
|
24
|
+
display: flex;
|
|
25
|
+
align-items: center;
|
|
26
|
+
justify-content: center;
|
|
27
|
+
padding: 0.375rem;
|
|
28
|
+
border-radius: 0.375rem;
|
|
29
|
+
border: 1px solid color-mix(in srgb, var(--rr-code-accent) 5%, transparent);
|
|
30
|
+
background: color-mix(in srgb, var(--rr-code-accent) 80%, transparent);
|
|
31
|
+
color: white;
|
|
32
|
+
cursor: pointer;
|
|
33
|
+
opacity: 0;
|
|
34
|
+
transition: opacity 0.2s ease;
|
|
35
|
+
backdrop-filter: blur(8px);
|
|
36
|
+
}
|
|
37
|
+
.rr-code-card:hover .rr-code-copy {
|
|
38
|
+
opacity: 1;
|
|
39
|
+
}
|
|
40
|
+
.rr-code-bg {
|
|
41
|
+
position: relative;
|
|
42
|
+
background: color-mix(in srgb, var(--rr-code-accent) 5%, transparent);
|
|
43
|
+
padding: 1rem 0;
|
|
44
|
+
}
|
|
45
|
+
.rr-code-scroll {
|
|
46
|
+
position: relative;
|
|
47
|
+
width: 100%;
|
|
48
|
+
overflow: auto;
|
|
49
|
+
}
|
|
50
|
+
.rr-code-scroll-collapsed {
|
|
51
|
+
max-height: 50vh;
|
|
52
|
+
overflow: hidden;
|
|
53
|
+
}
|
|
54
|
+
.rr-code-body {
|
|
55
|
+
position: relative;
|
|
56
|
+
overflow: auto;
|
|
57
|
+
background-color: transparent !important;
|
|
58
|
+
}
|
|
59
|
+
.rr-code-body > .shikicode.output {
|
|
60
|
+
position: static !important;
|
|
61
|
+
inset: auto !important;
|
|
62
|
+
}
|
|
63
|
+
.rr-code-readonly > textarea.shikicode {
|
|
64
|
+
display: none;
|
|
65
|
+
}
|
|
66
|
+
.rr-code-expand-wrap {
|
|
67
|
+
position: absolute;
|
|
68
|
+
bottom: 0;
|
|
69
|
+
left: 0;
|
|
70
|
+
right: 0;
|
|
71
|
+
display: flex;
|
|
72
|
+
justify-content: center;
|
|
73
|
+
padding: 0.5rem 0 0.75rem;
|
|
74
|
+
background: linear-gradient(to bottom, transparent 0%, var(--bg, var(--rc-bg-secondary)) 80%);
|
|
75
|
+
pointer-events: none;
|
|
76
|
+
}
|
|
77
|
+
.rr-code-expand {
|
|
78
|
+
appearance: none;
|
|
79
|
+
border: none;
|
|
80
|
+
background: none;
|
|
81
|
+
cursor: pointer;
|
|
82
|
+
display: flex;
|
|
83
|
+
align-items: center;
|
|
84
|
+
gap: 0.5rem;
|
|
85
|
+
font-size: 0.75rem;
|
|
86
|
+
color: inherit;
|
|
87
|
+
opacity: 0.7;
|
|
88
|
+
pointer-events: auto;
|
|
89
|
+
}
|
|
90
|
+
.rr-code-expand:hover {
|
|
91
|
+
opacity: 1;
|
|
92
|
+
}
|
|
93
|
+
.rr-code-card pre {
|
|
94
|
+
margin: 0 !important;
|
|
95
|
+
padding: 0 !important;
|
|
96
|
+
border-radius: 0;
|
|
97
|
+
font-size: min(1em, 16px);
|
|
98
|
+
}
|
|
99
|
+
.rr-code-card pre code {
|
|
100
|
+
display: flex;
|
|
101
|
+
flex-direction: column;
|
|
102
|
+
}
|
|
103
|
+
.rr-code-card .shiki, .rr-code-card code, .rr-code-card pre {
|
|
104
|
+
background: transparent !important;
|
|
105
|
+
}
|
|
106
|
+
.rr-code-card .shikicode.output .line::before {
|
|
107
|
+
background-color: transparent !important;
|
|
108
|
+
}
|
|
109
|
+
.rr-code-card .line {
|
|
110
|
+
display: block;
|
|
111
|
+
padding: 0 1.25rem;
|
|
112
|
+
}
|
|
113
|
+
.rr-code-card .shikicode.input.line-numbers {
|
|
114
|
+
padding-left: calc(5em + 1.25rem);
|
|
115
|
+
}
|
|
116
|
+
.rr-code-card .shikicode.input:not(.line-numbers) {
|
|
117
|
+
padding-left: 1.25rem;
|
|
118
|
+
}
|
|
119
|
+
.rr-code-card .line > span:last-child {
|
|
120
|
+
margin-right: 1.25rem;
|
|
121
|
+
}
|
|
122
|
+
.rr-code-card .line::after {
|
|
123
|
+
content: ' ';
|
|
124
|
+
}
|
|
125
|
+
.rr-code-card .highlighted, .rr-code-card .diff {
|
|
126
|
+
position: relative;
|
|
127
|
+
overflow-wrap: break-word;
|
|
128
|
+
}
|
|
129
|
+
.rr-code-card .highlighted::before, .rr-code-card .diff::before {
|
|
130
|
+
content: '';
|
|
131
|
+
position: absolute;
|
|
132
|
+
left: 0;
|
|
133
|
+
top: 0;
|
|
134
|
+
height: 100%;
|
|
135
|
+
width: 2px;
|
|
136
|
+
}
|
|
137
|
+
.rr-code-card .highlighted {
|
|
138
|
+
background: color-mix(in srgb, var(--rr-code-accent) 20%, transparent);
|
|
139
|
+
}
|
|
140
|
+
.rr-code-card .highlighted::before {
|
|
141
|
+
background: var(--rr-code-accent);
|
|
142
|
+
}
|
|
143
|
+
.rr-code-card .diff.add {
|
|
144
|
+
background: rgba(34, 197, 94, 0.15);
|
|
145
|
+
}
|
|
146
|
+
.rr-code-card .diff.add::before {
|
|
147
|
+
background: #22c55e;
|
|
148
|
+
}
|
|
149
|
+
.rr-code-card .diff.add::after {
|
|
150
|
+
content: ' +';
|
|
151
|
+
position: absolute;
|
|
152
|
+
left: 0;
|
|
153
|
+
color: #22c55e;
|
|
154
|
+
}
|
|
155
|
+
.rr-code-card .diff.remove {
|
|
156
|
+
background: rgba(239, 68, 68, 0.15);
|
|
157
|
+
}
|
|
158
|
+
.rr-code-card .diff.remove::before {
|
|
159
|
+
background: #ef4444;
|
|
160
|
+
}
|
|
161
|
+
.rr-code-card .diff.remove::after {
|
|
162
|
+
content: ' -';
|
|
163
|
+
position: absolute;
|
|
164
|
+
left: 0;
|
|
165
|
+
color: #ef4444;
|
|
166
|
+
}
|
|
167
|
+
.rr-code-lined {
|
|
168
|
+
counter-reset: shiki-line 0;
|
|
169
|
+
}
|
|
170
|
+
.rr-code-lined .line {
|
|
171
|
+
counter-increment: shiki-line 1;
|
|
172
|
+
}
|
|
173
|
+
.rr-code-lined .line::before {
|
|
174
|
+
content: counter(shiki-line);
|
|
175
|
+
color: transparent;
|
|
176
|
+
text-align: right;
|
|
177
|
+
box-sizing: border-box;
|
|
178
|
+
width: 2em;
|
|
179
|
+
display: inline-block;
|
|
180
|
+
position: sticky;
|
|
181
|
+
left: 0;
|
|
182
|
+
}
|
|
183
|
+
.rr-code-lined-ln .line::before {
|
|
184
|
+
color: inherit;
|
|
185
|
+
opacity: 0.4;
|
|
186
|
+
width: 5em;
|
|
187
|
+
padding-right: 2em;
|
|
188
|
+
}
|
|
189
|
+
.rr-code-scroll pre::-webkit-scrollbar-track {
|
|
190
|
+
margin-left: 1rem;
|
|
191
|
+
margin-right: var(--sr-margin, 0);
|
|
192
|
+
}
|
|
193
|
+
.rr-code-scroll pre::-webkit-scrollbar {
|
|
194
|
+
background-color: transparent !important;
|
|
195
|
+
}
|
|
196
|
+
.rich-code-block {
|
|
197
|
+
font-family: var(--rc-font-mono);
|
|
198
|
+
font-size: var(--rc-font-size-small);
|
|
199
|
+
background-color: var(--rc-code-bg);
|
|
200
|
+
border-radius: var(--rc-radius-md);
|
|
201
|
+
overflow: hidden;
|
|
202
|
+
margin: var(--rc-space-md) 0;
|
|
203
|
+
border: 1px solid var(--rc-border);
|
|
204
|
+
}
|
|
205
|
+
.rich-code-block pre {
|
|
206
|
+
margin: 0;
|
|
207
|
+
padding: var(--rc-space-md);
|
|
208
|
+
overflow-x: auto;
|
|
209
|
+
}
|
|
210
|
+
.rich-code-block code {
|
|
211
|
+
font-family: inherit;
|
|
212
|
+
font-size: inherit;
|
|
213
|
+
background: none;
|
|
214
|
+
border: none;
|
|
215
|
+
padding: 0;
|
|
216
|
+
color: inherit;
|
|
217
|
+
}
|
|
218
|
+
.rich-code-block-header {
|
|
219
|
+
display: flex;
|
|
220
|
+
align-items: center;
|
|
221
|
+
justify-content: space-between;
|
|
222
|
+
padding: var(--rc-space-sm) var(--rc-space-md);
|
|
223
|
+
border-bottom: 1px solid var(--rc-border);
|
|
224
|
+
font-size: var(--rc-font-size-small);
|
|
225
|
+
color: var(--rc-text-secondary);
|
|
226
|
+
user-select: none;
|
|
227
|
+
}
|
|
228
|
+
.rich-code-block-lang {
|
|
229
|
+
font-family: var(--rc-font-mono);
|
|
230
|
+
font-size: 0.85em;
|
|
231
|
+
text-transform: uppercase;
|
|
232
|
+
letter-spacing: 0.05em;
|
|
233
|
+
}
|
|
234
|
+
.rich-code-block-copy {
|
|
235
|
+
appearance: none;
|
|
236
|
+
border: none;
|
|
237
|
+
background: none;
|
|
238
|
+
color: var(--rc-text-secondary);
|
|
239
|
+
cursor: pointer;
|
|
240
|
+
padding: var(--rc-space-xs) var(--rc-space-sm);
|
|
241
|
+
border-radius: var(--rc-radius-sm);
|
|
242
|
+
font-family: var(--rc-font-family);
|
|
243
|
+
font-size: var(--rc-font-size-small);
|
|
244
|
+
line-height: 1;
|
|
245
|
+
transition: color 0.15s ease, background-color 0.15s ease;
|
|
246
|
+
}
|
|
247
|
+
.rich-code-block-copy:hover {
|
|
248
|
+
color: var(--rc-text);
|
|
249
|
+
background-color: var(--rc-bg-secondary);
|
|
250
|
+
}
|
|
251
|
+
.rich-code-block-numbered pre {
|
|
252
|
+
counter-reset: line;
|
|
253
|
+
}
|
|
254
|
+
.rich-code-block-numbered .line {
|
|
255
|
+
counter-increment: line;
|
|
256
|
+
}
|
|
257
|
+
.rich-code-block-numbered .line::before {
|
|
258
|
+
content: counter(line);
|
|
259
|
+
display: inline-block;
|
|
260
|
+
width: 2.5em;
|
|
261
|
+
margin-right: var(--rc-space-md);
|
|
262
|
+
text-align: right;
|
|
263
|
+
color: color-mix(in srgb, var(--rc-text-secondary) 40%, transparent);
|
|
264
|
+
opacity: 0.4;
|
|
265
|
+
user-select: none;
|
|
266
|
+
font-size: var(--rc-font-size-small);
|
|
267
|
+
}
|
package/dist/shiki.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shiki.d.ts","sourceRoot":"","sources":["../src/shiki.ts"],"names":[],"mappings":"AAcA,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,gBAe5D;AAED,eAAO,MAAM,YAAY;;;CAGf,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.css.d.ts","sourceRoot":"","sources":["../src/styles.css.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haklex/rich-renderer-codeblock",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Code block renderer with Shiki syntax highlighting",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./shiki": {
|
|
13
|
+
"import": "./dist/shiki.mjs",
|
|
14
|
+
"types": "./dist/shiki.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./constants": {
|
|
17
|
+
"import": "./dist/constants.mjs",
|
|
18
|
+
"types": "./dist/constants.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./style.css": "./dist/rich-renderer-codeblock.css"
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.mjs",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"lucide-react": "^0.574.0",
|
|
28
|
+
"shiki": "^3.21.0",
|
|
29
|
+
"shikicode": "*"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/react": "^19.0.0",
|
|
33
|
+
"@types/react-dom": "^19.0.0",
|
|
34
|
+
"@vanilla-extract/css": "^1.17.1",
|
|
35
|
+
"@vanilla-extract/vite-plugin": "^4.0.20",
|
|
36
|
+
"react": "19.2.4",
|
|
37
|
+
"react-dom": "19.2.4",
|
|
38
|
+
"typescript": "^5.9.0",
|
|
39
|
+
"vite": "^7.3.1",
|
|
40
|
+
"vite-plugin-dts": "^4.5.0",
|
|
41
|
+
"@haklex/rich-editor": "0.0.1",
|
|
42
|
+
"@haklex/rich-style-token": "0.0.1"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": ">=19",
|
|
46
|
+
"react-dom": ">=19",
|
|
47
|
+
"@haklex/rich-style-token": "0.0.1",
|
|
48
|
+
"@haklex/rich-editor": "0.0.1"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "vite build",
|
|
55
|
+
"dev:build": "vite build --watch"
|
|
56
|
+
},
|
|
57
|
+
"types": "./dist/index.d.ts"
|
|
58
|
+
}
|