Skip to content

Commit

Permalink
feat(markdown): useIsMarkdownCodeBlock hook (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 26, 2024
1 parent 53541d8 commit 5114a8f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-dancers-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react-markdown": patch
---

feat: useIsMarkdownCodeBlock hook
2 changes: 2 additions & 0 deletions packages/react-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type {
SyntaxHighlighterProps,
} from "./overrides/types";

export { useIsMarkdownCodeBlock } from "./overrides/PreOverride";

export {
makeMarkdownText,
type MakeMarkdownTextProps,
Expand Down
6 changes: 5 additions & 1 deletion packages/react-markdown/src/overrides/PreOverride.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { createContext, ComponentPropsWithoutRef } from "react";
import { createContext, ComponentPropsWithoutRef, useContext } from "react";
import { PreComponent } from "./types";

export const PreContext = createContext<Omit<
ComponentPropsWithoutRef<PreComponent>,
"children"
> | null>(null);

export const useIsMarkdownCodeBlock = () => {
return useContext(PreContext) !== null;
};

export const PreOverride: PreComponent = ({ children, ...rest }) => {
return <PreContext.Provider value={rest}>{children}</PreContext.Provider>;
};
15 changes: 13 additions & 2 deletions packages/shadcn-registry/registry/assistant-ui/markdown-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
CodeHeaderProps,
MarkdownTextPrimitive,
useIsMarkdownCodeBlock,
} from "@assistant-ui/react-markdown";
import remarkGfm from "remark-gfm";
import rehypeKatex from "rehype-katex";
Expand Down Expand Up @@ -155,11 +156,21 @@ const MarkdownTextImpl = () => {
{...props}
/>
),
code: ({ node, className, ...props }) => {
pre: ({ node, className, ...props }) => (
<pre
className={cn(
"overflow-x-auto rounded-b-lg bg-black p-4 text-white",
className,
)}
{...props}
/>
),
code: function Code({ node, className, ...props }) {
const isCodeBlock = useIsMarkdownCodeBlock();
return (
<code
className={cn(
"overflow-x-auto rounded-b-lg bg-black p-4 text-white",
!isCodeBlock && "bg-aui-muted rounded border font-semibold",
className,
)}
{...props}
Expand Down

0 comments on commit 5114a8f

Please sign in to comment.