Skip to content

Conversation

@diunko
Copy link

@diunko diunko commented Aug 20, 2025

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.

Copy link
Contributor

Copilot AI left a 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_markdown function 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.

Comment on lines +400 to +402
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):
Copy link

Copilot AI Aug 23, 2025

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.

Suggested change
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"])
):

Copilot uses AI. Check for mistakes.
Comment on lines +98 to +108
# 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: ![alt text](data:image/jpeg;base64,...)
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)
Copy link

Copilot AI Aug 23, 2025

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.

Suggested change
# 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: ![alt text](data:image/jpeg;base64,...)
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)

Copilot uses AI. Check for mistakes.
@taylorwilsdon
Copy link
Owner

I prefer markdown to almost anything else, great idea. Will get it reviewed today, thanks @diunko

@taylorwilsdon
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants