@haklex/rich-renderer-codeblock 0.0.80 → 0.0.82

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.
@@ -0,0 +1,134 @@
1
+ import { getLanguageDisplayName, languageToColorMap, normalizeLanguage } from "./constants.mjs";
2
+ import { _ as semanticClassNames, a as bodyBackground, c as copyButton, h as scroll, n as LanguageIcon, r as hasLanguageIcon, s as card } from "./icons-CnFW5y6g.js";
3
+ import { SHIKI_DUAL_THEMES, getHighlighterWithLang } from "./shiki.mjs";
4
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
+ import { Check, ChevronDown, Copy } from "lucide-react";
6
+ import { jsx, jsxs } from "react/jsx-runtime";
7
+ //#region src/CodeBlockCard.tsx
8
+ var CopyIcon = /* @__PURE__ */ jsx(Copy, { size: 16 });
9
+ var CheckIcon = /* @__PURE__ */ jsx(Check, { size: 16 });
10
+ var ExpandIcon = /* @__PURE__ */ jsx(ChevronDown, { size: 14 });
11
+ function CodeBlockCard({ code, language, collapsible = true, langSlot, children }) {
12
+ const normalizedLanguage = normalizeLanguage(language);
13
+ const [copied, setCopied] = useState(false);
14
+ const copyTimerRef = useRef(void 0);
15
+ const [isCollapsed, setIsCollapsed] = useState(true);
16
+ const [isOverflow, setIsOverflow] = useState(false);
17
+ const scrollRef = useRef(null);
18
+ useEffect(() => {
19
+ if (!collapsible) {
20
+ setIsOverflow(false);
21
+ return;
22
+ }
23
+ const el = scrollRef.current;
24
+ if (!el) return;
25
+ const check = () => {
26
+ const halfVh = window.innerHeight / 2;
27
+ setIsOverflow(el.scrollHeight >= halfVh);
28
+ };
29
+ const raf = requestAnimationFrame(check);
30
+ return () => cancelAnimationFrame(raf);
31
+ }, [code, collapsible]);
32
+ useEffect(() => {
33
+ return () => clearTimeout(copyTimerRef.current);
34
+ }, []);
35
+ const handleCopy = useCallback(() => {
36
+ navigator.clipboard.writeText(code).then(() => {
37
+ setCopied(true);
38
+ clearTimeout(copyTimerRef.current);
39
+ copyTimerRef.current = setTimeout(() => setCopied(false), 2e3);
40
+ }).catch(() => {});
41
+ }, [code]);
42
+ const languageLabel = getLanguageDisplayName(normalizedLanguage);
43
+ const accent = languageToColorMap[normalizedLanguage] || "#737373";
44
+ const cardStyle = useMemo(() => ({ "--rr-code-accent": accent }), [accent]);
45
+ const scrollClassName = [
46
+ scroll,
47
+ semanticClassNames.scroll,
48
+ collapsible && isCollapsed && isOverflow && "_1pn9r4q7",
49
+ collapsible && isCollapsed && isOverflow && semanticClassNames.scrollCollapsed
50
+ ].filter(Boolean).join(" ");
51
+ return /* @__PURE__ */ jsxs("div", {
52
+ className: `${card} ${semanticClassNames.card}`,
53
+ style: cardStyle,
54
+ children: [
55
+ langSlot ?? (normalizedLanguage !== "text" && /* @__PURE__ */ jsx("div", {
56
+ "aria-hidden": true,
57
+ className: `_1pn9r4q2 ${semanticClassNames.lang}`,
58
+ children: hasLanguageIcon(normalizedLanguage) ? /* @__PURE__ */ jsx(LanguageIcon, {
59
+ language: normalizedLanguage,
60
+ size: 14
61
+ }) : /* @__PURE__ */ jsx("span", { children: languageLabel })
62
+ })),
63
+ /* @__PURE__ */ jsx("button", {
64
+ "aria-label": copied ? "Copied" : "Copy code",
65
+ className: `${copyButton} ${semanticClassNames.copyButton}`,
66
+ type: "button",
67
+ onClick: handleCopy,
68
+ children: copied ? CheckIcon : CopyIcon
69
+ }),
70
+ /* @__PURE__ */ jsxs("div", {
71
+ className: `${bodyBackground} ${semanticClassNames.bodyBackground}`,
72
+ children: [/* @__PURE__ */ jsx("div", {
73
+ className: scrollClassName,
74
+ ref: scrollRef,
75
+ children
76
+ }), collapsible && isOverflow && isCollapsed && /* @__PURE__ */ jsx("div", {
77
+ className: `_1pn9r4qa ${semanticClassNames.expandWrap}`,
78
+ children: /* @__PURE__ */ jsxs("button", {
79
+ className: `_1pn9r4qb ${semanticClassNames.expandButton}`,
80
+ type: "button",
81
+ onClick: () => setIsCollapsed(false),
82
+ children: [ExpandIcon, /* @__PURE__ */ jsx("span", { children: "展开" })]
83
+ })
84
+ })]
85
+ })
86
+ ]
87
+ });
88
+ }
89
+ //#endregion
90
+ //#region src/CodeBlockRenderer.tsx
91
+ var CodeBlockRenderer = ({ code, language, showLineNumbers: showLineNumbersProp }) => {
92
+ const showLineNumbers = showLineNumbersProp ?? false;
93
+ const normalizedLanguage = normalizeLanguage(language);
94
+ const [html, setHtml] = useState(null);
95
+ useEffect(() => {
96
+ let cancelled = false;
97
+ (async () => {
98
+ const highlighter = await getHighlighterWithLang(normalizedLanguage);
99
+ if (cancelled) return;
100
+ const lang = highlighter.getLoadedLanguages().includes(normalizedLanguage) ? normalizedLanguage : "text";
101
+ const result = highlighter.codeToHtml(code, {
102
+ lang,
103
+ themes: SHIKI_DUAL_THEMES
104
+ });
105
+ if (!cancelled) setHtml(result);
106
+ })();
107
+ return () => {
108
+ cancelled = true;
109
+ };
110
+ }, [code, normalizedLanguage]);
111
+ const fallbackLines = useMemo(() => code.split("\n"), [code]);
112
+ const linedClassName = [
113
+ showLineNumbers && "_1pn9r4qc",
114
+ showLineNumbers && semanticClassNames.lined,
115
+ showLineNumbers && "_1pn9r4qd",
116
+ showLineNumbers && semanticClassNames.linedWithNumbers
117
+ ].filter(Boolean).join(" ");
118
+ return /* @__PURE__ */ jsx(CodeBlockCard, {
119
+ code,
120
+ language,
121
+ children: html ? /* @__PURE__ */ jsx("div", {
122
+ className: linedClassName,
123
+ dangerouslySetInnerHTML: { __html: html }
124
+ }) : /* @__PURE__ */ jsx("pre", {
125
+ className: linedClassName,
126
+ children: /* @__PURE__ */ jsx("code", { children: fallbackLines.map((line, i) => /* @__PURE__ */ jsx("span", {
127
+ className: "line",
128
+ children: line
129
+ }, i)) })
130
+ })
131
+ });
132
+ };
133
+ //#endregion
134
+ export { CodeBlockCard as n, CodeBlockRenderer as t };
@@ -1,76 +1,73 @@
1
- const languageToIconMap = {
2
- javascript: "JS",
3
- js: "JS",
4
- typescript: "TS",
5
- ts: "TS",
6
- jsx: "JSX",
7
- tsx: "TSX",
8
- html: "HTML",
9
- css: "CSS",
10
- scss: "SCSS",
11
- json: "JSON",
12
- markdown: "MD",
13
- md: "MD",
14
- bash: "SH",
15
- sh: "SH",
16
- shell: "SH",
17
- zsh: "SH",
18
- python: "PY",
19
- py: "PY",
20
- rust: "RS",
21
- go: "GO",
22
- java: "JAVA",
23
- c: "C",
24
- cpp: "C++",
25
- "c++": "C++",
26
- swift: "SW",
27
- kotlin: "KT",
28
- yaml: "YAML",
29
- yml: "YAML",
30
- sql: "SQL"
1
+ //#region src/constants.ts
2
+ var languageToIconMap = {
3
+ javascript: "JS",
4
+ js: "JS",
5
+ typescript: "TS",
6
+ ts: "TS",
7
+ jsx: "JSX",
8
+ tsx: "TSX",
9
+ html: "HTML",
10
+ css: "CSS",
11
+ scss: "SCSS",
12
+ json: "JSON",
13
+ markdown: "MD",
14
+ md: "MD",
15
+ bash: "SH",
16
+ sh: "SH",
17
+ shell: "SH",
18
+ zsh: "SH",
19
+ python: "PY",
20
+ py: "PY",
21
+ rust: "RS",
22
+ go: "GO",
23
+ java: "JAVA",
24
+ c: "C",
25
+ cpp: "C++",
26
+ "c++": "C++",
27
+ swift: "SW",
28
+ kotlin: "KT",
29
+ yaml: "YAML",
30
+ yml: "YAML",
31
+ sql: "SQL"
31
32
  };
32
- const languageToColorMap = {
33
- javascript: "#f7df1e",
34
- js: "#f7df1e",
35
- typescript: "#3178c6",
36
- ts: "#3178c6",
37
- jsx: "#61dafb",
38
- tsx: "#61dafb",
39
- html: "#e34f26",
40
- css: "#1572b6",
41
- scss: "#cc6699",
42
- json: "#f59e0b",
43
- markdown: "#737373",
44
- md: "#737373",
45
- bash: "#4eaa25",
46
- sh: "#4eaa25",
47
- shell: "#4eaa25",
48
- zsh: "#4eaa25",
49
- python: "#3776ab",
50
- py: "#3776ab",
51
- rust: "#dea584",
52
- go: "#00add8",
53
- java: "#b07219",
54
- c: "#a8b9cc",
55
- cpp: "#00599c",
56
- "c++": "#00599c",
57
- swift: "#fa7343",
58
- kotlin: "#7f52ff",
59
- yaml: "#cb171e",
60
- yml: "#cb171e",
61
- sql: "#e38c00"
33
+ var languageToColorMap = {
34
+ javascript: "#f7df1e",
35
+ js: "#f7df1e",
36
+ typescript: "#3178c6",
37
+ ts: "#3178c6",
38
+ jsx: "#61dafb",
39
+ tsx: "#61dafb",
40
+ html: "#e34f26",
41
+ css: "#1572b6",
42
+ scss: "#cc6699",
43
+ json: "#f59e0b",
44
+ markdown: "#737373",
45
+ md: "#737373",
46
+ bash: "#4eaa25",
47
+ sh: "#4eaa25",
48
+ shell: "#4eaa25",
49
+ zsh: "#4eaa25",
50
+ python: "#3776ab",
51
+ py: "#3776ab",
52
+ rust: "#dea584",
53
+ go: "#00add8",
54
+ java: "#b07219",
55
+ c: "#a8b9cc",
56
+ cpp: "#00599c",
57
+ "c++": "#00599c",
58
+ swift: "#fa7343",
59
+ kotlin: "#7f52ff",
60
+ yaml: "#cb171e",
61
+ yml: "#cb171e",
62
+ sql: "#e38c00"
62
63
  };
63
64
  function normalizeLanguage(lang) {
64
- if (!lang) return "text";
65
- return lang.trim().toLowerCase() || "text";
65
+ if (!lang) return "text";
66
+ return lang.trim().toLowerCase() || "text";
66
67
  }
67
68
  function getLanguageDisplayName(lang) {
68
- if (lang === "text") return "TEXT";
69
- return lang.toUpperCase();
69
+ if (lang === "text") return "TEXT";
70
+ return lang.toUpperCase();
70
71
  }
71
- export {
72
- getLanguageDisplayName,
73
- languageToColorMap,
74
- languageToIconMap,
75
- normalizeLanguage
76
- };
72
+ //#endregion
73
+ export { getLanguageDisplayName, languageToColorMap, languageToIconMap, normalizeLanguage };
@@ -0,0 +1,230 @@
1
+ import { useMemo } from "react";
2
+ import { getIconData, iconToHTML, iconToSVG } from "@iconify/utils";
3
+ import { icons } from "@iconify-json/material-icon-theme";
4
+ import { jsx } from "react/jsx-runtime";
5
+ //#region src/icons/material-icon.ts
6
+ /** Get SVG markup for a material-icon-theme icon by name. Returns null if not found. */
7
+ function getMaterialIconSvg(iconName) {
8
+ const data = getIconData(icons, iconName);
9
+ if (!data) return null;
10
+ const svg = iconToSVG(data);
11
+ return iconToHTML(svg.body, svg.attributes);
12
+ }
13
+ //#endregion
14
+ //#region src/icons/file.tsx
15
+ /** Maps file extension to material-icon-theme icon name. */
16
+ var EXT_TO_ICON = {
17
+ ts: "typescript",
18
+ tsx: "react-ts",
19
+ mts: "typescript",
20
+ cts: "typescript",
21
+ js: "javascript",
22
+ jsx: "react",
23
+ mjs: "javascript",
24
+ cjs: "javascript",
25
+ py: "python",
26
+ rs: "rust",
27
+ go: "go",
28
+ java: "java",
29
+ html: "html",
30
+ htm: "html",
31
+ css: "css",
32
+ scss: "sass",
33
+ sass: "sass",
34
+ less: "sass",
35
+ json: "json",
36
+ jsonc: "json",
37
+ json5: "json",
38
+ md: "markdown",
39
+ mdx: "markdown",
40
+ sh: "console",
41
+ bash: "console",
42
+ zsh: "console",
43
+ yml: "yaml",
44
+ yaml: "yaml",
45
+ sql: "database",
46
+ c: "c",
47
+ h: "c",
48
+ cpp: "cpp",
49
+ cc: "cpp",
50
+ cxx: "cpp",
51
+ hpp: "cpp",
52
+ swift: "swift",
53
+ kt: "kotlin",
54
+ kts: "kotlin",
55
+ rb: "ruby",
56
+ php: "php",
57
+ vue: "vue",
58
+ svelte: "svelte",
59
+ toml: "toml",
60
+ xml: "xml",
61
+ graphql: "graphql",
62
+ gql: "graphql",
63
+ dockerfile: "docker",
64
+ lua: "lua",
65
+ zig: "zig"
66
+ };
67
+ function getFileIconSvg(filename) {
68
+ return getMaterialIconSvg(EXT_TO_ICON[filename.split(".").pop()?.toLowerCase() ?? ""] ?? "file");
69
+ }
70
+ var FileIcon = ({ filename, size = 16, className, fallback = "F" }) => {
71
+ const html = useMemo(() => getFileIconSvg(filename), [filename]);
72
+ const wrapperStyle = {
73
+ width: size,
74
+ height: size,
75
+ display: "inline-flex",
76
+ alignItems: "center",
77
+ justifyContent: "center",
78
+ opacity: html ? 1 : .5,
79
+ fontSize: size * .6
80
+ };
81
+ if (!html) return /* @__PURE__ */ jsx("span", {
82
+ className,
83
+ style: wrapperStyle,
84
+ children: fallback
85
+ });
86
+ return /* @__PURE__ */ jsx("span", {
87
+ className,
88
+ dangerouslySetInnerHTML: { __html: html },
89
+ style: {
90
+ width: size,
91
+ height: size
92
+ }
93
+ });
94
+ };
95
+ //#endregion
96
+ //#region src/styles.css.ts
97
+ var semanticClassNames = {
98
+ card: "rr-code-card",
99
+ lang: "rr-code-lang",
100
+ langIcon: "rr-code-lang-icon",
101
+ copyButton: "rr-code-copy",
102
+ bodyBackground: "rr-code-bg",
103
+ scroll: "rr-code-scroll",
104
+ scrollCollapsed: "rr-code-scroll-collapsed",
105
+ body: "rr-code-body",
106
+ bodyReadonly: "rr-code-readonly",
107
+ expandWrap: "rr-code-expand-wrap",
108
+ expandButton: "rr-code-expand",
109
+ lined: "rr-code-lined",
110
+ linedWithNumbers: "rr-code-lined-ln"
111
+ };
112
+ var card = "_1pn9r4q0";
113
+ var langInput = "_1pn9r4q1";
114
+ var lang = "_1pn9r4q2";
115
+ var langIcon = "_1pn9r4q3";
116
+ var copyButton = "_1pn9r4q4";
117
+ var bodyBackground = "_1pn9r4q5";
118
+ var scroll = "_1pn9r4q6";
119
+ var scrollCollapsed = "_1pn9r4q7";
120
+ var body = "_1pn9r4q8";
121
+ var bodyReadonly = "_1pn9r4q9";
122
+ var expandWrap = "_1pn9r4qa";
123
+ var expandButton = "_1pn9r4qb";
124
+ var lined = "_1pn9r4qc";
125
+ var linedWithNumbers = "_1pn9r4qd";
126
+ //#endregion
127
+ //#region src/icons/language.tsx
128
+ /** Maps normalized language to material-icon-theme icon name. */
129
+ var LANG_TO_ICON = {
130
+ "javascript": "javascript",
131
+ "js": "javascript",
132
+ "typescript": "typescript",
133
+ "ts": "typescript",
134
+ "angular-ts": "angular",
135
+ "angular-html": "angular",
136
+ "jsx": "react",
137
+ "tsx": "react",
138
+ "html": "html",
139
+ "html-derivative": "html",
140
+ "css": "css",
141
+ "scss": "sass",
142
+ "sass": "sass",
143
+ "less": "sass",
144
+ "postcss": "postcss",
145
+ "stylus": "stylus",
146
+ "json": "json",
147
+ "json5": "json",
148
+ "jsonc": "json",
149
+ "jsonl": "json",
150
+ "markdown": "markdown",
151
+ "md": "markdown",
152
+ "mdx": "markdown",
153
+ "mdc": "markdown",
154
+ "bash": "console",
155
+ "sh": "console",
156
+ "shell": "console",
157
+ "shellscript": "console",
158
+ "zsh": "console",
159
+ "python": "python",
160
+ "py": "python",
161
+ "rust": "rust",
162
+ "go": "go",
163
+ "java": "java",
164
+ "c": "c",
165
+ "cpp": "cpp",
166
+ "c++": "cpp",
167
+ "swift": "swift",
168
+ "kotlin": "kotlin",
169
+ "kt": "kotlin",
170
+ "yaml": "yaml",
171
+ "yml": "yaml",
172
+ "sql": "database",
173
+ "xml": "xml",
174
+ "vue": "vue",
175
+ "vue-html": "vue",
176
+ "vue-vine": "vue",
177
+ "svelte": "svelte",
178
+ "astro": "astro",
179
+ "php": "php",
180
+ "rb": "ruby",
181
+ "r": "r",
182
+ "julia": "julia",
183
+ "lua": "lua",
184
+ "zig": "zig",
185
+ "toml": "toml",
186
+ "graphql": "graphql",
187
+ "gql": "graphql",
188
+ "dockerfile": "docker",
189
+ "coffee": "coffee",
190
+ "pug": "pug",
191
+ "haml": "haml",
192
+ "handlebars": "handlebars",
193
+ "marko": "markojs",
194
+ "imba": "imba",
195
+ "hurl": "hurl",
196
+ "http": "http"
197
+ };
198
+ function getLanguageIconSvg(lang) {
199
+ return getMaterialIconSvg(LANG_TO_ICON[lang] ?? "code");
200
+ }
201
+ /** Returns whether a Material icon is available for the given language. */
202
+ function hasLanguageIcon(lang) {
203
+ return getLanguageIconSvg(lang) !== null;
204
+ }
205
+ var LanguageIcon = ({ language, size = 14, className = `${langIcon} ${semanticClassNames.langIcon}` }) => {
206
+ const html = useMemo(() => getLanguageIconSvg(language), [language]);
207
+ if (!html) return /* @__PURE__ */ jsx("span", {
208
+ className,
209
+ style: {
210
+ width: size,
211
+ height: size,
212
+ display: "inline-flex",
213
+ alignItems: "center",
214
+ justifyContent: "center",
215
+ opacity: .6,
216
+ fontSize: size * .6
217
+ },
218
+ children: "•"
219
+ });
220
+ return /* @__PURE__ */ jsx("span", {
221
+ className,
222
+ dangerouslySetInnerHTML: { __html: html },
223
+ style: {
224
+ width: size,
225
+ height: size
226
+ }
227
+ });
228
+ };
229
+ //#endregion
230
+ export { semanticClassNames as _, bodyBackground as a, copyButton as c, lang as d, langInput as f, scrollCollapsed as g, scroll as h, body as i, expandButton as l, linedWithNumbers as m, LanguageIcon as n, bodyReadonly as o, lined as p, hasLanguageIcon as r, card as s, LANG_TO_ICON as t, expandWrap as u, FileIcon as v, getMaterialIconSvg as y };
package/dist/icons.mjs CHANGED
@@ -1,91 +1,2 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import { useMemo } from "react";
3
- import { g as getMaterialIconSvg } from "./language-UrxzhGSS.js";
4
- import { L, a, h } from "./language-UrxzhGSS.js";
5
- const EXT_TO_ICON = {
6
- ts: "typescript",
7
- tsx: "react-ts",
8
- // TSX → React icon (same as JSX)
9
- mts: "typescript",
10
- cts: "typescript",
11
- js: "javascript",
12
- jsx: "react",
13
- mjs: "javascript",
14
- cjs: "javascript",
15
- py: "python",
16
- rs: "rust",
17
- go: "go",
18
- java: "java",
19
- html: "html",
20
- htm: "html",
21
- css: "css",
22
- scss: "sass",
23
- sass: "sass",
24
- less: "sass",
25
- json: "json",
26
- jsonc: "json",
27
- json5: "json",
28
- md: "markdown",
29
- mdx: "markdown",
30
- sh: "console",
31
- bash: "console",
32
- zsh: "console",
33
- yml: "yaml",
34
- yaml: "yaml",
35
- sql: "database",
36
- c: "c",
37
- h: "c",
38
- cpp: "cpp",
39
- cc: "cpp",
40
- cxx: "cpp",
41
- hpp: "cpp",
42
- swift: "swift",
43
- kt: "kotlin",
44
- kts: "kotlin",
45
- rb: "ruby",
46
- php: "php",
47
- vue: "vue",
48
- svelte: "svelte",
49
- toml: "toml",
50
- xml: "xml",
51
- graphql: "graphql",
52
- gql: "graphql",
53
- dockerfile: "docker",
54
- lua: "lua",
55
- zig: "zig"
56
- };
57
- function getFileIconSvg(filename) {
58
- const ext = filename.split(".").pop()?.toLowerCase() ?? "";
59
- const iconName = EXT_TO_ICON[ext] ?? "file";
60
- return getMaterialIconSvg(iconName);
61
- }
62
- const FileIcon = ({ filename, size = 16, className, fallback = "F" }) => {
63
- const html = useMemo(() => getFileIconSvg(filename), [filename]);
64
- const wrapperStyle = {
65
- width: size,
66
- height: size,
67
- display: "inline-flex",
68
- alignItems: "center",
69
- justifyContent: "center",
70
- opacity: html ? 1 : 0.5,
71
- fontSize: size * 0.6
72
- };
73
- if (!html) {
74
- return /* @__PURE__ */ jsx("span", { className, style: wrapperStyle, children: fallback });
75
- }
76
- return /* @__PURE__ */ jsx(
77
- "span",
78
- {
79
- className,
80
- dangerouslySetInnerHTML: { __html: html },
81
- style: { width: size, height: size }
82
- }
83
- );
84
- };
85
- export {
86
- FileIcon,
87
- L as LANG_TO_ICON,
88
- a as LanguageIcon,
89
- getMaterialIconSvg,
90
- h as hasLanguageIcon
91
- };
1
+ import { n as LanguageIcon, r as hasLanguageIcon, t as LANG_TO_ICON, v as FileIcon, y as getMaterialIconSvg } from "./icons-CnFW5y6g.js";
2
+ export { FileIcon, LANG_TO_ICON, LanguageIcon, getMaterialIconSvg, hasLanguageIcon };