fix(parser): provide explicit file_extension for markitdown to resolve #544
This commit is contained in:
@@ -13,34 +13,34 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class StdMarkitdownParser(BaseParser):
|
class StdMarkitdownParser(BaseParser):
|
||||||
"""
|
"""
|
||||||
PDF Document Parser
|
Standard MarkItDown Parser Wrapper
|
||||||
|
|
||||||
This parser handles PDF documents by extracting text content.
|
This parser uses the markitdown library to convert various document formats
|
||||||
It uses the markitdown library for simple text extraction.
|
(docx, pptx, pdf, etc.) into text/markdown.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
# 这里的 super() 会调用 BaseParser 的初始化,确保 self.file_type 被正确赋值
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
self.markitdown = MarkItDown()
|
self.markitdown = MarkItDown()
|
||||||
|
|
||||||
def parse_into_text(self, content: bytes, file_extension: str = None) -> Document:
|
def parse_into_text(self, content: bytes) -> Document:
|
||||||
"""
|
"""
|
||||||
Modified to support explicit file_extension to fix Issue #544.
|
Parses content using MarkItDown.
|
||||||
If file_extension is not provided, we try to infer it or default to None.
|
Uses self.file_type (inherited from BaseParser) to hint the stream format.
|
||||||
"""
|
"""
|
||||||
try:
|
ext = self.file_type
|
||||||
# 核心修复点:传入 file_extension 参数
|
if ext and not ext.startswith('.'):
|
||||||
# 如果调用方没传,markitdown 可能会报错,这里我们至少保证它有尝试的机会
|
ext = '.' + ext
|
||||||
result = self.markitdown.convert(
|
|
||||||
io.BytesIO(content),
|
# 直接调用 convert,移除 try-catch,让异常由上层 PipelineParser 统一捕获
|
||||||
file_extension=file_extension,
|
result = self.markitdown.convert(
|
||||||
keep_data_uris=True
|
io.BytesIO(content),
|
||||||
)
|
file_extension=ext,
|
||||||
return Document(content=result.text_content)
|
keep_data_uris=True
|
||||||
except Exception as e:
|
)
|
||||||
logger.warning(f"Markitdown conversion failed: {e}. Fallback might be triggered.")
|
return Document(content=result.text_content)
|
||||||
# 必须抛出异常,这样外部的 FirstParser 才会切换到 DocxParser 这种备选方案
|
|
||||||
raise e
|
|
||||||
|
|
||||||
|
|
||||||
class MarkitdownParser(PipelineParser):
|
class MarkitdownParser(PipelineParser):
|
||||||
_parser_cls = (StdMarkitdownParser, MarkdownParser)
|
_parser_cls = (StdMarkitdownParser, MarkdownParser)
|
||||||
Reference in New Issue
Block a user