Project Files
src / documents / parsers / textParser.ts
/**
* Text and Markdown Parser
* Handles .txt and .md files with front-matter support
*/
import { readFile } from 'fs/promises';
import matter from 'gray-matter';
import type { ParsedDocument, ParserOptions } from '../../types';
export class TextParser {
/**
* Parse plain text files
*/
static async parse(
filePath: string,
options?: ParserOptions
): Promise<ParsedDocument> {
const content = await readFile(filePath, 'utf-8');
return {
content,
metadata: {
format: 'txt',
},
};
}
}
export class MarkdownParser {
/**
* Parse Markdown files with front-matter support
*/
static async parse(
filePath: string,
options?: ParserOptions
): Promise<ParsedDocument> {
const fileContent = await readFile(filePath, 'utf-8');
// Parse front-matter
const { data: frontMatter, content } = matter(fileContent);
return {
content,
metadata: {
format: 'md',
title: frontMatter.title,
...frontMatter,
},
};
}
}