Skip to content

Commit

Permalink
Add support for titles in code
Browse files Browse the repository at this point in the history
  • Loading branch information
cdedreuille committed Jun 19, 2024
1 parent c50de08 commit 3cbba4b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
22 changes: 22 additions & 0 deletions apps/frontpage/components/docs/mdx/figcaption.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use client';

import { useEffect, type FC, type ReactNode } from 'react';
import { useFigure } from './figure-provider';

interface PreProps {
children?: ReactNode;
}

export const Figcaption: FC<PreProps> = (props) => {
const { setTitle } = useFigure();

useEffect(() => {
setTitle(props.children as string);
}, []);

return (
<figcaption {...props} className="hidden">
{props.children}
</figcaption>
);
};
36 changes: 36 additions & 0 deletions apps/frontpage/components/docs/mdx/figure-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use client';

import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';

export interface FigureContextProps {
title: string;
setTitle: (id: string) => void;
}

export const FigureContext = createContext<FigureContextProps | undefined>(
undefined,
);

export function FigureProvider({ children }: { children: ReactNode }) {
const [title, setTitle] = useState<string>('');

return (
<FigureContext.Provider
value={{
title,
setTitle,
}}
>
{children}
</FigureContext.Provider>
);
}

export function useFigure() {
const context = useContext(FigureContext);
if (context === undefined) {
throw new Error('useFigure must be used within a FigureProvider');
}
return context;
}
14 changes: 14 additions & 0 deletions apps/frontpage/components/docs/mdx/figure.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { FC, ReactNode } from 'react';
import { FigureProvider } from './figure-provider';

interface PreProps {
children?: ReactNode;
}

export const Figure: FC<PreProps> = (props) => {
return (
<FigureProvider>
<figure {...props}>{props.children}</figure>
</FigureProvider>
);
};
2 changes: 2 additions & 0 deletions apps/frontpage/components/docs/mdx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { Table } from './table';
export { Th } from './th';
export { Tr } from './tr';
export { Td } from './td';
export { Figure } from './figure';
export { Figcaption } from './figcaption';
7 changes: 6 additions & 1 deletion apps/frontpage/components/docs/mdx/pre.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use client';

import type { FC, ReactNode } from 'react';
import { CodeWrapper } from './code-snippets/wrapper';
import { useFigure } from './figure-provider';

interface PreProps {
children?: ReactNode;
raw?: string;
}

export const Pre: FC<PreProps> = ({ children, raw }) => {
const { title } = useFigure();

return (
<CodeWrapper copy={raw}>
<CodeWrapper copy={raw} title={title || ''}>
<pre>{children}</pre>
</CodeWrapper>
);
Expand Down
4 changes: 4 additions & 0 deletions apps/frontpage/lib/get-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
Tr,
Td,
OrderedList,
Figure,
Figcaption,
} from '../components/docs/mdx';
import { generateDocsTree } from './get-tree';
import { rehypePrettyCodeOptions } from './rehype-pretty-code-options';
Expand Down Expand Up @@ -150,6 +152,8 @@ export const getPageData = async (
th: Th,
tr: Tr,
td: Td,
figure: Figure,
figcaption: Figcaption,
img: (props) => <Img activeVersion={activeVersion.id} {...props} />,
Video: (props) => <Video activeVersion={activeVersion.id} {...props} />,
CodeSnippets: (props) => (
Expand Down

0 comments on commit 3cbba4b

Please sign in to comment.