Skip to content

Commit

Permalink
Update packages/code-block/src/lib/formatter/formatter.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Ziad Beyens <[email protected]>
  • Loading branch information
patrick-hertling and zbeyens authored Feb 1, 2025
1 parent 2d6c1f5 commit 64c86b1
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions packages/code-block/src/lib/formatter/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
import { JsonFormatter } from './jsonFormatter';

export interface IFormatter {
format: (code: string) => string;
validSyntax: (code: string) => boolean;
}
import { formatJson, isValidJson } from './jsonFormatter';

const supportedLanguages = new Set(['json']);

export class Formatter {
format(code: string, lang?: string) {
if (!lang || !supportedLanguages.has(lang)) {
return '';
}
export const isLangSupported = (lang?: string): boolean =>
Boolean(lang && supportedLanguages.has(lang));

switch (lang) {
case 'json': {
return new JsonFormatter().format(code);
}
}
export const formatCode = (code: string, lang?: string): string => {
if (!isLangSupported(lang)) {
return '';
}

return code;
switch (lang) {
case 'json':
return formatJson(code);
default:
return code;
}
};

isLangSupported(lang?: string) {
return lang && supportedLanguages.has(lang);
export const isValidSyntax = (code: string, lang?: string): boolean => {
if (!isLangSupported(lang)) {
return false;
}

validSyntax(code: string, lang?: string) {
if (!lang || !supportedLanguages.has(lang)) {
switch (lang) {
case 'json':
return isValidJson(code);
default:
return false;
}

switch (lang) {
case 'json': {
return new JsonFormatter().validSyntax(code);
}
}
}
}
};

0 comments on commit 64c86b1

Please sign in to comment.