-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: react-ui Code Header Syntax Highlighter support * move implementation to react-markdown
- Loading branch information
Showing
28 changed files
with
432 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@assistant-ui/react-markdown": patch | ||
"@assistant-ui/react-ui": patch | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: Code Header and Syntax Highlighter support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
ComponentPropsWithoutRef, | ||
ComponentType, | ||
FC, | ||
useContext, | ||
useMemo, | ||
} from "react"; | ||
import { Slot } from "@radix-ui/react-slot"; | ||
import { useCallbackRef } from "@radix-ui/react-use-callback-ref"; | ||
import { PreContext } from "./PreOverride"; | ||
import { | ||
CodeComponent, | ||
CodeHeaderProps, | ||
PreComponent, | ||
SyntaxHighlighterProps, | ||
} from "./types"; | ||
import { DefaultSyntaxHighlighter } from "./defaultComponents"; | ||
|
||
type CodeOverrideProps = ComponentPropsWithoutRef<CodeComponent> & { | ||
components: { | ||
Pre: PreComponent; | ||
Code: CodeComponent; | ||
CodeHeader: ComponentType<CodeHeaderProps>; | ||
SyntaxHighlighter: ComponentType<SyntaxHighlighterProps>; | ||
}; | ||
}; | ||
|
||
const CodeBlockOverride: FC<CodeOverrideProps> = ({ | ||
components: { Pre, Code, SyntaxHighlighter, CodeHeader }, | ||
children, | ||
...codeProps | ||
}) => { | ||
const preProps = useContext(PreContext); | ||
const WrappedPre: PreComponent = useCallbackRef((props) => ( | ||
<Slot {...(preProps as any)}> | ||
<Pre {...props} /> | ||
</Slot> | ||
)); | ||
const WrappedCode: CodeComponent = useCallbackRef((props) => ( | ||
<Slot {...(codeProps as any)}> | ||
<Code {...props} /> | ||
</Slot> | ||
)); | ||
|
||
const components = useMemo( | ||
() => ({ Pre: WrappedPre, Code: WrappedCode }), | ||
[WrappedPre, WrappedCode], | ||
); | ||
|
||
const language = /language-(\w+)/.exec(codeProps.className || "")?.[1]; | ||
const code = children as string; | ||
const SH = language ? SyntaxHighlighter : DefaultSyntaxHighlighter; | ||
|
||
return ( | ||
<> | ||
<CodeHeader language={language} code={code} /> | ||
<SH | ||
components={components} | ||
language={language ?? "unknown"} | ||
code={code} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export const CodeOverride: FC<CodeOverrideProps> = ({ | ||
components, | ||
...props | ||
}) => { | ||
const preProps = useContext(PreContext); | ||
if (!preProps) return <components.Code {...(props as any)} />; | ||
return <CodeBlockOverride components={components} {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,60 @@ | ||
import { INTERNAL, useContentPartText } from "@assistant-ui/react"; | ||
import type { FC } from "react"; | ||
import type { ComponentType, FC } from "react"; | ||
import ReactMarkdown, { type Options } from "react-markdown"; | ||
import { SyntaxHighlighterProps, CodeHeaderProps } from "./types"; | ||
import { PreOverride } from "./PreOverride"; | ||
import { | ||
DefaultPre, | ||
DefaultCode, | ||
DefaultSyntaxHighlighter, | ||
DefaultCodeHeader, | ||
} from "./defaultComponents"; | ||
import { useCallbackRef } from "@radix-ui/react-use-callback-ref"; | ||
import { CodeOverride } from "./CodeOverride"; | ||
|
||
const { useSmooth } = INTERNAL; | ||
|
||
export type MarkdownTextPrimitiveProps = Omit<Options, "children"> & { | ||
export type MarkdownTextPrimitiveProps = Omit< | ||
Options, | ||
"components" | "children" | ||
> & { | ||
components?: NonNullable<Options["components"]> & { | ||
SyntaxHighlighter?: ComponentType<SyntaxHighlighterProps>; | ||
CodeHeader?: ComponentType<CodeHeaderProps>; | ||
}; | ||
smooth?: boolean; | ||
}; | ||
|
||
export const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps> = ( | ||
options, | ||
) => { | ||
export const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps> = ({ | ||
smooth = true, | ||
components: userComponents, | ||
...rest | ||
}) => { | ||
const { | ||
part: { text }, | ||
} = useContentPartText(); | ||
const smoothText = useSmooth(text, options.smooth); | ||
return <ReactMarkdown {...options}>{smoothText}</ReactMarkdown>; | ||
const smoothText = useSmooth(text, smooth); | ||
|
||
const { | ||
pre = DefaultPre, | ||
code = DefaultCode, | ||
SyntaxHighlighter = DefaultSyntaxHighlighter, | ||
CodeHeader = DefaultCodeHeader, | ||
...componentsRest | ||
} = userComponents ?? {}; | ||
const components: typeof userComponents = { | ||
...componentsRest, | ||
pre: PreOverride, | ||
code: useCallbackRef((props) => ( | ||
<CodeOverride | ||
components={{ Pre: pre, Code: code, SyntaxHighlighter, CodeHeader }} | ||
{...props} | ||
/> | ||
)), | ||
}; | ||
|
||
return ( | ||
<ReactMarkdown components={components} {...rest}> | ||
{smoothText} | ||
</ReactMarkdown> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext, ComponentPropsWithoutRef } from "react"; | ||
import { PreComponent } from "./types"; | ||
|
||
export const PreContext = createContext<Omit< | ||
ComponentPropsWithoutRef<PreComponent>, | ||
"children" | ||
> | null>(null); | ||
|
||
export const PreOverride: PreComponent = ({ children, ...rest }) => { | ||
return <PreContext.Provider value={rest}>{children}</PreContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ComponentType } from "react"; | ||
import { | ||
PreComponent, | ||
CodeComponent, | ||
SyntaxHighlighterProps, | ||
CodeHeaderProps, | ||
} from "./types"; | ||
|
||
export const DefaultPre: PreComponent = ({ node, ...rest }) => ( | ||
<pre {...rest} /> | ||
); | ||
export const DefaultCode: CodeComponent = ({ node, ...rest }) => ( | ||
<code {...rest} /> | ||
); | ||
export const DefaultSyntaxHighlighter: ComponentType< | ||
SyntaxHighlighterProps | ||
> = ({ components: { Pre, Code }, code }) => ( | ||
<Pre> | ||
<Code>{code}</Code> | ||
</Pre> | ||
); | ||
export const DefaultCodeHeader: ComponentType<CodeHeaderProps> = () => null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Options } from "react-markdown"; | ||
|
||
export type PreComponent = NonNullable< | ||
NonNullable<Options["components"]>["pre"] | ||
>; | ||
export type CodeComponent = NonNullable< | ||
NonNullable<Options["components"]>["code"] | ||
>; | ||
|
||
export type CodeHeaderProps = { | ||
language: string | undefined; | ||
code: string; | ||
}; | ||
|
||
export type SyntaxHighlighterProps = { | ||
components: { | ||
Pre: PreComponent; | ||
Code: CodeComponent; | ||
}; | ||
language: string; | ||
code: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.