58 lines
1.6 KiB
Markdown
58 lines
1.6 KiB
Markdown
|
|
# Task ID: 74
|
||
|
|
|
||
|
|
**Title:** Implement Content Type Detection
|
||
|
|
|
||
|
|
**Status:** done
|
||
|
|
|
||
|
|
**Dependencies:** None
|
||
|
|
|
||
|
|
**Priority:** high
|
||
|
|
|
||
|
|
**Description:** Create a utility that detects content type (JSON, YAML, XML, code, prose) for structural splitting in the section-split stage.
|
||
|
|
|
||
|
|
**Details:**
|
||
|
|
|
||
|
|
Create `src/mcplocal/src/proxymodel/content-detection.ts`:
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
export type ContentType = 'json' | 'yaml' | 'xml' | 'code' | 'prose';
|
||
|
|
|
||
|
|
export function detectContentType(content: string): ContentType {
|
||
|
|
const trimmed = content.trimStart();
|
||
|
|
|
||
|
|
// JSON detection
|
||
|
|
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
||
|
|
try {
|
||
|
|
JSON.parse(content);
|
||
|
|
return 'json';
|
||
|
|
} catch { /* not valid JSON, continue */ }
|
||
|
|
}
|
||
|
|
|
||
|
|
// XML detection
|
||
|
|
if (trimmed.startsWith('<?xml') || /^<[a-zA-Z][^>]*>/.test(trimmed)) {
|
||
|
|
return 'xml';
|
||
|
|
}
|
||
|
|
|
||
|
|
// YAML detection (key: value at start of lines)
|
||
|
|
if (/^[a-zA-Z_][a-zA-Z0-9_]*:\s/m.test(trimmed) && !trimmed.includes('{')) {
|
||
|
|
return 'yaml';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Code detection (common patterns)
|
||
|
|
const codePatterns = [
|
||
|
|
/^(function |class |def |const |let |var |import |export |package |pub fn |fn |impl )/m,
|
||
|
|
/^#include\s+[<"]/m,
|
||
|
|
/^(public |private |protected )?(static )?(void |int |string |bool )/m,
|
||
|
|
];
|
||
|
|
if (codePatterns.some(p => p.test(trimmed))) {
|
||
|
|
return 'code';
|
||
|
|
}
|
||
|
|
|
||
|
|
return 'prose';
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
**Test Strategy:**
|
||
|
|
|
||
|
|
Unit tests with sample content for each type: valid JSON objects/arrays, XML documents, YAML configs, code snippets in multiple languages (JS, Python, Rust, Go, Java), and prose markdown. Edge cases: JSON-like strings that aren't valid JSON, mixed content.
|