Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions frontend/components/q&a/AccordionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,21 @@ function renderTextNodes(
return nodes.map((node, i) => renderTextNode(node, i, highlight));
}

function resolveCodeContent(
block: Pick<CodeBlockEntry, 'content' | 'code'>
): string {
if (typeof block.content === 'string') return block.content;
if (typeof block.code === 'string') return block.code;
return '';
}

function renderCodeBlock(block: CodeBlockEntry, index: number): ReactNode {
return (
<CodeBlock key={index} code={block.content} language={block.language} />
<CodeBlock
key={index}
code={resolveCodeContent(block)}
language={block.language}
/>
);
}

Expand Down Expand Up @@ -236,7 +248,11 @@ function renderListItemChildren(

if ('type' in child && child.type === 'code') {
return (
<CodeBlock key={i} code={child.content} language={child.language} />
<CodeBlock
key={i}
code={resolveCodeContent(child)}
language={child.language}
/>
);
}

Expand Down
8 changes: 5 additions & 3 deletions frontend/components/q&a/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import { Highlight, themes } from 'prism-react-renderer';
import { useState } from 'react';

type Props = {
code: string;
code?: string | null;
language?: string | null;
};

export default function CodeBlock({ code, language }: Props) {
const { resolvedTheme } = useTheme();
const [copied, setCopied] = useState(false);
const safeCode = typeof code === 'string' ? code : '';

const theme = resolvedTheme === 'dark' ? themes.nightOwl : themes.github;

const handleCopy = async () => {
await navigator.clipboard.writeText(code);
if (!safeCode) return;
await navigator.clipboard.writeText(safeCode);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
};

return (
<Highlight
code={code.trim()}
code={safeCode.trim()}
language={(language ?? 'markup') as any}
theme={theme}
>
Expand Down
3 changes: 2 additions & 1 deletion frontend/components/q&a/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export type TextNode = {
export type CodeBlock = {
type: 'code';
language: string | null;
content: string;
content?: string;
code?: string;
};

export type ListEntry = ListItemBlock | ListItemChild;
Expand Down
Loading