diff --git a/src/app/components/code-highlight/CodeHighlightRenderer.test.tsx b/src/app/components/code-highlight/CodeHighlightRenderer.test.tsx new file mode 100644 index 00000000..c0bcd8b2 --- /dev/null +++ b/src/app/components/code-highlight/CodeHighlightRenderer.test.tsx @@ -0,0 +1,88 @@ +import { render, waitFor } from '@testing-library/react'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { CodeHighlightRenderer } from '.'; + +const { highlightCode, useArboriumThemeStatus } = vi.hoisted(() => ({ + highlightCode: vi.fn(), + useArboriumThemeStatus: vi.fn(), +})); + +vi.mock('$plugins/arborium', () => ({ + highlightCode, + useArboriumThemeStatus, +})); + +afterEach(() => { + vi.clearAllMocks(); +}); + +describe('CodeHighlightRenderer', () => { + it('renders highlighted HTML when Arborium succeeds and theme is ready', async () => { + highlightCode.mockResolvedValue({ + mode: 'highlighted', + html: 'const value = 1;', + language: 'typescript', + }); + useArboriumThemeStatus.mockReturnValue({ ready: true }); + + const { container } = render( + + ); + + const code = container.querySelector('code'); + expect(code).toHaveClass('code'); + + await waitFor(() => { + expect(code?.innerHTML).toContain('const'); + }); + + expect(code?.innerHTML).toContain('const'); + expect(highlightCode).toHaveBeenCalledWith({ + code: 'const value = 1;', + language: 'ts', + allowDetect: true, + }); + }); + + it('renders plain text when theme is not ready', async () => { + highlightCode.mockResolvedValue({ + mode: 'highlighted', + html: 'const value = 1;', + language: 'typescript', + }); + useArboriumThemeStatus.mockReturnValue({ ready: false }); + + const { container } = render( + + ); + + const code = container.querySelector('code'); + + await waitFor(() => { + expect(code).toHaveTextContent('const value = 1;'); + }); + + expect(code?.innerHTML).toBe('const value = 1;'); + }); + + it('renders plain text when Arborium returns plain mode', async () => { + highlightCode.mockResolvedValue({ + mode: 'plain', + html: 'const value = 1;', + language: 'ts', + }); + useArboriumThemeStatus.mockReturnValue({ ready: true }); + + const { container } = render( + + ); + + const code = container.querySelector('code'); + + await waitFor(() => { + expect(code).toHaveTextContent('const value = 1;'); + }); + + expect(code?.innerHTML).toBe('const value = 1;'); + }); +}); diff --git a/src/app/components/code-highlight/CodeHighlightRenderer.tsx b/src/app/components/code-highlight/CodeHighlightRenderer.tsx new file mode 100644 index 00000000..bac7cc60 --- /dev/null +++ b/src/app/components/code-highlight/CodeHighlightRenderer.tsx @@ -0,0 +1,65 @@ +import { useEffect, useState } from 'react'; + +import { highlightCode, type HighlightResult, useArboriumThemeStatus } from '$plugins/arborium'; + +type CodeHighlightRendererProps = { + code: string; + language?: string; + allowDetect?: boolean; + className?: string; +}; + +type RenderResult = HighlightResult; + +const createPlainResult = (code: string, language?: string): RenderResult => { + const result: RenderResult = { + mode: 'plain', + html: code, + }; + + if (language !== undefined) { + result.language = language; + } + + return result; +}; + +export function CodeHighlightRenderer({ + code, + language, + allowDetect = false, + className, +}: CodeHighlightRendererProps) { + const { ready } = useArboriumThemeStatus(); + const [result, setResult] = useState(() => createPlainResult(code, language)); + + useEffect(() => { + let cancelled = false; + + setResult(createPlainResult(code, language)); + + highlightCode({ code, language, allowDetect }) + .then((next) => { + if (!cancelled) { + setResult(next); + } + }) + .catch(() => { + if (!cancelled) { + setResult(createPlainResult(code, language)); + } + }); + + return () => { + cancelled = true; + }; + }, [code, language, allowDetect]); + + if (!ready || result.mode === 'plain') { + return {code}; + } + + // Arborium HTML is only rendered after both highlight and theme CSS are ready. + /* eslint-disable-next-line react/no-danger */ + return ; +} diff --git a/src/app/components/code-highlight/index.ts b/src/app/components/code-highlight/index.ts new file mode 100644 index 00000000..7ab7bb6b --- /dev/null +++ b/src/app/components/code-highlight/index.ts @@ -0,0 +1,3 @@ +export type { HighlightCodeDeps, HighlightCodeInput, HighlightResult } from '$plugins/arborium'; +export { highlightCode, useArboriumThemeStatus } from '$plugins/arborium'; +export { CodeHighlightRenderer } from './CodeHighlightRenderer';