Repository navigation
Add support for preserving base64 encoded images - #1140
Conversation
|
Hey this looks great. Thanks for the contribution. Would you be able to add some test cases to verify this behavior in the CI. Once we have reasonable test coverage, I think it's good to merge. |
| _markdownify = _CustomMarkdownify( | ||
| keep_data_uris=kwargs.get("keep_data_uris", False) | ||
| ) |
There was a problem hiding this comment.
| _markdownify = _CustomMarkdownify( | |
| keep_data_uris=kwargs.get("keep_data_uris", False) | |
| ) | |
| _markdownify = _CustomMarkdownify( | |
| **kwargs | |
| ) |
By this way, we lay a "foundation" so that we can easily add new options to _CustomMarkdownify in the future without changing too much in upstream objects
|
|
||
| def __init__(self, **options: Any): | ||
| options["heading_style"] = options.get("heading_style", markdownify.ATX) | ||
| self.keep_data_uris = options.pop("keep_data_uris", False) |
There was a problem hiding this comment.
likewise, we can
| self.keep_data_uris = options.pop("keep_data_uris", False) | |
| options["keep_data_uris"] = options.get("keep_data_uris", False) |
by this way, we have:
- Default value in case the option is not supplied in upstream objects
- Keep the implementation consistent
What do you think ?
There was a problem hiding this comment.
Absolutely! 🫡
There was a problem hiding this comment.
I have modified it in the form of kwargs passing parameters. For the sake of uniformity, I used the kwargs passing parameter form in all places where _CustomMarkdownify is initialized, and also reserved adjustment space for adding more optional parameters later.
There was a problem hiding this comment.
Since the command line parameter parser must declare parameters before parsing, kwargs cannot be used, so main.py still uses a fixed actual parameter passing method.
Add module cli vector tests
I have added tests for the cli and module calls. I mainly focus on office files such as word and powerpoint. Since Excel uses the pandas library to read, it does not support reading any images, so it is not possible, and I do not plan to add tests to retain images. |
|
Ok, there are some formatting issues, and I want to make one small change to how the tests are organized. Let me do that really quickly. Stay tuned. |
|
Yuzhong Zhang (@BetterAndBetterII) I fixed some formatting issues, and reorganized tests a little. If this looks good to you, let me know and I'll merge it. |
great! |
|
quick question here, is the support for |
I think the tool is predominantly built for |
That is correct. Images in office files are their own entities and are easy to extract. PDFs, on the other hand, make things much hard (which is typically the case). I have it on my to-do list to integrate or provide options to use much much better PDF parsers than what we currently ship (and indeed Azure Document Intelligence will already yield better results for PDFs). But until then, we basically are stuck with the text layer of the PDFs. |
Issue Description
Currently, the markitdown library automatically truncates base64-encoded image data when converting HTML, DOCX, and PPTX files. This behavior may not be ideal for certain use cases, especially when users want to preserve complete image data in the converted Markdown documents.
Related:
#51
#58
#162
#246
#1102
#1139
Solution
Add a new parameter
keep_data_uristhat allows users to control whether base64-encoded image data is preserved during the conversion process.Changes Made
Modified
_CustomMarkdownifyclass (in_markdownify.py):keep_data_uris(default:False) to control whether to preserve base64 data URIsconvert_imgmethod to respect this parameter when processing image sourcesUpdated
HtmlConverterclass (in_html_converter.py):convertmethod to accept and pass through thekeep_data_urisparameter fromkwargs_CustomMarkdownifyconstructorUpdated
DocxConverterclass (in_docx_converter.py):convertmethod to extract and pass thekeep_data_urisparameter to the HTML converterUpdated
PptxConverterclass (in_pptx_converter.py):keep_data_urisparameterkeep_data_uris=True, encode image blobs as base64 and include them directly in the Markdownkeep_data_uris=False(default), continue using placeholder filenamesUpdated auxiliary converters:
BingSerpConverter: Added parameter forwarding forkeep_data_urisRssConverter: Added__init__method to storekwargsand modified_parse_contentto use the parameterWikipediaConverter: Added parameter forwarding forkeep_data_urisVerified
YouTubeConverter:_CustomMarkdownify, it does not actually use it for conversionBenefits
Usage Example
This enhancement ensures a consistent approach to handling base64 image data across all converters in the markitdown library, providing flexibility while maintaining backward compatibility.