-
Notifications
You must be signed in to change notification settings - Fork 249
Add markdown export tool for google docs #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new markdown export tool for Google Docs using the Drive API's native markdown export capability. This provides an alternative to the existing text-based document retrieval method by preserving formatting like headers, lists, and links.
- Implements
get_doc_markdownfunction that exports Google Docs to markdown format using Drive API - Adds image processing functionality to replace base64-encoded images with readable placeholders
- Includes the new tool in the core tool tiers configuration
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| gdocs/docs_tools.py | Adds markdown export function and base64 image replacement utility |
| core/tool_tiers.yaml | Registers the new get_doc_markdown tool in the core tier |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| except Exception as e: | ||
| # If Markdown export fails, provide helpful error message | ||
| if "Invalid mime type" in str(e) or "Export only supports" in str(e): |
Copilot
AI
Aug 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String matching on exception messages is fragile and could break if the API changes error message formats. Consider catching specific exception types from the Google API client library instead of relying on string content matching.
| except Exception as e: | |
| # If Markdown export fails, provide helpful error message | |
| if "Invalid mime type" in str(e) or "Export only supports" in str(e): | |
| except HttpError as e: | |
| # If Markdown export fails, provide helpful error message | |
| error_content = None | |
| try: | |
| import json | |
| error_content = json.loads(e.content.decode("utf-8")) | |
| except Exception: | |
| error_content = {} | |
| error_reason = "" | |
| if error_content and "error" in error_content: | |
| error_message = error_content["error"].get("message", "") | |
| error_reason = error_content["error"].get("status", "") | |
| else: | |
| error_message = str(e) | |
| # Check for invalid mime type or unsupported export | |
| if ( | |
| ("Invalid mime type" in error_message) | |
| or ("Export only supports" in error_message) | |
| or (error_reason in ["FAILED_PRECONDITION", "INVALID_ARGUMENT"]) | |
| ): |
| # Pattern for reference-style images: [image1]: <data:image/png;base64,...> | ||
| reference_pattern = r'\[([^\]]+)\]:\s*<data:image/([^;]+);base64,([^>]+)>' | ||
| markdown_content = re.sub(reference_pattern, replace_reference_image, markdown_content) | ||
|
|
||
| # Pattern for inline images:  | ||
| inline_pattern = r'!\[([^\]]*)\]\(data:image/([^;]+);base64,([^)]+)\)' | ||
| markdown_content = re.sub(inline_pattern, replace_inline_image, markdown_content) | ||
|
|
||
| # Also handle images without angle brackets (some variations) | ||
| reference_pattern_no_brackets = r'\[([^\]]+)\]:\s*data:image/([^;]+);base64,(\S+)' | ||
| markdown_content = re.sub(reference_pattern_no_brackets, replace_reference_image, markdown_content) |
Copilot
AI
Aug 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function performs three separate regex substitutions on the same content. For large documents, this could be inefficient. Consider combining the patterns into a single regex with alternation groups or using re.compile() to pre-compile the patterns if this function will be called frequently.
| # Pattern for reference-style images: [image1]: <data:image/png;base64,...> | |
| reference_pattern = r'\[([^\]]+)\]:\s*<data:image/([^;]+);base64,([^>]+)>' | |
| markdown_content = re.sub(reference_pattern, replace_reference_image, markdown_content) | |
| # Pattern for inline images:  | |
| inline_pattern = r'!\[([^\]]*)\]\(data:image/([^;]+);base64,([^)]+)\)' | |
| markdown_content = re.sub(inline_pattern, replace_inline_image, markdown_content) | |
| # Also handle images without angle brackets (some variations) | |
| reference_pattern_no_brackets = r'\[([^\]]+)\]:\s*data:image/([^;]+);base64,(\S+)' | |
| markdown_content = re.sub(reference_pattern_no_brackets, replace_reference_image, markdown_content) | |
| # Use pre-compiled patterns for efficiency | |
| markdown_content = _REFERENCE_PATTERN.sub(replace_reference_image, markdown_content) | |
| markdown_content = _INLINE_PATTERN.sub(replace_inline_image, markdown_content) | |
| markdown_content = _REFERENCE_PATTERN_NO_BRACKETS.sub(replace_reference_image, markdown_content) |
|
I prefer markdown to almost anything else, great idea. Will get it reviewed today, thanks @diunko |
|
Can you refactor that giant image swapping helper function out of docs tools (which is already way too big haha) into the helper file in the same directory? Will get it in! Thanks |
Hey! wdyt about having markdown export in gdocs?
Found it very useful for me, so thought I'd share it.
Uses Drive API's export_media with markdown mime type - different from get_doc_content which needs Docs API for parsing, so kept them separate. Also handles base64 images by replacing them with placeholders so they don't bloat the context.
Tested with a couple docs so far - basic formatting works fine but haven't tried anything super complex.