Use uri_export_strategy with CustomUriExportStrategy and a Python callable to rewrite resource URIs in the Markdown output. This is useful when images are served from a CDN or a different path than where they’re saved.
Note
URI rewriting works with file-based image strategies (ExportImagesToFileSystemStrategy or CustomImagesStrategy). It has no effect when images are embedded as Base64.
Prepend a CDN base URL
fromgroupdocs.markdownimport(MarkdownConverter,ConvertOptions,ExportImagesToFileSystemStrategy,CustomUriExportStrategy)defuri_rewriting_cdn():"""Rewrite image URIs to point to a CDN base URL in the Markdown output."""# Step 1: Define the CDN base URLCDN_BASE="https://cdn.example.com/assets/"# Step 2: Create a handler that prepends the CDN URL to each resourcedefcdn_handler(call_info):"""Called for each resource URI during conversion.
Receives context with ResourceFileName, ResourceFileUri.
Returns dict with resource_file_uri to override the URI."""ctx=call_info["context"]original=ctx["ResourceFileName"]return{"resource_file_uri":CDN_BASE+original}# Step 3: Configure image and URI export strategiesoptions=ConvertOptions()options.image_export_strategy=ExportImagesToFileSystemStrategy("./output/images")options.uri_export_strategy=CustomUriExportStrategy(cdn_handler)# Step 4: Convert and save using keyword argument for optionsMarkdownConverter.to_file("business-plan.docx","output/report.md",convert_options=options)# Images saved locally to output/images/# Markdown references: if__name__=="__main__":importosos.makedirs("output/images",exist_ok=True)uri_rewriting_cdn()
business-plan.docx is sample file used in this example. Click here to download it.