Skip to content

Commit

Permalink
simply codesnippet
Browse files Browse the repository at this point in the history
  • Loading branch information
mimarz committed Apr 17, 2024
1 parent 8e2826d commit 56b9bbf
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions apps/storefront/components/CodeSnippet/CodeSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import { Tooltip } from '@digdir/designsystemet-react';

import classes from './CodeSnippet.module.css';

const plugins = [
prettierTypescript,
prettierEstree,
prettierCSS,
prettierMarkdown,
prettierHtml,
];

type CodeSnippetProps = {
language?: 'css' | 'html' | 'ts' | 'markdown';
children?: string;
Expand All @@ -26,42 +34,21 @@ const CodeSnippet = ({
const [snippet, setSnippet] = useState('');

useEffect(() => {
async function formatSnippet(children: string) {
let options: Options = {
parser: 'typescript',
plugins: [prettierTypescript, prettierEstree],
};

if (language === 'css') {
options = {
parser: 'css',
plugins: [prettierCSS],
};
}
if (language === 'markdown') {
options = {
parser: 'markdown',
plugins: [prettierMarkdown],
};
}
if (language === 'html') {
options = {
parser: 'html',
plugins: [prettierHtml],
};
}
async function formatSnippet(
children: string,
language: CodeSnippetProps['language'],
) {
try {
const formatted = await format(children, options);
const formatted = await format(children, {
parser: language === 'ts' ? 'typescript' : language,
plugins,
});
setSnippet(formatted);
} catch (error) {
console.error('Failed formatting code snippet:', {
children,
options,
error,
});
console.error('Failed formatting code snippet:', error);
}
}
void formatSnippet(children);
void formatSnippet(children, language);

return () => {
setSnippet('');
Expand Down

0 comments on commit 56b9bbf

Please sign in to comment.