mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-30 06:02:00 +02:00
Response Streaming (#124)
This commit is contained in:
@@ -1,26 +1,34 @@
|
||||
import type { EditorProps } from '../components/core/Editor';
|
||||
|
||||
export function languageFromContentType(contentType: string | null): EditorProps['language'] {
|
||||
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 'html';
|
||||
return detectFromContent(content);
|
||||
} else if (justContentType.includes('javascript')) {
|
||||
return 'javascript';
|
||||
} else {
|
||||
return 'text';
|
||||
}
|
||||
|
||||
return detectFromContent(content);
|
||||
}
|
||||
|
||||
export function isJSON(text: string): boolean {
|
||||
try {
|
||||
JSON.parse(text);
|
||||
return true;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
return text.startsWith('{') || text.startsWith('[');
|
||||
}
|
||||
|
||||
function detectFromContent(content: string | null): EditorProps['language'] {
|
||||
if (content == null) return 'text';
|
||||
|
||||
if (content.startsWith('{') || content.startsWith('[')) {
|
||||
return 'json';
|
||||
} else if (content.startsWith('<!DOCTYPE') || content.startsWith('<html')) {
|
||||
return 'html';
|
||||
}
|
||||
return 'text';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user