@cxxi-lab/hubbyware-ui 0.5.1 → 0.6.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.
package/README.md CHANGED
@@ -4,3 +4,13 @@ Hubbyware's design system: its React components, design tokens (`theme.css`) and
4
4
  A hubby imports it directly; the apps provide one shared instance at run time.
5
5
 
6
6
  Formerly `@cxxi-lab/hub-ui`. Source: [cxxi-lab/hubbyware-ui](https://github.com/cxxi-lab/hubbyware-ui). MIT.
7
+
8
+ ## Markdown
9
+
10
+ `<Markdown>{text}</Markdown>` renders Markdown the same way everywhere (an AI's answers, the
11
+ back-office, the showcase, the docs): CommonMark and GitHub's extensions (tables, task lists,
12
+ strikethrough, autolinks), code blocks with a copy button, styled with the design tokens. Safe by
13
+ default — the text may come from a model or a user: raw HTML is shown as text, never rendered;
14
+ `javascript:` links are dropped and links open outside; images are shown as links unless `images`.
15
+ It re-renders on each chunk of a stream (an unclosed code block already shows as code).
16
+ `npm test` checks all this.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cxxi-lab/hubbyware-ui",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Hubbyware's design system: React components, design tokens and fonts (formerly @cxxi-lab/hub-ui).",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,7 +21,8 @@
21
21
  "README.md"
22
22
  ],
23
23
  "scripts": {
24
- "typecheck": "tsc --noEmit"
24
+ "typecheck": "tsc --noEmit",
25
+ "test": "esbuild test/markdown.test.tsx --bundle --platform=node --format=cjs --jsx=automatic --outfile=../../node_modules/.cache/hubbyware-ui-test/markdown.test.cjs --log-level=warning && node ../../node_modules/.cache/hubbyware-ui-test/markdown.test.cjs"
25
26
  },
26
27
  "repository": {
27
28
  "type": "git",
@@ -32,6 +33,8 @@
32
33
  "access": "public"
33
34
  },
34
35
  "dependencies": {
36
+ "react-markdown": "^10.1.0",
37
+ "remark-gfm": "^4.0.1",
35
38
  "tailwind-merge": "^3.6.0",
36
39
  "tailwind-variants": "^3.2.2"
37
40
  },
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export * from './modals'
9
9
  export * from './navs'
10
10
  export * from './tables'
11
11
  export * from './Brand'
12
+ export * from './markdown'
12
13
  export type { FormData, FormErrors, FormContextValue } from './contexts/FormContext'
13
14
  export { useFormContext } from './contexts/FormContext'
14
15
  export { useBackHandler, handleBack, onBackDepth } from './back'
@@ -0,0 +1,89 @@
1
+ import { useState, type ComponentPropsWithoutRef, type ReactNode } from 'react'
2
+ import ReactMarkdown, { type Components } from 'react-markdown'
3
+ import remarkGfm from 'remark-gfm'
4
+ import { twMerge } from 'tailwind-merge'
5
+
6
+ /**
7
+ * Markdown, as the design system renders it everywhere (an AI's answers, a hubby's notes, the
8
+ * back-office, the showcase, the docs). Safe by default — its text may come from a model or a user:
9
+ * no raw HTML is ever rendered (shown as text), links open outside (and `javascript:` ones are
10
+ * dropped), images are not loaded unless `images` allows them. CommonMark + GitHub's extensions
11
+ * (tables, task lists, strikethrough, autolinks); code blocks with a copy button. Re-rendered on each
12
+ * chunk of a stream without flicker (an unclosed code block already shows as code).
13
+ */
14
+ export interface MarkdownProps {
15
+ children: string
16
+ /** load the images it links to (default: shown as links) */
17
+ images?: boolean
18
+ className?: string
19
+ }
20
+
21
+ function CopyButton({ text }: { text: string }) {
22
+ const [copied, setCopied] = useState(false)
23
+ return (
24
+ <button
25
+ type="button"
26
+ onClick={() => void navigator.clipboard?.writeText(text).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1500) })}
27
+ className="absolute top-1.5 right-1.5 rounded px-1.5 py-0.5 text-[11px] text-steel-300 bg-steel-800/80 border border-steel-700 opacity-0 group-hover:opacity-100 focus:opacity-100 cursor-pointer"
28
+ >
29
+ {copied ? 'Copied' : 'Copy'}
30
+ </button>
31
+ )
32
+ }
33
+
34
+ /** The text of a rendered node (a code block's, for its copy button). */
35
+ function textOf(node: ReactNode): string {
36
+ if (typeof node === 'string' || typeof node === 'number') return String(node)
37
+ if (Array.isArray(node)) return node.map(textOf).join('')
38
+ if (node && typeof node === 'object' && 'props' in node) return textOf((node as { props: { children?: ReactNode } }).props.children)
39
+ return ''
40
+ }
41
+
42
+ function components(images: boolean): Components {
43
+ return {
44
+ h1: ({ node: _n, ...p }) => <h1 className="mt-4 mb-2 text-xl font-semibold text-white first:mt-0" {...p} />,
45
+ h2: ({ node: _n, ...p }) => <h2 className="mt-4 mb-2 text-lg font-semibold text-white first:mt-0" {...p} />,
46
+ h3: ({ node: _n, ...p }) => <h3 className="mt-3 mb-1.5 text-base font-semibold text-white first:mt-0" {...p} />,
47
+ h4: ({ node: _n, ...p }) => <h4 className="mt-3 mb-1 text-sm font-semibold text-white first:mt-0" {...p} />,
48
+ p: ({ node: _n, ...p }) => <p className="my-2 first:mt-0 last:mb-0" {...p} />,
49
+ a: ({ node: _n, ...p }) => <a className="text-primary-400 underline underline-offset-2 hover:text-primary-300" target="_blank" rel="noopener noreferrer" {...p} />,
50
+ ul: ({ node: _n, className, ...p }) => <ul className={twMerge('my-2 pl-5 list-disc marker:text-steel-400', className?.includes('contains-task-list') && 'pl-1 list-none')} {...p} />,
51
+ ol: ({ node: _n, ...p }) => <ol className="my-2 pl-5 list-decimal marker:text-steel-400" {...p} />,
52
+ li: ({ node: _n, ...p }) => <li className="my-0.5" {...p} />,
53
+ input: ({ node: _n, ...p }) => <input {...p} disabled className="mr-1.5 align-middle accent-primary-500" />,
54
+ blockquote: ({ node: _n, ...p }) => <blockquote className="my-2 border-l-2 border-steel-600 pl-3 text-steel-200" {...p} />,
55
+ hr: () => <hr className="my-4 border-steel-700" />,
56
+ del: ({ node: _n, ...p }) => <del className="text-steel-400" {...p} />,
57
+ pre: ({ node: _n, children }) => (
58
+ <div className="group relative my-2">
59
+ <pre className="overflow-x-auto rounded-md bg-steel-950 border border-steel-800 p-3 text-[13px] leading-relaxed">{children}</pre>
60
+ <CopyButton text={textOf(children).replace(/\n$/, '')} />
61
+ </div>
62
+ ),
63
+ code: ({ node: _n, className, ...p }: ComponentPropsWithoutRef<'code'> & { node?: unknown }) => (
64
+ // in a block (<pre>): its language class; inline: a chip
65
+ className?.startsWith('language-') || (p.children && String(p.children).includes('\n'))
66
+ ? <code className={twMerge('font-mono text-steel-100', className)} {...p} />
67
+ : <code className="rounded bg-steel-800 px-1 py-0.5 font-mono text-[0.9em] text-steel-100" {...p} />
68
+ ),
69
+ table: ({ node: _n, ...p }) => <div className="my-2 overflow-x-auto"><table className="border-collapse text-sm" {...p} /></div>,
70
+ th: ({ node: _n, ...p }) => <th className="border border-steel-700 bg-steel-800 px-2 py-1 text-left font-semibold text-white" {...p} />,
71
+ td: ({ node: _n, ...p }) => <td className="border border-steel-700 px-2 py-1" {...p} />,
72
+ img: ({ node: _n, src, alt }) => images && typeof src === 'string'
73
+ ? <img src={src} alt={alt ?? ''} className="my-2 max-w-full rounded" loading="lazy" />
74
+ : <a href={typeof src === 'string' ? src : undefined} target="_blank" rel="noopener noreferrer" className="text-primary-400 underline">{alt || 'image'}</a>
75
+ }
76
+ }
77
+
78
+ const PLAIN = components(false)
79
+ const WITH_IMAGES = components(true)
80
+
81
+ export function Markdown({ children, images = false, className }: MarkdownProps) {
82
+ return (
83
+ <div className={twMerge('text-sm leading-relaxed text-steel-100 break-words', className)}>
84
+ <ReactMarkdown remarkPlugins={[remarkGfm]} components={images ? WITH_IMAGES : PLAIN} skipHtml={false}>
85
+ {children}
86
+ </ReactMarkdown>
87
+ </div>
88
+ )
89
+ }
@@ -0,0 +1 @@
1
+ export { Markdown, type MarkdownProps } from './Markdown'