@bernierllc/markdown-detector
v0.2.2
Published
Detects markdown flavor (MDX, GFM, standard, frontmatter) and extracts metadata with high confidence.
Readme
@bernierllc/markdown-detector
A pure TypeScript package for detecting markdown flavor (MDX, GFM, standard, frontmatter) and extracting metadata with high confidence. No rendering or React/remark/rehype dependencies.
Features
- 🔄 Automatic Detection: Detects markdown flavor based on content analysis
- 📝 Multi-Flavor Support: MDX, GFM, standard markdown, and frontmatter
- 🎯 High Confidence: Provides confidence scores and suggestions
- ⚡ Performance: Optimized for large content with efficient regex patterns
- 🛡️ Edge Case Handling: Robust detection with comprehensive test coverage
- 🔧 Configurable: Flexible for custom detection needs
- 🪶 Lightweight: No rendering or heavy dependencies
Installation
npm install @bernierllc/markdown-detectorQuick Start
Basic Usage
import { MarkdownDetector } from '@bernierllc/markdown-detector';
const content = `
# My Document
This is **bold** and *italic* text.
<MyComponent prop="value" />
`;
const result = MarkdownDetector.detect(content);
console.log(result.flavor); // 'mdx'
console.log(result.confidence); // 0.95
console.log(result.suggestions); // [ ... ]API Reference
MarkdownDetector.detect(content: string): DetectionResult
Detects the markdown flavor and extracts metadata.
Returns:
flavor: 'mdx' | 'gfm' | 'standard' | 'markdown-with-frontmatter'confidence: number (0.0 - 1.0)features: detailed feature analysissuggestions: helpful suggestions for processing
Types
export type MarkdownFlavor =
| 'auto'
| 'standard'
| 'gfm'
| 'mdx'
| 'markdown-with-frontmatter';
export interface DetectionResult {
flavor: MarkdownFlavor;
confidence: number;
features: {
hasFrontmatter: boolean;
hasJSX: boolean;
hasGFM: boolean;
hasCodeBlocks: boolean;
hasTables: boolean;
hasMath: boolean;
// ...more
};
suggestions: string[];
}Detection Features
- MDX: JSX tags, expressions, comments, fragments, import/export, hooks
- GFM: Tables, strikethrough, task lists, autolinks, emoji, mentions, issues
- Frontmatter: YAML at start, valid key-value pairs, arrays, nested objects
- Standard: Headers, emphasis, lists, links, images, code blocks
- Edge Cases: Handles code blocks, invalid frontmatter, HTML vs JSX, large content, unicode, special characters, conflicting features
Example: Detecting Uploaded Markdown
import { MarkdownDetector } from '@bernierllc/markdown-detector';
function handleUpload(content: string) {
const detection = MarkdownDetector.detect(content);
// Store detection.flavor and detection.confidence as metadata
// Route or process content accordingly
}Performance
- Regex optimization: Efficient patterns
- Early termination: Stops when confident
- Memory efficient: Line-by-line processing
Testing
npm test
npm run test:coverage- Unit tests: All detection methods
- Integration tests: Full detection pipeline
- Edge case tests: Boundary and error cases
- Performance tests: Large and complex content
License
This package is licensed under the Bernier LLC license. See LICENSE file for details.
