@hanzo/ui 4.6.0 → 4.7.0

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.
Files changed (290) hide show
  1. package/dist/index.d.mts +16 -0
  2. package/dist/index.d.ts +16 -0
  3. package/dist/index.js +9458 -0
  4. package/dist/index.mjs +9449 -0
  5. package/dist/lib/utils.d.mts +2 -0
  6. package/dist/lib/utils.d.ts +2 -0
  7. package/dist/lib/utils.js +47 -0
  8. package/dist/lib/utils.mjs +28 -0
  9. package/dist/src/utils.d.mts +7 -0
  10. package/dist/src/utils.d.ts +7 -0
  11. package/dist/src/utils.js +47 -0
  12. package/dist/src/utils.mjs +28 -0
  13. package/dist/tailwind/index.d.mts +2 -0
  14. package/dist/tailwind/index.d.ts +2 -0
  15. package/dist/tailwind/index.js +2048 -0
  16. package/dist/tailwind/index.mjs +2017 -0
  17. package/dist/types/index.d.mts +12 -0
  18. package/dist/types/index.d.ts +12 -0
  19. package/dist/types/index.js +79 -0
  20. package/dist/types/index.mjs +56 -0
  21. package/package.json +151 -25
  22. package/MCP-INSTRUCTIONS.md +0 -73
  23. package/README-MCP.md +0 -175
  24. package/assets/ai-icons.tsx +0 -207
  25. package/assets/crypto.tsx +0 -33
  26. package/assets/file-type-icon.tsx +0 -66
  27. package/assets/file.tsx +0 -45
  28. package/assets/general.tsx +0 -2318
  29. package/assets/hanzo-logo.svg +0 -9
  30. package/assets/hanzo-logo.tsx +0 -15
  31. package/assets/index.ts +0 -8
  32. package/assets/index.tsx +0 -4
  33. package/assets/llm-provider.tsx +0 -1094
  34. package/blocks/components/accordian-block.tsx +0 -48
  35. package/blocks/components/block-component-props.ts +0 -11
  36. package/blocks/components/bullet-cards-block.tsx +0 -46
  37. package/blocks/components/card-block/index.tsx +0 -171
  38. package/blocks/components/card-block/link-out-button.tsx +0 -20
  39. package/blocks/components/card-block/util.ts +0 -28
  40. package/blocks/components/carte-blanche-block/index.tsx +0 -127
  41. package/blocks/components/carte-blanche-block/variant-content-left.tsx +0 -49
  42. package/blocks/components/content.tsx +0 -70
  43. package/blocks/components/cta-block.tsx +0 -115
  44. package/blocks/components/enh-heading-block.tsx +0 -204
  45. package/blocks/components/grid-block/grid-block-mutator.ts +0 -12
  46. package/blocks/components/grid-block/index.tsx +0 -83
  47. package/blocks/components/grid-block/mutator-registry.ts +0 -10
  48. package/blocks/components/grid-block/table-borders.mutator.ts +0 -47
  49. package/blocks/components/group-block.tsx +0 -83
  50. package/blocks/components/heading-block.tsx +0 -88
  51. package/blocks/components/image-block.tsx +0 -111
  52. package/blocks/components/index.ts +0 -30
  53. package/blocks/components/screenful-block/content.tsx +0 -123
  54. package/blocks/components/screenful-block/index.tsx +0 -107
  55. package/blocks/components/screenful-block/poster-background.tsx +0 -34
  56. package/blocks/components/screenful-block/video-background.tsx +0 -45
  57. package/blocks/components/space-block.tsx +0 -66
  58. package/blocks/components/video-block.tsx +0 -138
  59. package/blocks/def/accordian-block.ts +0 -14
  60. package/blocks/def/block.ts +0 -7
  61. package/blocks/def/bullet-cards-block.ts +0 -22
  62. package/blocks/def/card-block.ts +0 -22
  63. package/blocks/def/carte-blanche-block.ts +0 -21
  64. package/blocks/def/cta-block.ts +0 -19
  65. package/blocks/def/element-block.ts +0 -11
  66. package/blocks/def/enh-heading-block.ts +0 -44
  67. package/blocks/def/grid-block.ts +0 -16
  68. package/blocks/def/group-block.ts +0 -11
  69. package/blocks/def/heading-block.ts +0 -15
  70. package/blocks/def/image-block.ts +0 -31
  71. package/blocks/def/index.ts +0 -35
  72. package/blocks/def/screenful-block.ts +0 -54
  73. package/blocks/def/space-block.ts +0 -64
  74. package/blocks/def/video-block.ts +0 -9
  75. package/blocks/index.ts +0 -2
  76. package/components/index.ts +0 -56
  77. package/dist/button.d.ts +0 -1
  78. package/dist/button.js +0 -1
  79. package/dist/hooks/index.d.ts +0 -7
  80. package/dist/hooks/index.js +0 -7
  81. package/dist/hooks/use-click-away.d.ts +0 -2
  82. package/dist/hooks/use-click-away.js +0 -23
  83. package/dist/hooks/use-combined-refs.d.ts +0 -3
  84. package/dist/hooks/use-combined-refs.js +0 -18
  85. package/dist/hooks/use-copy-clipboard.d.ts +0 -9
  86. package/dist/hooks/use-copy-clipboard.js +0 -21
  87. package/dist/hooks/use-debounce.d.ts +0 -1
  88. package/dist/hooks/use-debounce.js +0 -13
  89. package/dist/hooks/use-fill-ids.d.ts +0 -8
  90. package/dist/hooks/use-fill-ids.js +0 -20
  91. package/dist/hooks/use-map.d.ts +0 -1
  92. package/dist/hooks/use-map.js +0 -20
  93. package/dist/hooks/use-measure.d.ts +0 -8
  94. package/dist/hooks/use-measure.js +0 -25
  95. package/dist/hooks/use-reverse-video-playback.d.ts +0 -1
  96. package/dist/hooks/use-reverse-video-playback.js +0 -41
  97. package/dist/hooks/use-scroll-restoration.d.ts +0 -8
  98. package/dist/hooks/use-scroll-restoration.js +0 -36
  99. package/dist/mcp/enhanced-server.d.ts +0 -29
  100. package/dist/mcp/enhanced-server.js +0 -1128
  101. package/dist/mcp/index.d.ts +0 -28
  102. package/dist/mcp/index.js +0 -436
  103. package/dist/registry/api.d.ts +0 -37
  104. package/dist/registry/api.js +0 -129
  105. package/dist/registry/index.d.ts +0 -353
  106. package/dist/registry/index.js +0 -45
  107. package/dist/utils.d.ts +0 -1
  108. package/dist/utils.js +0 -1
  109. package/environment.d.ts +0 -6
  110. package/helpers/file.ts +0 -33
  111. package/helpers/memoization.ts +0 -40
  112. package/primitives/accordion.tsx +0 -74
  113. package/primitives/action-button.tsx +0 -42
  114. package/primitives/alert-dialog.tsx +0 -185
  115. package/primitives/alert.tsx +0 -74
  116. package/primitives/apply-typography.tsx +0 -55
  117. package/primitives/aspect-ratio.tsx +0 -5
  118. package/primitives/avatar.tsx +0 -57
  119. package/primitives/background-beams.tsx +0 -142
  120. package/primitives/badge.tsx +0 -44
  121. package/primitives/breadcrumb.tsx +0 -130
  122. package/primitives/breakpoint-indicator.tsx +0 -19
  123. package/primitives/button.tsx +0 -82
  124. package/primitives/calendar.tsx +0 -72
  125. package/primitives/card.tsx +0 -97
  126. package/primitives/carousel.tsx +0 -237
  127. package/primitives/chat/chat-input-area.tsx +0 -87
  128. package/primitives/chat/chat-input.tsx +0 -71
  129. package/primitives/chat/files-preview.tsx +0 -330
  130. package/primitives/chat/index.ts +0 -6
  131. package/primitives/chat/json-form.tsx +0 -8
  132. package/primitives/chat/message-list.tsx +0 -307
  133. package/primitives/chat/message.tsx +0 -569
  134. package/primitives/chat/sqlite-preview.tsx +0 -215
  135. package/primitives/checkbox.tsx +0 -31
  136. package/primitives/collapsible.tsx +0 -9
  137. package/primitives/combobox.tsx +0 -239
  138. package/primitives/command.tsx +0 -149
  139. package/primitives/context-menu.tsx +0 -206
  140. package/primitives/copy-to-clipboard-icon.tsx +0 -60
  141. package/primitives/dialog-video-controller.tsx +0 -38
  142. package/primitives/dialog.tsx +0 -123
  143. package/primitives/dot-pattern.tsx +0 -57
  144. package/primitives/dots-loader.tsx +0 -13
  145. package/primitives/drawer.tsx +0 -110
  146. package/primitives/dropdown-menu.tsx +0 -199
  147. package/primitives/error-message.tsx +0 -19
  148. package/primitives/file-uploader.tsx +0 -200
  149. package/primitives/form.tsx +0 -183
  150. package/primitives/hover-card.tsx +0 -28
  151. package/primitives/icons/github.tsx +0 -14
  152. package/primitives/icons/index.ts +0 -18
  153. package/primitives/icons/youtube-logo.tsx +0 -59
  154. package/primitives/index-common.ts +0 -303
  155. package/primitives/index-next.ts +0 -4
  156. package/primitives/input-otp.tsx +0 -65
  157. package/primitives/input.tsx +0 -125
  158. package/primitives/label.tsx +0 -20
  159. package/primitives/list-adaptor.ts +0 -12
  160. package/primitives/list-box.tsx +0 -74
  161. package/primitives/loading-spinner.tsx +0 -33
  162. package/primitives/markdown-preview.tsx +0 -609
  163. package/primitives/mermaid.tsx +0 -196
  164. package/primitives/navigation-menu.tsx +0 -147
  165. package/primitives/next/image.tsx +0 -90
  166. package/primitives/next/index.ts +0 -7
  167. package/primitives/next/inline-icon.tsx +0 -36
  168. package/primitives/next/link-element.tsx +0 -109
  169. package/primitives/next/mdx-link.tsx +0 -22
  170. package/primitives/next/media-stack.tsx +0 -69
  171. package/primitives/next/nav-items.tsx +0 -45
  172. package/primitives/next/youtube-embed.tsx +0 -83
  173. package/primitives/pagination.tsx +0 -117
  174. package/primitives/popover.tsx +0 -32
  175. package/primitives/pretty-json-print.tsx +0 -28
  176. package/primitives/progress.tsx +0 -26
  177. package/primitives/prompt-textarea.tsx +0 -72
  178. package/primitives/qr-code.tsx +0 -112
  179. package/primitives/radio-group.tsx +0 -42
  180. package/primitives/resizable.tsx +0 -47
  181. package/primitives/scroll-area.tsx +0 -57
  182. package/primitives/search-input.tsx +0 -66
  183. package/primitives/select.tsx +0 -122
  184. package/primitives/separator.tsx +0 -25
  185. package/primitives/sheet.tsx +0 -139
  186. package/primitives/skeleton.tsx +0 -17
  187. package/primitives/slider.tsx +0 -62
  188. package/primitives/sonner.tsx +0 -35
  189. package/primitives/step-indicator.tsx +0 -69
  190. package/primitives/stepper.tsx +0 -272
  191. package/primitives/switch.tsx +0 -26
  192. package/primitives/table.tsx +0 -105
  193. package/primitives/tabs.tsx +0 -50
  194. package/primitives/text-area.tsx +0 -26
  195. package/primitives/text-link.tsx +0 -25
  196. package/primitives/textarea.tsx +0 -61
  197. package/primitives/textfield.tsx +0 -75
  198. package/primitives/toast.tsx +0 -30
  199. package/primitives/toggle-group.tsx +0 -63
  200. package/primitives/toggle.tsx +0 -44
  201. package/primitives/tooltip.tsx +0 -47
  202. package/primitives/video-player.tsx +0 -23
  203. package/public/r/accordion.json +0 -11
  204. package/public/r/alert.json +0 -11
  205. package/public/r/avatar.json +0 -11
  206. package/public/r/badge.json +0 -11
  207. package/public/r/button.json +0 -11
  208. package/public/r/card.json +0 -11
  209. package/public/r/checkbox.json +0 -11
  210. package/public/r/default.json +0 -6
  211. package/public/r/dialog.json +0 -11
  212. package/public/r/input.json +0 -11
  213. package/public/r/label.json +0 -11
  214. package/public/r/new-york.json +0 -6
  215. package/public/r/popover.json +0 -11
  216. package/public/r/select.json +0 -11
  217. package/public/r/table.json +0 -11
  218. package/public/r/tabs.json +0 -11
  219. package/public/r/toast.json +0 -11
  220. package/registry.json +0 -184
  221. package/src/button.ts +0 -1
  222. package/src/hooks/index.ts +0 -7
  223. package/src/hooks/use-click-away.ts +0 -31
  224. package/src/hooks/use-combined-refs.ts +0 -22
  225. package/src/hooks/use-copy-clipboard.ts +0 -30
  226. package/src/hooks/use-debounce.ts +0 -17
  227. package/src/hooks/use-fill-ids.ts +0 -25
  228. package/src/hooks/use-map.ts +0 -26
  229. package/src/hooks/use-measure.ts +0 -42
  230. package/src/hooks/use-reverse-video-playback.ts +0 -43
  231. package/src/hooks/use-scroll-restoration.ts +0 -50
  232. package/src/mcp/README.md +0 -141
  233. package/src/mcp/enhanced-server.ts +0 -1208
  234. package/src/mcp/index.ts +0 -518
  235. package/src/mcp/package.json +0 -10
  236. package/src/registry/api.ts +0 -164
  237. package/src/registry/index.ts +0 -60
  238. package/src/registry/package.json +0 -10
  239. package/src/utils.ts +0 -1
  240. package/tailwind/colors.tailwind.js +0 -53
  241. package/tailwind/fontFamily.tailwind.ts +0 -7
  242. package/tailwind/fontSize.tailwind.ts +0 -13
  243. package/tailwind/index.ts +0 -7
  244. package/tailwind/safelist.tailwind.js +0 -26
  245. package/tailwind/screens.tailwind.js +0 -8
  246. package/tailwind/spacing.tailwind.js +0 -65
  247. package/tailwind/tailwind.config.hanzo-preset.d.ts +0 -5
  248. package/tailwind/tailwind.config.hanzo-preset.js +0 -915
  249. package/tailwind/tw-font-desc.ts +0 -15
  250. package/tailwind/typo-plugin/get-plugin-styles.js +0 -679
  251. package/tailwind/typo-plugin/index.d.ts +0 -9
  252. package/tailwind/typo-plugin/index.js +0 -141
  253. package/tailwind/typo-plugin/utils.js +0 -60
  254. package/tailwind/typography-test.mdx +0 -35
  255. package/tailwind/z-index.tailwind.js +0 -71
  256. package/test/test-registry.js +0 -73
  257. package/test-imports.mjs +0 -19
  258. package/tsconfig.json +0 -22
  259. package/types/animation-def.ts +0 -3
  260. package/types/breakpoints.ts +0 -11
  261. package/types/bullet-item.ts +0 -10
  262. package/types/button-def.ts +0 -39
  263. package/types/dimensions.ts +0 -8
  264. package/types/grid-def.ts +0 -56
  265. package/types/image-def.ts +0 -32
  266. package/types/index.ts +0 -29
  267. package/types/link-def.ts +0 -56
  268. package/types/media-stack-def.ts +0 -31
  269. package/types/t-shirt-size.ts +0 -5
  270. package/types/tshirt-dimensions.ts +0 -20
  271. package/types/video-def.ts +0 -25
  272. package/util/blob.ts +0 -28
  273. package/util/copy-to-clipboard.ts +0 -17
  274. package/util/create-shadow-root.ts +0 -22
  275. package/util/date.ts +0 -83
  276. package/util/debounce.ts +0 -11
  277. package/util/file.ts +0 -15
  278. package/util/format-and-abbreviate-as-currency.ts +0 -125
  279. package/util/format-text.ts +0 -33
  280. package/util/format-to-max-char.ts +0 -68
  281. package/util/index-client.ts +0 -3
  282. package/util/index.ts +0 -9
  283. package/util/number-abbreviate.ts +0 -49
  284. package/util/specifier.ts +0 -43
  285. package/util/spread-to-transform.ts +0 -24
  286. package/util/step-animation.ts +0 -90
  287. package/util/timing.ts +0 -3
  288. package/util/toasts.tsx +0 -17
  289. package/util/two-way-map.ts +0 -19
  290. package/utils.ts +0 -9
@@ -1,609 +0,0 @@
1
- import { Primitive } from '@radix-ui/react-primitive';
2
- import { useCallbackRef } from '@radix-ui/react-use-callback-ref';
3
- import { Element } from 'hast';
4
-
5
- import React, {
6
- type ComponentPropsWithoutRef,
7
- type ComponentType,
8
- createContext,
9
- type ElementType,
10
- type FC,
11
- memo,
12
- type ReactNode,
13
- useContext,
14
- useMemo,
15
- } from 'react';
16
- import ReactMarkdown, { Options } from 'react-markdown';
17
- import {
18
- PrismAsyncLight,
19
- type SyntaxHighlighterProps as SHP,
20
- } from 'react-syntax-highlighter';
21
- import python from 'react-syntax-highlighter/dist/esm/languages/prism/python';
22
- import tsx from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
23
- import { default as github } from 'react-syntax-highlighter/dist/esm/styles/hljs/github';
24
- import rehypeKatex from 'rehype-katex';
25
- import remarkGfm from 'remark-gfm';
26
- import remarkMath from 'remark-math';
27
-
28
- import {
29
- memoCompareNodes,
30
- memoizeMarkdownComponents,
31
- } from '../helpers/memoization';
32
- import { cn } from '../src/utils';
33
- import { CopyToClipboardIcon } from './copy-to-clipboard-icon';
34
-
35
- import { MermaidDiagram } from './mermaid';
36
-
37
- export type CodeHeaderProps = {
38
- node?: Element | undefined;
39
- language: string | undefined;
40
- code: string;
41
- };
42
-
43
- export const CodeHeader: FC<CodeHeaderProps> = ({ language, code }) => {
44
- return (
45
- <div className="code-header-root bg-bg-darker text-text-default flex items-center justify-between gap-4 rounded-t-lg px-4 py-1">
46
- <span className="code-header-language text-text-secondary text-xs font-medium lowercase">
47
- {language}
48
- </span>
49
- <CopyToClipboardIcon
50
- className={cn(
51
- 'text-text-secondary h-7 w-7 bg-transparent hover:bg-gray-900 [&>svg]:h-3 [&>svg]:w-3',
52
- )}
53
- string={code}
54
- />
55
- </div>
56
- );
57
- };
58
-
59
- const makeMakeSyntaxHighlighter =
60
- (SyntaxHighlighter: ComponentType<SHP>) =>
61
- (config: Omit<SHP, 'language' | 'children'>) => {
62
- const PrismSyntaxHighlighter: FC<SyntaxHighlighterProps> = ({
63
- components: { Pre, Code },
64
- language,
65
- code,
66
- }) => {
67
- return (
68
- <SyntaxHighlighter
69
- CodeTag={Code as any}
70
- PreTag={Pre as any}
71
- {...config}
72
- language={language}
73
- >
74
- {code}
75
- </SyntaxHighlighter>
76
- );
77
- };
78
-
79
- PrismSyntaxHighlighter.displayName = 'PrismSyntaxHighlighter';
80
-
81
- return PrismSyntaxHighlighter;
82
- };
83
-
84
- // register languages you want to support
85
- PrismAsyncLight.registerLanguage('js', tsx);
86
- PrismAsyncLight.registerLanguage('jsx', tsx);
87
- PrismAsyncLight.registerLanguage('ts', tsx);
88
- PrismAsyncLight.registerLanguage('tsx', tsx);
89
- PrismAsyncLight.registerLanguage('python', python);
90
-
91
- export const makePrismAsyncLightSyntaxHighlighter =
92
- makeMakeSyntaxHighlighter(PrismAsyncLight);
93
-
94
- export const SyntaxHighlighterBase = makePrismAsyncLightSyntaxHighlighter({
95
- style: github,
96
- customStyle: {
97
- margin: 0,
98
- width: '100%',
99
- background: 'var(--color-bg-darker)',
100
- padding: '1.5rem 1rem',
101
- fontSize: '0.875em',
102
- },
103
- });
104
-
105
- export const defaultComponents = memoizeMarkdownComponents({
106
- h1: ({ className, ...props }) => (
107
- <h1
108
- className={cn(
109
- 'mt-[1em] mb-[0.7em] scroll-m-20 text-[1.375rem] font-bold last:mb-0',
110
- className,
111
- )}
112
- {...props}
113
- />
114
- ),
115
- h2: ({ className, ...props }) => (
116
- <h2
117
- className={cn(
118
- 'text-em-xl mt-[0.8em] mb-[0.4em] scroll-m-20 font-semibold first:mt-0 last:mb-0',
119
- className,
120
- )}
121
- {...props}
122
- />
123
- ),
124
- h3: ({ className, ...props }) => (
125
- <h3
126
- className={cn(
127
- 'text-em-lg mt-[1em] mb-[0.5em] scroll-m-20 font-semibold first:mt-0 last:mb-0',
128
- className,
129
- )}
130
- {...props}
131
- />
132
- ),
133
- h4: ({ className, ...props }) => (
134
- <h4
135
- className={cn(
136
- 'text-em-base mt-[1em] mb-[0.5em] scroll-m-20 font-semibold first:mt-0 last:mb-0',
137
- className,
138
- )}
139
- {...props}
140
- />
141
- ),
142
- h5: ({ className, ...props }) => (
143
- <h5
144
- className={cn(
145
- 'text-em-base mt-[1em] mb-[0.5em] font-semibold first:mt-0 last:mb-0',
146
- className,
147
- )}
148
- {...props}
149
- />
150
- ),
151
- h6: ({ className, ...props }) => (
152
- <h6
153
- className={cn(
154
- 'text-em-base mt-[1em] mb-[0.5em] font-semibold first:mt-0 last:mb-0',
155
- className,
156
- )}
157
- {...props}
158
- />
159
- ),
160
- p: ({ className, ...props }) => (
161
- <p
162
- className={cn('text-em-base my-[0.5em] first:mt-0 last:mb-0', className)}
163
- {...props}
164
- />
165
- ),
166
- a: ({ className, ...props }) => (
167
- <a
168
- className={cn(
169
- 'font-medium text-blue-400 underline underline-offset-4',
170
- className,
171
- )}
172
- target="_blank"
173
- {...props}
174
- />
175
- ),
176
- blockquote: ({ className, ...props }) => (
177
- <blockquote
178
- className={cn('border-l-2 pl-6 italic', className)}
179
- {...props}
180
- />
181
- ),
182
- ul: ({ className, ...props }) => (
183
- <ul
184
- className={cn(
185
- 'text-em-base my-[1em] ml-6 list-disc [&>li]:mt-2',
186
- className,
187
- )}
188
- {...props}
189
- />
190
- ),
191
- ol: ({ className, ...props }) => (
192
- <ol
193
- className={cn(
194
- 'text-em-base my-[1em] ml-6 list-decimal [&>li]:mt-[0.5em]',
195
- className,
196
- )}
197
- {...props}
198
- />
199
- ),
200
- hr: ({ className, ...props }) => (
201
- <hr
202
- className={cn('border-divider my-[2.25em] border-b', className)}
203
- {...props}
204
- />
205
- ),
206
- table: ({ className, ...props }) => (
207
- <div className="my-[1.5em] size-full overflow-x-auto">
208
- <table className="w-full border-collapse border-spacing-0" {...props} />
209
- </div>
210
- ),
211
- thead: ({ className, ...props }) => (
212
- <thead className={cn('[&>tr]:border-none', className)} {...props} />
213
- ),
214
- th: ({ className, ...props }) => (
215
- <th
216
- className={cn(
217
- 'text-em-sm px-[1em] py-[0.875em] text-left font-semibold first:rounded-tl-lg last:rounded-tr-lg [&[align=center]]:text-center [&[align=right]]:text-right',
218
- className,
219
- )}
220
- {...props}
221
- />
222
- ),
223
- td: ({ className, ...props }) => (
224
- <td
225
- className={cn(
226
- 'text-em-sm border-divider border-b px-[1em] py-[0.875em] text-left [&[align=center]]:text-center [&[align=right]]:text-right',
227
- className,
228
- )}
229
- {...props}
230
- />
231
- ),
232
- tr: ({ className, ...props }) => (
233
- <tr
234
- className={cn(
235
- 'm-0 border-b p-0 first:border-t [&:last-child>td:first-child]:rounded-bl-lg [&:last-child>td:last-child]:rounded-br-lg',
236
- className,
237
- )}
238
- {...props}
239
- />
240
- ),
241
- sup: ({ className, ...props }) => (
242
- <sup
243
- className={cn('[&>a]:text-em-xs [&>a]:no-underline', className)}
244
- {...props}
245
- />
246
- ),
247
- pre: ({ className, ...props }) => (
248
- <pre
249
- className={cn(
250
- 'bg-bg-darker text-text-default overflow-x-auto rounded-b-lg !px-[1em] !py-[0.5em]',
251
- className,
252
- )}
253
- {...props}
254
- />
255
- ),
256
- code: function Code({ className, ...props }) {
257
- const isCodeBlock = useIsMarkdownCodeBlock();
258
- return (
259
- <code
260
- className={cn(
261
- !isCodeBlock &&
262
- 'bg-bg-darker text-em-sm border-divider rounded-sm p-[0.25em] font-semibold',
263
- className,
264
- )}
265
- {...props}
266
- />
267
- );
268
- },
269
- CodeHeader,
270
- SyntaxHighlighter: SyntaxHighlighterBase,
271
- });
272
-
273
- export type PreComponent = ComponentType<
274
- ComponentPropsWithoutRef<'pre'> & { node?: Element | undefined }
275
- >;
276
- export type CodeComponent = ComponentType<
277
- ComponentPropsWithoutRef<'code'> & { node?: Element | undefined }
278
- >;
279
-
280
- export const PreContext = createContext<Omit<
281
- ComponentPropsWithoutRef<PreComponent>,
282
- 'children'
283
- > | null>(null);
284
-
285
- export const IsRunningContext = createContext<boolean | undefined>(undefined);
286
-
287
- export const useIsMarkdownCodeBlock = () => {
288
- return useContext(PreContext) !== null;
289
- };
290
-
291
- export const useIsRunning = () => {
292
- return useContext(IsRunningContext);
293
- };
294
-
295
- export const PreOverride: PreComponent = ({ children, ...rest }) => {
296
- return <PreContext.Provider value={rest}>{children}</PreContext.Provider>;
297
- };
298
-
299
- export const DefaultPre: PreComponent = ({
300
- className,
301
- ...rest
302
- }: ComponentPropsWithoutRef<'pre'>) => <pre className={className} {...rest} />;
303
-
304
- export const DefaultCode: CodeComponent = ({
305
- className,
306
- ...rest
307
- }: ComponentPropsWithoutRef<'code'>) => (
308
- <code className={className} {...rest} />
309
- );
310
-
311
- export const DefaultCodeBlockContent: ComponentType<{
312
- node: Element | undefined;
313
- components: { Pre: PreComponent; Code: CodeComponent };
314
- code: string | ReactNode | undefined;
315
- }> = ({ node, components: { Pre, Code }, code }) => (
316
- <Pre>
317
- <Code node={node}>{code}</Code>
318
- </Pre>
319
- );
320
-
321
- export const DefaultCodeHeader: ComponentType<CodeHeaderProps> = () => null;
322
-
323
- type PrimitiveDivProps = ComponentPropsWithoutRef<typeof Primitive.div>;
324
-
325
- export type MarkdownTextPrimitiveProps = Omit<
326
- Options,
327
- 'components' | 'children'
328
- > & {
329
- containerProps?: Omit<PrimitiveDivProps, 'children' | 'asChild'> | undefined;
330
- containerComponent?: ElementType;
331
- componentsByLanguage?:
332
- | Record<
333
- string,
334
- {
335
- CodeHeader?: ComponentType<CodeHeaderProps> | undefined;
336
- SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps> | undefined;
337
- }
338
- >
339
- | undefined;
340
- components?:
341
- | (NonNullable<Options['components']> & {
342
- SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps> | undefined;
343
- CodeHeader?: ComponentType<CodeHeaderProps> | undefined;
344
- })
345
- | undefined;
346
- content: string;
347
- ref?: React.RefObject<HTMLDivElement>;
348
- };
349
-
350
- export type CodeOverrideProps = ComponentPropsWithoutRef<CodeComponent> & {
351
- components: {
352
- Pre: PreComponent;
353
- Code: CodeComponent;
354
- CodeHeader: ComponentType<CodeHeaderProps>;
355
- SyntaxHighlighter: ComponentType<SyntaxHighlighterProps>;
356
- };
357
- componentsByLanguage?:
358
- | Record<
359
- string,
360
- {
361
- CodeHeader?: ComponentType<CodeHeaderProps>;
362
- SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>;
363
- }
364
- >
365
- | undefined;
366
- };
367
-
368
- export type SyntaxHighlighterProps = {
369
- components: {
370
- Pre: PreComponent;
371
- Code: CodeComponent;
372
- };
373
- language: string;
374
- code: string;
375
- isRunning?: boolean;
376
- };
377
-
378
- export type CodeBlockProps = {
379
- node: Element | undefined;
380
- language: string;
381
- code: string;
382
- components: {
383
- Pre: PreComponent;
384
- Code: CodeComponent;
385
- CodeHeader: ComponentType<CodeHeaderProps>;
386
- SyntaxHighlighter: ComponentType<SyntaxHighlighterProps>;
387
- };
388
- isRunning?: boolean;
389
- };
390
-
391
- export const DefaultCodeBlock: FC<CodeBlockProps> = ({
392
- node,
393
- components: { Pre, Code, SyntaxHighlighter, CodeHeader },
394
- language,
395
- code,
396
- isRunning,
397
- }) => {
398
- const components = useMemo(() => ({ Pre, Code }), [Pre, Code]);
399
-
400
- const SH = !!language ? SyntaxHighlighter : DefaultCodeBlockContent;
401
-
402
- return (
403
- <>
404
- <CodeHeader node={node} language={language} code={code} />
405
- <SH
406
- node={node}
407
- components={components}
408
- language={language ?? 'unknown'}
409
- code={code}
410
- isRunning={isRunning}
411
- />
412
- </>
413
- );
414
- };
415
-
416
- export const withDefaultProps =
417
- <TProps extends { className?: string | undefined }>({
418
- className,
419
- ...defaultProps
420
- }: Partial<TProps>) =>
421
- ({ className: classNameProp, ...props }: TProps) => {
422
- return {
423
- className: cn(className, classNameProp),
424
- ...defaultProps,
425
- ...props,
426
- } as TProps;
427
- };
428
-
429
- const CodeBlockOverride: FC<CodeOverrideProps> = ({
430
- node,
431
- components: {
432
- Pre,
433
- Code,
434
- SyntaxHighlighter: FallbackSyntaxHighlighter,
435
- CodeHeader: FallbackCodeHeader,
436
- },
437
- componentsByLanguage = {},
438
- children,
439
- ...codeProps
440
- }) => {
441
- const preProps = useContext(PreContext)!;
442
- const isRunning = useIsRunning();
443
- const getPreProps = withDefaultProps<any>(preProps);
444
- const WrappedPre: PreComponent = useCallbackRef((props) => (
445
- <Pre {...getPreProps(props)} />
446
- ));
447
-
448
- const getCodeProps = withDefaultProps<any>(codeProps);
449
- const WrappedCode: CodeComponent = useCallbackRef((props) => (
450
- <Code {...getCodeProps(props)} />
451
- ));
452
-
453
- const language = /language-(\w+)/.exec(codeProps.className || '')?.[1] ?? '';
454
-
455
- // if the code content is not string (due to rehype plugins), return a default code block
456
- if (typeof children !== 'string') {
457
- return (
458
- <DefaultCodeBlockContent
459
- code={children}
460
- components={{ Pre: WrappedPre, Code: WrappedCode }}
461
- node={node}
462
- />
463
- );
464
- }
465
-
466
- const SyntaxHighlighter: ComponentType<SyntaxHighlighterProps> =
467
- componentsByLanguage[language]?.SyntaxHighlighter ??
468
- FallbackSyntaxHighlighter;
469
-
470
- const CodeHeader: ComponentType<CodeHeaderProps> =
471
- componentsByLanguage[language]?.CodeHeader ?? FallbackCodeHeader;
472
-
473
- return (
474
- <DefaultCodeBlock
475
- node={node}
476
- components={{
477
- Pre: WrappedPre,
478
- Code: WrappedCode,
479
- SyntaxHighlighter,
480
- CodeHeader,
481
- }}
482
- language={language || 'unknown'}
483
- code={children}
484
- isRunning={isRunning}
485
- />
486
- );
487
- };
488
-
489
- export const CodeOverrideBase: FC<CodeOverrideProps> = ({
490
- node,
491
- components,
492
- componentsByLanguage,
493
- ...props
494
- }) => {
495
- const isCodeBlock = useContext(PreContext) !== null;
496
- if (!isCodeBlock) return <components.Code {...props} />;
497
- return (
498
- <CodeBlockOverride
499
- components={components}
500
- componentsByLanguage={componentsByLanguage}
501
- node={node}
502
- {...props}
503
- />
504
- );
505
- };
506
-
507
- export const CodeOverride = memo(CodeOverrideBase, (prev, next) => {
508
- const isEqual =
509
- prev.components === next.components &&
510
- prev.componentsByLanguage === next.componentsByLanguage &&
511
- memoCompareNodes(prev, next);
512
- return isEqual;
513
- });
514
-
515
- export const MarkdownTextPrimitive = ({
516
- components: userComponents,
517
- className,
518
- containerProps,
519
- containerComponent: Container = 'div',
520
- content,
521
- componentsByLanguage,
522
- ref,
523
- isRunning,
524
- ...rest
525
- }: MarkdownTextPrimitiveProps & { isRunning?: boolean }) => {
526
- const {
527
- pre = DefaultPre,
528
- code = DefaultCode,
529
- SyntaxHighlighter = DefaultCodeBlockContent,
530
- CodeHeader = DefaultCodeHeader,
531
- } = userComponents ?? {};
532
-
533
- const useCodeOverrideComponents = useMemo(() => {
534
- return {
535
- Pre: pre,
536
- Code: code,
537
- SyntaxHighlighter,
538
- CodeHeader,
539
- };
540
- }, [pre, code, SyntaxHighlighter, CodeHeader]);
541
- const CodeComponent = useCallbackRef((props) => (
542
- <CodeOverride
543
- components={useCodeOverrideComponents}
544
- componentsByLanguage={componentsByLanguage}
545
- {...props}
546
- />
547
- ));
548
-
549
- const components: Options['components'] = useMemo(() => {
550
- const { pre, code, SyntaxHighlighter, CodeHeader, ...componentsRest } =
551
- userComponents ?? {};
552
- return {
553
- ...componentsRest,
554
- pre: PreOverride,
555
- code: CodeComponent,
556
- };
557
- }, [CodeComponent, userComponents]);
558
-
559
- return (
560
- <Container
561
- // data-status={status.type}
562
- {...containerProps}
563
- className={cn(className, containerProps?.className)}
564
- ref={ref}
565
- >
566
- <IsRunningContext.Provider value={isRunning}>
567
- <ReactMarkdown components={components} {...rest}>
568
- {content}
569
- </ReactMarkdown>
570
- </IsRunningContext.Provider>
571
- </Container>
572
- );
573
- };
574
-
575
- MarkdownTextPrimitive.displayName = 'MarkdownTextPrimitive';
576
-
577
- export type MakeMarkdownTextProps = MarkdownTextPrimitiveProps & {
578
- isRunning?: boolean;
579
- };
580
-
581
- export const MarkdownTextBase = ({
582
- className,
583
- isRunning,
584
- components: userComponents,
585
- ...rest
586
- }: MakeMarkdownTextProps) => {
587
- return (
588
- <MarkdownTextPrimitive
589
- components={defaultComponents}
590
- remarkPlugins={[remarkGfm, remarkMath]}
591
- rehypePlugins={[rehypeKatex]}
592
- className={cn(isRunning && 'md-running', className)}
593
- isRunning={isRunning}
594
- componentsByLanguage={{
595
- mermaid: {
596
- SyntaxHighlighter: MermaidDiagram,
597
- },
598
- }}
599
- {...rest}
600
- />
601
- );
602
- };
603
- export const MarkdownText = memo(
604
- MarkdownTextBase,
605
- (prev, next) =>
606
- prev.content === next.content &&
607
- prev.isRunning === next.isRunning &&
608
- prev.className === next.className,
609
- );