Skip to content

Commit

Permalink
feat: add title prop to SyntaxHighlight (#589)
Browse files Browse the repository at this point in the history
* feat: add title prop to SyntaxHighlight

* fix type

* correct prerendering
  • Loading branch information
mosch authored Jan 24, 2025
1 parent 10d2c9e commit 700098f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/zudoku/src/lib/components/SyntaxHighlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type SyntaxHighlightProps = {
copyable?: boolean;
showLanguageIndicator?: boolean;
language?: string;
title?: string;
} & Omit<HighlightProps, "children" | "language">;

const remapLang = {
Expand All @@ -50,6 +51,7 @@ const remapLang = {
export const SyntaxHighlight = ({
copyable = true,
language = "plain",
title,
...props
}: SyntaxHighlightProps) => {
const { resolvedTheme } = useTheme();
Expand All @@ -70,12 +72,18 @@ export const SyntaxHighlight = ({
<ClientOnly
fallback={
<div className="relative group">
{title && (
<div className="text-xs text-muted-foreground absolute top-2 font-mono border-b w-full pb-2 px-4 ">
{title}
</div>
)}
<pre
className={cn(
"relative scrollbar overflow-x-auto",
props.className,
props.noBackground ? "!bg-transparent" : themeColorClasses,
props.wrapLines && "whitespace-pre-wrap break-words",
title && "pt-10",
)}
>
{props.code}
Expand All @@ -95,13 +103,19 @@ export const SyntaxHighlight = ({
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<div className="relative group">
{title && (
<div className="text-xs text-muted-foreground absolute top-2 font-mono border-b w-full pb-2 px-4 ">
{title}
</div>
)}
<pre
className={cn(
"relative scrollbar overflow-x-auto",
className,
props.className,
props.noBackground && "!bg-transparent",
props.wrapLines && "whitespace-pre-wrap break-words",
title && "pt-10",
)}
style={style}
>
Expand Down
3 changes: 2 additions & 1 deletion packages/zudoku/src/lib/util/MdxComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const MdxComponents = {
code: ({ className, children, ...props }) => {
// `inline` provided by the rehype plugin, as react-markdown removed support for that
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const inline = (props as any).inline;
const { inline, title } = props as Record<string, string | boolean>;

if (inline === true || inline === "true") {
return <InlineCode className={className}>{children}</InlineCode>;
Expand All @@ -75,6 +75,7 @@ export const MdxComponents = {
className="rounded-xl p-4 border dark:!bg-foreground/10 dark:border-transparent"
showLanguageIndicator
code={String(children).trim()}
title={String(title) ?? undefined}
/>
);
},
Expand Down

0 comments on commit 700098f

Please sign in to comment.