feat: add shared arborium code renderer
This commit is contained in:
3 files changed
+156
No files matched your search
@@ -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: '<span class="token keyword">const</span> value = 1;',
|
||||
language: 'typescript',
|
||||
});
|
||||
useArboriumThemeStatus.mockReturnValue({ ready: true });
|
||||
|
||||
const { container } = render(
|
||||
<CodeHighlightRenderer code="const value = 1;" language="ts" allowDetect className="code" />
|
||||
);
|
||||
|
||||
const code = container.querySelector('code');
|
||||
expect(code).toHaveClass('code');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(code?.innerHTML).toContain('<span class="token keyword">const</span>');
|
||||
});
|
||||
|
||||
expect(code?.innerHTML).toContain('<span class="token keyword">const</span>');
|
||||
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: '<span class="token keyword">const</span> value = 1;',
|
||||
language: 'typescript',
|
||||
});
|
||||
useArboriumThemeStatus.mockReturnValue({ ready: false });
|
||||
|
||||
const { container } = render(
|
||||
<CodeHighlightRenderer code="const value = 1;" language="ts" allowDetect />
|
||||
);
|
||||
|
||||
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(
|
||||
<CodeHighlightRenderer code="const value = 1;" language="ts" allowDetect />
|
||||
);
|
||||
|
||||
const code = container.querySelector('code');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(code).toHaveTextContent('const value = 1;');
|
||||
});
|
||||
|
||||
expect(code?.innerHTML).toBe('const value = 1;');
|
||||
});
|
||||
});
|
||||
@@ -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<RenderResult>(() => 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 className={className}>{code}</code>;
|
||||
}
|
||||
|
||||
// Arborium HTML is only rendered after both highlight and theme CSS are ready.
|
||||
/* eslint-disable-next-line react/no-danger */
|
||||
return <code className={className} dangerouslySetInnerHTML={{ __html: result.html }} />;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export type { HighlightCodeDeps, HighlightCodeInput, HighlightResult } from '$plugins/arborium';
|
||||
export { highlightCode, useArboriumThemeStatus } from '$plugins/arborium';
|
||||
export { CodeHighlightRenderer } from './CodeHighlightRenderer';
|
||||
Reference in new issue
Block a user