mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 21:53:36 +01:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import type { EditorProps } from '../components/core/Editor/Editor';
|
|
|
|
export function languageFromContentType(
|
|
contentType: string | null,
|
|
content: string | null = null,
|
|
): EditorProps['language'] {
|
|
const justContentType = contentType?.split(';')[0] ?? contentType ?? '';
|
|
if (justContentType.includes('json')) {
|
|
return 'json';
|
|
} else if (justContentType.includes('xml')) {
|
|
return 'xml';
|
|
} else if (justContentType.includes('html')) {
|
|
return detectFromContent(content, 'html');
|
|
} else if (justContentType.includes('javascript')) {
|
|
return 'javascript';
|
|
}
|
|
|
|
return detectFromContent(content, 'text');
|
|
}
|
|
|
|
function detectFromContent(
|
|
content: string | null,
|
|
fallback: EditorProps['language'],
|
|
): EditorProps['language'] {
|
|
if (content == null) return 'text';
|
|
|
|
if (content.startsWith('{') || content.startsWith('[')) {
|
|
return 'json';
|
|
} else if (
|
|
content.toLowerCase().startsWith('<!doctype') ||
|
|
content.toLowerCase().startsWith('<html')
|
|
) {
|
|
return 'html';
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
export function isJSON(content: string | null | undefined): boolean {
|
|
if (typeof content !== 'string') return false;
|
|
|
|
try {
|
|
JSON.parse(content)
|
|
return true;
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
}
|