@swisper.ai/web-chat-ui 0.4.0 → 0.5.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/dist/components/chat-notification-card/chat-notification-card.d.ts +11 -3
- package/dist/components/chat-notification-card/chat-notification-card.d.ts.map +1 -1
- package/dist/components/chat-notification-card/chat-notification-card.stories.d.ts +4 -0
- package/dist/components/chat-notification-card/chat-notification-card.stories.d.ts.map +1 -1
- package/dist/components/html-viewer/html-viewer.d.ts +18 -0
- package/dist/components/html-viewer/html-viewer.d.ts.map +1 -0
- package/dist/components/html-viewer/html-viewer.stories.d.ts +14 -0
- package/dist/components/html-viewer/html-viewer.stories.d.ts.map +1 -0
- package/dist/components/html-viewer/index.d.ts +3 -0
- package/dist/components/html-viewer/index.d.ts.map +1 -0
- package/dist/components/html-viewer/types.d.ts +7 -0
- package/dist/components/html-viewer/types.d.ts.map +1 -0
- package/dist/components/html-viewer/utils.d.ts +8 -0
- package/dist/components/html-viewer/utils.d.ts.map +1 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/index.css +1 -1
- package/dist/index.es.js +1199 -1053
- package/dist/index.es.js.map +1 -1
- package/package.json +5 -3
- package/src/components/chat-notification-card/chat-notification-card.json +4 -1
- package/src/components/chat-notification-card/chat-notification-card.stories.tsx +82 -31
- package/src/components/chat-notification-card/chat-notification-card.tsx +20 -5
- package/src/components/chat-notification-popup/__stories__/notification-item.tsx +1 -1
- package/src/components/html-viewer/__tests__/html-viewer.spec.tsx +105 -0
- package/src/components/html-viewer/html-viewer.json +81 -0
- package/src/components/html-viewer/html-viewer.stories.tsx +118 -0
- package/src/components/html-viewer/html-viewer.tsx +134 -0
- package/src/components/html-viewer/index.ts +2 -0
- package/src/components/html-viewer/types.ts +8 -0
- package/src/components/html-viewer/utils.ts +58 -0
- package/src/components/index.ts +1 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { toJsxRuntime, type ExtraProps } from 'hast-util-to-jsx-runtime';
|
|
2
|
+
import { useMemo, type ComponentProps, type MouseEvent } from 'react';
|
|
3
|
+
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
|
|
4
|
+
import rehypeParse from 'rehype-parse';
|
|
5
|
+
import rehypeSanitize from 'rehype-sanitize';
|
|
6
|
+
import { unified } from 'unified';
|
|
7
|
+
|
|
8
|
+
import { Link } from '@swisper.ai/ui';
|
|
9
|
+
import { cn } from '@swisper.ai/web-core';
|
|
10
|
+
|
|
11
|
+
import type { HtmlViewerComponents, HtmlViewerSanitizeSchema } from './types';
|
|
12
|
+
import { mergeSanitizeSchema } from './utils';
|
|
13
|
+
|
|
14
|
+
export interface HtmlViewerProps {
|
|
15
|
+
/** HTML fragment to render. It is parsed and sanitised; it is never set as `innerHTML`. */
|
|
16
|
+
html: string;
|
|
17
|
+
/** Classes merged last onto the root element (`data-slot="html-viewer"`). */
|
|
18
|
+
className?: string;
|
|
19
|
+
/** Renderers keyed by tag name, merged over the defaults (`a`, `p`, `ul`, `ol`, `li`, `strong`, `b`, `em`, `i`, `code`). */
|
|
20
|
+
components?: HtmlViewerComponents;
|
|
21
|
+
/** Added to the base schema (`rehype-sanitize`'s default without `width`, `height` and `noWrap`, except on `img`), e.g. to allow an extra tag or attribute. */
|
|
22
|
+
sanitizeSchema?: HtmlViewerSanitizeSchema;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Renders an HTML fragment as React elements: parsed with rehype, sanitised, and mapped through
|
|
27
|
+
* `components`. The root keeps the content inside its container: long words wrap anywhere, and
|
|
28
|
+
* images and tables are capped at its width, whichever renderer draws them.
|
|
29
|
+
*/
|
|
30
|
+
export function HtmlViewer({ html, className, components, sanitizeSchema }: HtmlViewerProps) {
|
|
31
|
+
const schema = useMemo(() => mergeSanitizeSchema(sanitizeSchema), [sanitizeSchema]);
|
|
32
|
+
|
|
33
|
+
const processor = useMemo(
|
|
34
|
+
() => unified().use(rehypeParse, { fragment: true }).use(rehypeSanitize, schema),
|
|
35
|
+
[schema],
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const content = useMemo(() => {
|
|
39
|
+
const tree = processor.runSync(processor.parse(html));
|
|
40
|
+
|
|
41
|
+
return toJsxRuntime(tree, {
|
|
42
|
+
Fragment,
|
|
43
|
+
jsx,
|
|
44
|
+
jsxs,
|
|
45
|
+
components: { ...DEFAULT_COMPONENTS, ...components },
|
|
46
|
+
});
|
|
47
|
+
}, [processor, html, components]);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<div
|
|
51
|
+
data-slot="html-viewer"
|
|
52
|
+
data-testid="html-viewer"
|
|
53
|
+
className={cn(
|
|
54
|
+
'min-w-0 wrap-anywhere',
|
|
55
|
+
'[&_img]:h-auto [&_img]:max-w-full [&_table]:max-w-full',
|
|
56
|
+
className,
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
{content}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type ElementProps<TagName extends keyof JSX.IntrinsicElements> = ComponentProps<TagName> &
|
|
65
|
+
ExtraProps;
|
|
66
|
+
|
|
67
|
+
function stopPropagation(event: MouseEvent<HTMLAnchorElement>) {
|
|
68
|
+
event.stopPropagation();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Opens in a new tab; its clicks don't reach a clickable parent such as a notification card. */
|
|
72
|
+
function HtmlViewerLink({ href, title, className, children }: ElementProps<'a'>) {
|
|
73
|
+
return (
|
|
74
|
+
<Link
|
|
75
|
+
href={href}
|
|
76
|
+
title={title}
|
|
77
|
+
className={cn('inline-block!', className)}
|
|
78
|
+
isExternal
|
|
79
|
+
onMouseDown={stopPropagation}
|
|
80
|
+
onClick={stopPropagation}
|
|
81
|
+
>
|
|
82
|
+
{children}
|
|
83
|
+
</Link>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function HtmlViewerParagraph({ node: _node, className, ...rest }: ElementProps<'p'>) {
|
|
88
|
+
return <p className={cn('my-0 not-first:mt-8', className)} {...rest} />;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function HtmlViewerUnorderedList({ node: _node, className, ...rest }: ElementProps<'ul'>) {
|
|
92
|
+
return <ul className={cn('my-0 not-first:mt-8 pl-20 list-disc', className)} {...rest} />;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function HtmlViewerOrderedList({ node: _node, className, ...rest }: ElementProps<'ol'>) {
|
|
96
|
+
return <ol className={cn('my-0 not-first:mt-8 pl-20 list-decimal', className)} {...rest} />;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function HtmlViewerListItem({ node: _node, className, ...rest }: ElementProps<'li'>) {
|
|
100
|
+
return <li className={cn('my-0', className)} {...rest} />;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function HtmlViewerStrong({ node: _node, className, ...rest }: ElementProps<'strong'>) {
|
|
104
|
+
return <strong className={cn('font-semibold', className)} {...rest} />;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function HtmlViewerBold({ node: _node, className, ...rest }: ElementProps<'b'>) {
|
|
108
|
+
return <b className={cn('font-semibold', className)} {...rest} />;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function HtmlViewerEmphasis({ node: _node, className, ...rest }: ElementProps<'em'>) {
|
|
112
|
+
return <em className={cn('italic', className)} {...rest} />;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function HtmlViewerItalic({ node: _node, className, ...rest }: ElementProps<'i'>) {
|
|
116
|
+
return <i className={cn('italic', className)} {...rest} />;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function HtmlViewerCode({ node: _node, className, ...rest }: ElementProps<'code'>) {
|
|
120
|
+
return <code className={cn('font-mono text-[0.9em]', className)} {...rest} />;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const DEFAULT_COMPONENTS: HtmlViewerComponents = {
|
|
124
|
+
a: HtmlViewerLink,
|
|
125
|
+
p: HtmlViewerParagraph,
|
|
126
|
+
ul: HtmlViewerUnorderedList,
|
|
127
|
+
ol: HtmlViewerOrderedList,
|
|
128
|
+
li: HtmlViewerListItem,
|
|
129
|
+
strong: HtmlViewerStrong,
|
|
130
|
+
b: HtmlViewerBold,
|
|
131
|
+
em: HtmlViewerEmphasis,
|
|
132
|
+
i: HtmlViewerItalic,
|
|
133
|
+
code: HtmlViewerCode,
|
|
134
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Components } from 'hast-util-to-jsx-runtime';
|
|
2
|
+
import type { Options as SanitizeSchema } from 'rehype-sanitize';
|
|
3
|
+
|
|
4
|
+
/** Renderers keyed by HTML tag name. Each receives the element's props plus its hast `node`. */
|
|
5
|
+
export type HtmlViewerComponents = Partial<Components>;
|
|
6
|
+
|
|
7
|
+
/** A `rehype-sanitize` schema. Its lists are added to the base schema, not swapped for it. */
|
|
8
|
+
export type HtmlViewerSanitizeSchema = SanitizeSchema;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { defaultSchema } from 'rehype-sanitize';
|
|
2
|
+
|
|
3
|
+
import type { HtmlViewerSanitizeSchema } from './types';
|
|
4
|
+
|
|
5
|
+
/** Attributes that pin a size and push content past its container, e.g. `<table width="600">`. */
|
|
6
|
+
const FIXED_LAYOUT_ATTRIBUTES = new Set(['width', 'height', 'noWrap']);
|
|
7
|
+
|
|
8
|
+
const defaultAttributes = Object.fromEntries(
|
|
9
|
+
Object.entries(defaultSchema.attributes ?? {}).map(([tagName, values]) => [
|
|
10
|
+
tagName,
|
|
11
|
+
values.filter((value) => !FIXED_LAYOUT_ATTRIBUTES.has(Array.isArray(value) ? value[0] : value)),
|
|
12
|
+
]),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `rehype-sanitize`'s default schema without `width`, `height` and `noWrap`, which it allows on
|
|
17
|
+
* every element. `img` keeps `width` and `height` as its aspect ratio; the viewer caps images with
|
|
18
|
+
* `max-w-full h-auto`, so they never overflow.
|
|
19
|
+
*/
|
|
20
|
+
const BASE_SCHEMA: HtmlViewerSanitizeSchema = {
|
|
21
|
+
...defaultSchema,
|
|
22
|
+
attributes: {
|
|
23
|
+
...defaultAttributes,
|
|
24
|
+
img: [...(defaultAttributes.img ?? []), 'width', 'height'],
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Adds `extra` to the base schema (`rehype-sanitize`'s default without `width`, `height` and
|
|
30
|
+
* `noWrap`, except on `img`): tag names, per-tag attributes, protocols and required attributes are
|
|
31
|
+
* appended to the base lists; any other key replaces the base value.
|
|
32
|
+
*/
|
|
33
|
+
export function mergeSanitizeSchema(extra?: HtmlViewerSanitizeSchema): HtmlViewerSanitizeSchema {
|
|
34
|
+
if (!extra) {
|
|
35
|
+
return BASE_SCHEMA;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const attributes = { ...BASE_SCHEMA.attributes };
|
|
39
|
+
|
|
40
|
+
for (const [tagName, values] of Object.entries(extra.attributes ?? {})) {
|
|
41
|
+
attributes[tagName] = [...(attributes[tagName] ?? []), ...values];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const protocols = { ...BASE_SCHEMA.protocols };
|
|
45
|
+
|
|
46
|
+
for (const [attribute, values] of Object.entries(extra.protocols ?? {})) {
|
|
47
|
+
protocols[attribute] = [...(protocols[attribute] ?? []), ...(values ?? [])];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
...BASE_SCHEMA,
|
|
52
|
+
...extra,
|
|
53
|
+
tagNames: [...(BASE_SCHEMA.tagNames ?? []), ...(extra.tagNames ?? [])],
|
|
54
|
+
attributes,
|
|
55
|
+
protocols,
|
|
56
|
+
required: { ...BASE_SCHEMA.required, ...extra.required },
|
|
57
|
+
};
|
|
58
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ export * from './new-chat-trigger';
|
|
|
20
20
|
export * from './chat-reasoning';
|
|
21
21
|
export * from './chat-thinking-process';
|
|
22
22
|
export * from './deprecated-empty-state';
|
|
23
|
+
export * from './html-viewer';
|
|
23
24
|
export * from './markdown-viewer';
|
|
24
25
|
export * from './rich-text-area';
|
|
25
26
|
export * from './rich-text-field';
|