from pathlib import Path
from typing import Optional
from markitdown import MarkItDown
def get_markdown_content(file_path: Path | str) -> Optional[str]:
"""
Extract content from a markdown file and return it as a string.
Args:
file_path (Path | str): Path to the markdown file to be processed
Returns:
Optional[str]: The extracted text content from the markdown file.
Returns None if file processing fails.
Raises:
FileNotFoundError: If the specified file does not exist
Exception: For other processing errors
"""
try:
# Convert string path to Path object if needed
file_path = Path(file_path) if isinstance(file_path, str) else file_path
# Verify file exists
if not file_path.exists():
raise FileNotFoundError(f"File not found: {file_path}")
# Initialize markdown converter with plugins disabled
markdown_converter = MarkItDown(enable_plugins=False)
# Convert markdown content
conversion_result = markdown_converter.convert(str(file_path))
return conversion_result.text_content
except FileNotFoundError as file_error:
return f"Error: {file_error}"
except Exception as error:
return f"Error processing markdown file: {error}"