@assistant-ui/react-markdown 0.0.4 → 0.0.5
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/index.d.mts +22 -3
- package/dist/index.d.ts +22 -3
- package/dist/index.js +89 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
|
-
import { FC } from 'react';
|
|
1
|
+
import { ComponentType, FC } from 'react';
|
|
2
2
|
import { Options } from 'react-markdown';
|
|
3
3
|
|
|
4
|
-
type
|
|
4
|
+
type PreComponent = NonNullable<NonNullable<Options["components"]>["pre"]>;
|
|
5
|
+
type CodeComponent = NonNullable<NonNullable<Options["components"]>["code"]>;
|
|
6
|
+
type CodeHeaderProps = {
|
|
7
|
+
language: string | undefined;
|
|
8
|
+
code: string;
|
|
9
|
+
};
|
|
10
|
+
type SyntaxHighlighterProps = {
|
|
11
|
+
components: {
|
|
12
|
+
Pre: PreComponent;
|
|
13
|
+
Code: CodeComponent;
|
|
14
|
+
};
|
|
15
|
+
language: string;
|
|
16
|
+
code: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type MarkdownTextPrimitiveProps = Omit<Options, "components" | "children"> & {
|
|
20
|
+
components?: NonNullable<Options["components"]> & {
|
|
21
|
+
SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>;
|
|
22
|
+
CodeHeader?: ComponentType<CodeHeaderProps>;
|
|
23
|
+
};
|
|
5
24
|
smooth?: boolean;
|
|
6
25
|
};
|
|
7
26
|
declare const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps>;
|
|
8
27
|
|
|
9
|
-
export { MarkdownTextPrimitive, type MarkdownTextPrimitiveProps };
|
|
28
|
+
export { type CodeHeaderProps, MarkdownTextPrimitive, type MarkdownTextPrimitiveProps, type SyntaxHighlighterProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
|
-
import { FC } from 'react';
|
|
1
|
+
import { ComponentType, FC } from 'react';
|
|
2
2
|
import { Options } from 'react-markdown';
|
|
3
3
|
|
|
4
|
-
type
|
|
4
|
+
type PreComponent = NonNullable<NonNullable<Options["components"]>["pre"]>;
|
|
5
|
+
type CodeComponent = NonNullable<NonNullable<Options["components"]>["code"]>;
|
|
6
|
+
type CodeHeaderProps = {
|
|
7
|
+
language: string | undefined;
|
|
8
|
+
code: string;
|
|
9
|
+
};
|
|
10
|
+
type SyntaxHighlighterProps = {
|
|
11
|
+
components: {
|
|
12
|
+
Pre: PreComponent;
|
|
13
|
+
Code: CodeComponent;
|
|
14
|
+
};
|
|
15
|
+
language: string;
|
|
16
|
+
code: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type MarkdownTextPrimitiveProps = Omit<Options, "components" | "children"> & {
|
|
20
|
+
components?: NonNullable<Options["components"]> & {
|
|
21
|
+
SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>;
|
|
22
|
+
CodeHeader?: ComponentType<CodeHeaderProps>;
|
|
23
|
+
};
|
|
5
24
|
smooth?: boolean;
|
|
6
25
|
};
|
|
7
26
|
declare const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps>;
|
|
8
27
|
|
|
9
|
-
export { MarkdownTextPrimitive, type MarkdownTextPrimitiveProps };
|
|
28
|
+
export { type CodeHeaderProps, MarkdownTextPrimitive, type MarkdownTextPrimitiveProps, type SyntaxHighlighterProps };
|
package/dist/index.js
CHANGED
|
@@ -35,16 +35,99 @@ __export(src_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
36
|
|
|
37
37
|
// src/MarkdownText.tsx
|
|
38
|
-
var
|
|
38
|
+
var import_react3 = require("@assistant-ui/react");
|
|
39
39
|
var import_react_markdown = __toESM(require("react-markdown"));
|
|
40
|
+
|
|
41
|
+
// src/PreOverride.tsx
|
|
42
|
+
var import_react = require("react");
|
|
40
43
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
41
|
-
var
|
|
42
|
-
var
|
|
44
|
+
var PreContext = (0, import_react.createContext)(null);
|
|
45
|
+
var PreOverride = ({ children, ...rest }) => {
|
|
46
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PreContext.Provider, { value: rest, children });
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/defaultComponents.tsx
|
|
50
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
51
|
+
var DefaultPre = ({ node, ...rest }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("pre", { ...rest });
|
|
52
|
+
var DefaultCode = ({ node, ...rest }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("code", { ...rest });
|
|
53
|
+
var DefaultSyntaxHighlighter = ({ components: { Pre, Code }, code }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Pre, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Code, { children: code }) });
|
|
54
|
+
var DefaultCodeHeader = () => null;
|
|
55
|
+
|
|
56
|
+
// src/MarkdownText.tsx
|
|
57
|
+
var import_react_use_callback_ref2 = require("@radix-ui/react-use-callback-ref");
|
|
58
|
+
|
|
59
|
+
// src/CodeOverride.tsx
|
|
60
|
+
var import_react2 = require("react");
|
|
61
|
+
var import_react_slot = require("@radix-ui/react-slot");
|
|
62
|
+
var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
|
|
63
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
64
|
+
var CodeBlockOverride = ({
|
|
65
|
+
components: { Pre, Code, SyntaxHighlighter, CodeHeader },
|
|
66
|
+
children,
|
|
67
|
+
...codeProps
|
|
68
|
+
}) => {
|
|
69
|
+
const preProps = (0, import_react2.useContext)(PreContext);
|
|
70
|
+
const WrappedPre = (0, import_react_use_callback_ref.useCallbackRef)((props) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_slot.Slot, { ...preProps, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Pre, { ...props }) }));
|
|
71
|
+
const WrappedCode = (0, import_react_use_callback_ref.useCallbackRef)((props) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_slot.Slot, { ...codeProps, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Code, { ...props }) }));
|
|
72
|
+
const components = (0, import_react2.useMemo)(
|
|
73
|
+
() => ({ Pre: WrappedPre, Code: WrappedCode }),
|
|
74
|
+
[WrappedPre, WrappedCode]
|
|
75
|
+
);
|
|
76
|
+
const language = /language-(\w+)/.exec(codeProps.className || "")?.[1];
|
|
77
|
+
const code = children;
|
|
78
|
+
const SH = language ? SyntaxHighlighter : DefaultSyntaxHighlighter;
|
|
79
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
|
|
80
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CodeHeader, { language, code }),
|
|
81
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
82
|
+
SH,
|
|
83
|
+
{
|
|
84
|
+
components,
|
|
85
|
+
language: language ?? "unknown",
|
|
86
|
+
code
|
|
87
|
+
}
|
|
88
|
+
)
|
|
89
|
+
] });
|
|
90
|
+
};
|
|
91
|
+
var CodeOverride = ({
|
|
92
|
+
components,
|
|
93
|
+
...props
|
|
94
|
+
}) => {
|
|
95
|
+
const preProps = (0, import_react2.useContext)(PreContext);
|
|
96
|
+
if (!preProps) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(components.Code, { ...props });
|
|
97
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CodeBlockOverride, { components, ...props });
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/MarkdownText.tsx
|
|
101
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
102
|
+
var { useSmooth } = import_react3.INTERNAL;
|
|
103
|
+
var MarkdownTextPrimitive = ({
|
|
104
|
+
smooth = true,
|
|
105
|
+
components: userComponents,
|
|
106
|
+
...rest
|
|
107
|
+
}) => {
|
|
43
108
|
const {
|
|
44
109
|
part: { text }
|
|
45
|
-
} = (0,
|
|
46
|
-
const smoothText = useSmooth(text,
|
|
47
|
-
|
|
110
|
+
} = (0, import_react3.useContentPartText)();
|
|
111
|
+
const smoothText = useSmooth(text, smooth);
|
|
112
|
+
const {
|
|
113
|
+
pre = DefaultPre,
|
|
114
|
+
code = DefaultCode,
|
|
115
|
+
SyntaxHighlighter = DefaultSyntaxHighlighter,
|
|
116
|
+
CodeHeader = DefaultCodeHeader,
|
|
117
|
+
...componentsRest
|
|
118
|
+
} = userComponents ?? {};
|
|
119
|
+
const components = {
|
|
120
|
+
...componentsRest,
|
|
121
|
+
pre: PreOverride,
|
|
122
|
+
code: (0, import_react_use_callback_ref2.useCallbackRef)((props) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
123
|
+
CodeOverride,
|
|
124
|
+
{
|
|
125
|
+
components: { Pre: pre, Code: code, SyntaxHighlighter, CodeHeader },
|
|
126
|
+
...props
|
|
127
|
+
}
|
|
128
|
+
))
|
|
129
|
+
};
|
|
130
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_markdown.default, { components, ...rest, children: smoothText });
|
|
48
131
|
};
|
|
49
132
|
// Annotate the CommonJS export names for ESM import in node:
|
|
50
133
|
0 && (module.exports = {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/MarkdownText.tsx"],"sourcesContent":["export {\n MarkdownTextPrimitive,\n type MarkdownTextPrimitiveProps,\n} from \"./MarkdownText\";\n","import { INTERNAL, useContentPartText } from \"@assistant-ui/react\";\nimport type { FC } from \"react\";\nimport ReactMarkdown, { type Options } from \"react-markdown\";\n\nconst { useSmooth } = INTERNAL;\n\nexport type MarkdownTextPrimitiveProps = Omit
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/MarkdownText.tsx","../src/PreOverride.tsx","../src/defaultComponents.tsx","../src/CodeOverride.tsx"],"sourcesContent":["export {\n MarkdownTextPrimitive,\n type MarkdownTextPrimitiveProps,\n} from \"./MarkdownText\";\n\nexport type { CodeHeaderProps, SyntaxHighlighterProps } from \"./types\";\n","import { INTERNAL, useContentPartText } from \"@assistant-ui/react\";\nimport type { ComponentType, FC } from \"react\";\nimport ReactMarkdown, { type Options } from \"react-markdown\";\nimport { SyntaxHighlighterProps, CodeHeaderProps } from \"./types\";\nimport { PreOverride } from \"./PreOverride\";\nimport {\n DefaultPre,\n DefaultCode,\n DefaultSyntaxHighlighter,\n DefaultCodeHeader,\n} from \"./defaultComponents\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { CodeOverride } from \"./CodeOverride\";\n\nconst { useSmooth } = INTERNAL;\n\nexport type MarkdownTextPrimitiveProps = Omit<\n Options,\n \"components\" | \"children\"\n> & {\n components?: NonNullable<Options[\"components\"]> & {\n SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>;\n CodeHeader?: ComponentType<CodeHeaderProps>;\n };\n smooth?: boolean;\n};\nexport const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps> = ({\n smooth = true,\n components: userComponents,\n ...rest\n}) => {\n const {\n part: { text },\n } = useContentPartText();\n const smoothText = useSmooth(text, smooth);\n\n const {\n pre = DefaultPre,\n code = DefaultCode,\n SyntaxHighlighter = DefaultSyntaxHighlighter,\n CodeHeader = DefaultCodeHeader,\n ...componentsRest\n } = userComponents ?? {};\n const components: typeof userComponents = {\n ...componentsRest,\n pre: PreOverride,\n code: useCallbackRef((props) => (\n <CodeOverride\n components={{ Pre: pre, Code: code, SyntaxHighlighter, CodeHeader }}\n {...props}\n />\n )),\n };\n\n return (\n <ReactMarkdown components={components} {...rest}>\n {smoothText}\n </ReactMarkdown>\n );\n};\n","import { createContext, ComponentPropsWithoutRef } from \"react\";\nimport { PreComponent } from \"./types\";\n\nexport const PreContext = createContext<Omit<\n ComponentPropsWithoutRef<PreComponent>,\n \"children\"\n> | null>(null);\n\nexport const PreOverride: PreComponent = ({ children, ...rest }) => {\n return <PreContext.Provider value={rest}>{children}</PreContext.Provider>;\n};\n","import type { ComponentType } from \"react\";\nimport {\n PreComponent,\n CodeComponent,\n SyntaxHighlighterProps,\n CodeHeaderProps,\n} from \"./types\";\n\nexport const DefaultPre: PreComponent = ({ node, ...rest }) => (\n <pre {...rest} />\n);\nexport const DefaultCode: CodeComponent = ({ node, ...rest }) => (\n <code {...rest} />\n);\nexport const DefaultSyntaxHighlighter: ComponentType<\n SyntaxHighlighterProps\n> = ({ components: { Pre, Code }, code }) => (\n <Pre>\n <Code>{code}</Code>\n </Pre>\n);\nexport const DefaultCodeHeader: ComponentType<CodeHeaderProps> = () => null;\n","import {\n ComponentPropsWithoutRef,\n ComponentType,\n FC,\n useContext,\n useMemo,\n} from \"react\";\nimport { Slot } from \"@radix-ui/react-slot\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { PreContext } from \"./PreOverride\";\nimport {\n CodeComponent,\n CodeHeaderProps,\n PreComponent,\n SyntaxHighlighterProps,\n} from \"./types\";\nimport { DefaultSyntaxHighlighter } from \"./defaultComponents\";\n\ntype CodeOverrideProps = ComponentPropsWithoutRef<CodeComponent> & {\n components: {\n Pre: PreComponent;\n Code: CodeComponent;\n CodeHeader: ComponentType<CodeHeaderProps>;\n SyntaxHighlighter: ComponentType<SyntaxHighlighterProps>;\n };\n};\n\nconst CodeBlockOverride: FC<CodeOverrideProps> = ({\n components: { Pre, Code, SyntaxHighlighter, CodeHeader },\n children,\n ...codeProps\n}) => {\n const preProps = useContext(PreContext);\n const WrappedPre: PreComponent = useCallbackRef((props) => (\n <Slot {...(preProps as any)}>\n <Pre {...props} />\n </Slot>\n ));\n const WrappedCode: CodeComponent = useCallbackRef((props) => (\n <Slot {...(codeProps as any)}>\n <Code {...props} />\n </Slot>\n ));\n\n const components = useMemo(\n () => ({ Pre: WrappedPre, Code: WrappedCode }),\n [WrappedPre, WrappedCode],\n );\n\n const language = /language-(\\w+)/.exec(codeProps.className || \"\")?.[1];\n const code = children as string;\n const SH = language ? SyntaxHighlighter : DefaultSyntaxHighlighter;\n\n return (\n <>\n <CodeHeader language={language} code={code} />\n <SH\n components={components}\n language={language ?? \"unknown\"}\n code={code}\n />\n </>\n );\n};\n\nexport const CodeOverride: FC<CodeOverrideProps> = ({\n components,\n ...props\n}) => {\n const preProps = useContext(PreContext);\n if (!preProps) return <components.Code {...(props as any)} />;\n return <CodeBlockOverride components={components} {...props} />;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAA6C;AAE7C,4BAA4C;;;ACF5C,mBAAwD;AAS/C;AANF,IAAM,iBAAa,4BAGhB,IAAI;AAEP,IAAM,cAA4B,CAAC,EAAE,UAAU,GAAG,KAAK,MAAM;AAClE,SAAO,4CAAC,WAAW,UAAX,EAAoB,OAAO,MAAO,UAAS;AACrD;;;ACDE,IAAAC,sBAAA;AADK,IAAM,aAA2B,CAAC,EAAE,MAAM,GAAG,KAAK,MACvD,6CAAC,SAAK,GAAG,MAAM;AAEV,IAAM,cAA6B,CAAC,EAAE,MAAM,GAAG,KAAK,MACzD,6CAAC,UAAM,GAAG,MAAM;AAEX,IAAM,2BAET,CAAC,EAAE,YAAY,EAAE,KAAK,KAAK,GAAG,KAAK,MACrC,6CAAC,OACC,uDAAC,QAAM,gBAAK,GACd;AAEK,IAAM,oBAAoD,MAAM;;;AFVvE,IAAAC,iCAA+B;;;AGX/B,IAAAC,gBAMO;AACP,wBAAqB;AACrB,oCAA+B;AA2BzB,IAAAC,sBAAA;AARN,IAAM,oBAA2C,CAAC;AAAA,EAChD,YAAY,EAAE,KAAK,MAAM,mBAAmB,WAAW;AAAA,EACvD;AAAA,EACA,GAAG;AACL,MAAM;AACJ,QA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AAAA,IACxC,GAAG;AAAA,IACH,KAAK;AAAA,IACL,UAAM,+CAAe,CAAC,UACpB;AAAA,MAAC;AAAA;AAAA,QACC,YAAY,EAAE,KAAK,KAAK,MAAM,MAAM,mBAAmB,WAAW;AAAA,QACjE,GAAG;AAAA;AAAA,IACN,CACD;AAAA,EACH;AAEA,SACE,6CAAC,sBAAAC,SAAA,EAAc,YAAyB,GAAG,MACxC,sBACH;AAEJ;","names":["import_react","import_jsx_runtime","import_react_use_callback_ref","import_react","import_jsx_runtime","import_jsx_runtime","ReactMarkdown"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,100 @@
|
|
|
1
1
|
// src/MarkdownText.tsx
|
|
2
2
|
import { INTERNAL, useContentPartText } from "@assistant-ui/react";
|
|
3
3
|
import ReactMarkdown from "react-markdown";
|
|
4
|
+
|
|
5
|
+
// src/PreOverride.tsx
|
|
6
|
+
import { createContext } from "react";
|
|
4
7
|
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
var PreContext = createContext(null);
|
|
9
|
+
var PreOverride = ({ children, ...rest }) => {
|
|
10
|
+
return /* @__PURE__ */ jsx(PreContext.Provider, { value: rest, children });
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/defaultComponents.tsx
|
|
14
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
15
|
+
var DefaultPre = ({ node, ...rest }) => /* @__PURE__ */ jsx2("pre", { ...rest });
|
|
16
|
+
var DefaultCode = ({ node, ...rest }) => /* @__PURE__ */ jsx2("code", { ...rest });
|
|
17
|
+
var DefaultSyntaxHighlighter = ({ components: { Pre, Code }, code }) => /* @__PURE__ */ jsx2(Pre, { children: /* @__PURE__ */ jsx2(Code, { children: code }) });
|
|
18
|
+
var DefaultCodeHeader = () => null;
|
|
19
|
+
|
|
20
|
+
// src/MarkdownText.tsx
|
|
21
|
+
import { useCallbackRef as useCallbackRef2 } from "@radix-ui/react-use-callback-ref";
|
|
22
|
+
|
|
23
|
+
// src/CodeOverride.tsx
|
|
24
|
+
import {
|
|
25
|
+
useContext,
|
|
26
|
+
useMemo
|
|
27
|
+
} from "react";
|
|
28
|
+
import { Slot } from "@radix-ui/react-slot";
|
|
29
|
+
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
30
|
+
import { Fragment, jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
31
|
+
var CodeBlockOverride = ({
|
|
32
|
+
components: { Pre, Code, SyntaxHighlighter, CodeHeader },
|
|
33
|
+
children,
|
|
34
|
+
...codeProps
|
|
35
|
+
}) => {
|
|
36
|
+
const preProps = useContext(PreContext);
|
|
37
|
+
const WrappedPre = useCallbackRef((props) => /* @__PURE__ */ jsx3(Slot, { ...preProps, children: /* @__PURE__ */ jsx3(Pre, { ...props }) }));
|
|
38
|
+
const WrappedCode = useCallbackRef((props) => /* @__PURE__ */ jsx3(Slot, { ...codeProps, children: /* @__PURE__ */ jsx3(Code, { ...props }) }));
|
|
39
|
+
const components = useMemo(
|
|
40
|
+
() => ({ Pre: WrappedPre, Code: WrappedCode }),
|
|
41
|
+
[WrappedPre, WrappedCode]
|
|
42
|
+
);
|
|
43
|
+
const language = /language-(\w+)/.exec(codeProps.className || "")?.[1];
|
|
44
|
+
const code = children;
|
|
45
|
+
const SH = language ? SyntaxHighlighter : DefaultSyntaxHighlighter;
|
|
46
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
47
|
+
/* @__PURE__ */ jsx3(CodeHeader, { language, code }),
|
|
48
|
+
/* @__PURE__ */ jsx3(
|
|
49
|
+
SH,
|
|
50
|
+
{
|
|
51
|
+
components,
|
|
52
|
+
language: language ?? "unknown",
|
|
53
|
+
code
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
] });
|
|
57
|
+
};
|
|
58
|
+
var CodeOverride = ({
|
|
59
|
+
components,
|
|
60
|
+
...props
|
|
61
|
+
}) => {
|
|
62
|
+
const preProps = useContext(PreContext);
|
|
63
|
+
if (!preProps) return /* @__PURE__ */ jsx3(components.Code, { ...props });
|
|
64
|
+
return /* @__PURE__ */ jsx3(CodeBlockOverride, { components, ...props });
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/MarkdownText.tsx
|
|
68
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
5
69
|
var { useSmooth } = INTERNAL;
|
|
6
|
-
var MarkdownTextPrimitive = (
|
|
70
|
+
var MarkdownTextPrimitive = ({
|
|
71
|
+
smooth = true,
|
|
72
|
+
components: userComponents,
|
|
73
|
+
...rest
|
|
74
|
+
}) => {
|
|
7
75
|
const {
|
|
8
76
|
part: { text }
|
|
9
77
|
} = useContentPartText();
|
|
10
|
-
const smoothText = useSmooth(text,
|
|
11
|
-
|
|
78
|
+
const smoothText = useSmooth(text, smooth);
|
|
79
|
+
const {
|
|
80
|
+
pre = DefaultPre,
|
|
81
|
+
code = DefaultCode,
|
|
82
|
+
SyntaxHighlighter = DefaultSyntaxHighlighter,
|
|
83
|
+
CodeHeader = DefaultCodeHeader,
|
|
84
|
+
...componentsRest
|
|
85
|
+
} = userComponents ?? {};
|
|
86
|
+
const components = {
|
|
87
|
+
...componentsRest,
|
|
88
|
+
pre: PreOverride,
|
|
89
|
+
code: useCallbackRef2((props) => /* @__PURE__ */ jsx4(
|
|
90
|
+
CodeOverride,
|
|
91
|
+
{
|
|
92
|
+
components: { Pre: pre, Code: code, SyntaxHighlighter, CodeHeader },
|
|
93
|
+
...props
|
|
94
|
+
}
|
|
95
|
+
))
|
|
96
|
+
};
|
|
97
|
+
return /* @__PURE__ */ jsx4(ReactMarkdown, { components, ...rest, children: smoothText });
|
|
12
98
|
};
|
|
13
99
|
export {
|
|
14
100
|
MarkdownTextPrimitive
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/MarkdownText.tsx"],"sourcesContent":["import { INTERNAL, useContentPartText } from \"@assistant-ui/react\";\nimport type { FC } from \"react\";\nimport ReactMarkdown, { type Options } from \"react-markdown\";\n\nconst { useSmooth } = INTERNAL;\n\nexport type MarkdownTextPrimitiveProps = Omit
|
|
1
|
+
{"version":3,"sources":["../src/MarkdownText.tsx","../src/PreOverride.tsx","../src/defaultComponents.tsx","../src/CodeOverride.tsx"],"sourcesContent":["import { INTERNAL, useContentPartText } from \"@assistant-ui/react\";\nimport type { ComponentType, FC } from \"react\";\nimport ReactMarkdown, { type Options } from \"react-markdown\";\nimport { SyntaxHighlighterProps, CodeHeaderProps } from \"./types\";\nimport { PreOverride } from \"./PreOverride\";\nimport {\n DefaultPre,\n DefaultCode,\n DefaultSyntaxHighlighter,\n DefaultCodeHeader,\n} from \"./defaultComponents\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { CodeOverride } from \"./CodeOverride\";\n\nconst { useSmooth } = INTERNAL;\n\nexport type MarkdownTextPrimitiveProps = Omit<\n Options,\n \"components\" | \"children\"\n> & {\n components?: NonNullable<Options[\"components\"]> & {\n SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>;\n CodeHeader?: ComponentType<CodeHeaderProps>;\n };\n smooth?: boolean;\n};\nexport const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps> = ({\n smooth = true,\n components: userComponents,\n ...rest\n}) => {\n const {\n part: { text },\n } = useContentPartText();\n const smoothText = useSmooth(text, smooth);\n\n const {\n pre = DefaultPre,\n code = DefaultCode,\n SyntaxHighlighter = DefaultSyntaxHighlighter,\n CodeHeader = DefaultCodeHeader,\n ...componentsRest\n } = userComponents ?? {};\n const components: typeof userComponents = {\n ...componentsRest,\n pre: PreOverride,\n code: useCallbackRef((props) => (\n <CodeOverride\n components={{ Pre: pre, Code: code, SyntaxHighlighter, CodeHeader }}\n {...props}\n />\n )),\n };\n\n return (\n <ReactMarkdown components={components} {...rest}>\n {smoothText}\n </ReactMarkdown>\n );\n};\n","import { createContext, ComponentPropsWithoutRef } from \"react\";\nimport { PreComponent } from \"./types\";\n\nexport const PreContext = createContext<Omit<\n ComponentPropsWithoutRef<PreComponent>,\n \"children\"\n> | null>(null);\n\nexport const PreOverride: PreComponent = ({ children, ...rest }) => {\n return <PreContext.Provider value={rest}>{children}</PreContext.Provider>;\n};\n","import type { ComponentType } from \"react\";\nimport {\n PreComponent,\n CodeComponent,\n SyntaxHighlighterProps,\n CodeHeaderProps,\n} from \"./types\";\n\nexport const DefaultPre: PreComponent = ({ node, ...rest }) => (\n <pre {...rest} />\n);\nexport const DefaultCode: CodeComponent = ({ node, ...rest }) => (\n <code {...rest} />\n);\nexport const DefaultSyntaxHighlighter: ComponentType<\n SyntaxHighlighterProps\n> = ({ components: { Pre, Code }, code }) => (\n <Pre>\n <Code>{code}</Code>\n </Pre>\n);\nexport const DefaultCodeHeader: ComponentType<CodeHeaderProps> = () => null;\n","import {\n ComponentPropsWithoutRef,\n ComponentType,\n FC,\n useContext,\n useMemo,\n} from \"react\";\nimport { Slot } from \"@radix-ui/react-slot\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { PreContext } from \"./PreOverride\";\nimport {\n CodeComponent,\n CodeHeaderProps,\n PreComponent,\n SyntaxHighlighterProps,\n} from \"./types\";\nimport { DefaultSyntaxHighlighter } from \"./defaultComponents\";\n\ntype CodeOverrideProps = ComponentPropsWithoutRef<CodeComponent> & {\n components: {\n Pre: PreComponent;\n Code: CodeComponent;\n CodeHeader: ComponentType<CodeHeaderProps>;\n SyntaxHighlighter: ComponentType<SyntaxHighlighterProps>;\n };\n};\n\nconst CodeBlockOverride: FC<CodeOverrideProps> = ({\n components: { Pre, Code, SyntaxHighlighter, CodeHeader },\n children,\n ...codeProps\n}) => {\n const preProps = useContext(PreContext);\n const WrappedPre: PreComponent = useCallbackRef((props) => (\n <Slot {...(preProps as any)}>\n <Pre {...props} />\n </Slot>\n ));\n const WrappedCode: CodeComponent = useCallbackRef((props) => (\n <Slot {...(codeProps as any)}>\n <Code {...props} />\n </Slot>\n ));\n\n const components = useMemo(\n () => ({ Pre: WrappedPre, Code: WrappedCode }),\n [WrappedPre, WrappedCode],\n );\n\n const language = /language-(\\w+)/.exec(codeProps.className || \"\")?.[1];\n const code = children as string;\n const SH = language ? SyntaxHighlighter : DefaultSyntaxHighlighter;\n\n return (\n <>\n <CodeHeader language={language} code={code} />\n <SH\n components={components}\n language={language ?? \"unknown\"}\n code={code}\n />\n </>\n );\n};\n\nexport const CodeOverride: FC<CodeOverrideProps> = ({\n components,\n ...props\n}) => {\n const preProps = useContext(PreContext);\n if (!preProps) return <components.Code {...(props as any)} />;\n return <CodeBlockOverride components={components} {...props} />;\n};\n"],"mappings":";AAAA,SAAS,UAAU,0BAA0B;AAE7C,OAAO,mBAAqC;;;ACF5C,SAAS,qBAA+C;AAS/C;AANF,IAAM,aAAa,cAGhB,IAAI;AAEP,IAAM,cAA4B,CAAC,EAAE,UAAU,GAAG,KAAK,MAAM;AAClE,SAAO,oBAAC,WAAW,UAAX,EAAoB,OAAO,MAAO,UAAS;AACrD;;;ACDE,gBAAAA,YAAA;AADK,IAAM,aAA2B,CAAC,EAAE,MAAM,GAAG,KAAK,MACvD,gBAAAA,KAAC,SAAK,GAAG,MAAM;AAEV,IAAM,cAA6B,CAAC,EAAE,MAAM,GAAG,KAAK,MACzD,gBAAAA,KAAC,UAAM,GAAG,MAAM;AAEX,IAAM,2BAET,CAAC,EAAE,YAAY,EAAE,KAAK,KAAK,GAAG,KAAK,MACrC,gBAAAA,KAAC,OACC,0BAAAA,KAAC,QAAM,gBAAK,GACd;AAEK,IAAM,oBAAoD,MAAM;;;AFVvE,SAAS,kBAAAC,uBAAsB;;;AGX/B;AAAA,EAIE;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY;AACrB,SAAS,sBAAsB;AA2BzB,SAmBF,UAnBE,OAAAC,MAmBF,YAnBE;AARN,IAAM,oBAA2C,CAAC;AAAA,EAChD,YAAY,EAAE,KAAK,MAAM,mBAAmB,WAAW;AAAA,EACvD;AAAA,EACA,GAAG;AACL,MAAM;AACJ,QAAM,WA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L,IAAI,kBAAkB,CAAC;AACvB,QAAM,aAAoC;AAAA,IACxC,GAAG;AAAA,IACH,KAAK;AAAA,IACL,MAAMC,gBAAe,CAAC,UACpB,gBAAAD;AAAA,MAAC;AAAA;AAAA,QACC,YAAY,EAAE,KAAK,KAAK,MAAM,MAAM,mBAAmB,WAAW;AAAA,QACjE,GAAG;AAAA;AAAA,IACN,CACD;AAAA,EACH;AAEA,SACE,gBAAAA,KAAC,iBAAc,YAAyB,GAAG,MACxC,sBACH;AAEJ;","names":["jsx","useCallbackRef","jsx","jsx","useCallbackRef"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@assistant-ui/react-markdown",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -24,11 +24,12 @@
|
|
|
24
24
|
],
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"react-
|
|
28
|
-
"
|
|
27
|
+
"@radix-ui/react-slot": "^1.1.0",
|
|
28
|
+
"@radix-ui/react-use-callback-ref": "^1.1.0",
|
|
29
|
+
"react-markdown": "^9.0.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@assistant-ui/react": "^0.3.
|
|
32
|
+
"@assistant-ui/react": "^0.3.5",
|
|
32
33
|
"@types/react": "*",
|
|
33
34
|
"react": "^18"
|
|
34
35
|
},
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"
|
|
42
|
+
"eslint-config-next": "14.2.4",
|
|
42
43
|
"tsup": "^8.1.0",
|
|
43
44
|
"@assistant-ui/tsconfig": "0.0.0"
|
|
44
45
|
},
|
|
@@ -55,6 +56,6 @@
|
|
|
55
56
|
"url": "https://github.com/Yonom/assistant-ui/issues"
|
|
56
57
|
},
|
|
57
58
|
"scripts": {
|
|
58
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap"
|
|
59
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap --clean"
|
|
59
60
|
}
|
|
60
61
|
}
|