Skip to content

Expand Markdown support for Google Docs#40

Merged
a-bonus merged 3 commits intoa-bonus:mainfrom
cyberkni:main
Feb 4, 2026
Merged

Expand Markdown support for Google Docs#40
a-bonus merged 3 commits intoa-bonus:mainfrom
cyberkni:main

Conversation

@cyberkni
Copy link
Copy Markdown
Contributor

@cyberkni cyberkni commented Feb 4, 2026

I wanted to use Claude code to revise documents in Google Docs. Markdown was a reasonable interchange format to work with.

This pull request adds comprehensive markdown support to the Google Docs MCP server, enabling users to read, edit, and write documents using markdown syntax. It introduces two new tools for converting markdown to Google Docs format, updates documentation to reflect these capabilities, and enhances batch update handling for large document changes. The changes also include improvements to formatting functions and add required dependencies for markdown parsing.

Major Features:

Markdown Support & Tools:

  • Added two new tools: replaceDocumentWithMarkdown and appendMarkdownToGoogleDoc, allowing users to replace or append document content using markdown syntax with full formatting (headings, bold, italic, strikethrough, links, lists, etc.).
  • Implemented a markdown parser utility in src/markdownParser.ts for analyzing and parsing markdown content.
  • Added markdown-to-Google-Docs conversion logic and integrated it into the server workflow.

Batch Update Improvements:

  • Introduced executeBatchUpdateWithSplitting in src/googleDocsApiHelpers.ts to efficiently handle large sets of requests by splitting them into delete, insert, and format operations, improving reliability for bulk document updates.

Formatting & Styling Enhancements:

  • Updated text and paragraph style request builders to support the tabId parameter, enabling precise formatting in multi-tab documents. [1] [2] [3] [4]

cyberkni and others added 3 commits January 13, 2026 17:12
Enable users to edit Google Docs using familiar markdown syntax, allowing for:
- Offline editing in preferred text editors (VS Code, Vim, etc.)
- Version control of document content with Git
- Batch processing and automation via scripts
- Faster editing with markdown's concise syntax

This addresses a key workflow limitation: the existing text editing tools
(appendToGoogleDoc, insertText) only accept plain text, requiring separate
formatting operations. Users requested the ability to work with markdown as
an interchange format, enabling a "retrieve as markdown → edit → apply back"
workflow.

Added two new MCP tools (bringing total from 42 to 44):
- replaceDocumentWithMarkdown: Replace entire document with markdown content
- appendMarkdownToGoogleDoc: Append markdown content to document end

1. **markdown-it Parser** (src/markdownParser.ts)
   - Lightweight, fast markdown parser (~20KB vs 500KB alternatives)
   - Parses markdown into token stream for sequential processing
   - Helper functions for extracting heading levels and link URLs

2. **Conversion Engine** (src/markdownToGoogleDocs.ts)
   - Two-pass algorithm: insert text → apply formatting
   - Tracks formatting state through nested markdown (bold + italic + links)
   - Generates Google Docs API batch update requests
   - Supports: headings (H1-H6), bold, italic, strikethrough, links, lists

3. **Batch Splitting** (src/googleDocsApiHelpers.ts)
   - executeBatchUpdateWithSplitting handles Google's 50-request limit
   - Separates insert and format operations for correct execution order
   - Logs progress for large documents (100+ operations)

- Headings: # H1 through ###### H6 → HEADING_1 through HEADING_6
- Bold: **text** or __text__
- Italic: *text* or _text_
- Strikethrough: ~~text~~
- Links: [text](url)
- Lists: Bullet (-, *) and numbered (1., 2.)
- Nested formatting: ***bold italic***, **bold [link](url)**

Created comprehensive test suite (tests/markdown.test.js) with 17 tests covering:
- Basic formatting (bold, italic, strikethrough)
- Links and nested formatting
- Headings (H1-H6)
- Lists (bullet and numbered)
- Mixed content documents
- Index tracking and tab support
- Edge cases (empty input, plain text, whitespace)

All tests pass ✓

- CLAUDE.md: Added Markdown category, workflow guide, tool reference
- README.md: Added Markdown Support feature section
- SAMPLE_TASKS.md: Added Task a-bonus#16 demonstrating markdown workflow
…tWithMarkdown

Root cause: Google Docs API applies all requests in a batchUpdate against the
INITIAL document state, not sequentially. When delete and insert operations
were in the same batch, inserts used indices calculated for empty document
but were applied to the original document, causing corruption.

Solution: Execute delete operation in a completely separate batchUpdate call
before generating and executing markdown conversion requests.

Changes:
- Split replaceDocumentWithMarkdown into 3 API calls:
  1. Delete existing content
  2. Convert markdown (indices now correct for empty doc)
  3. Insert and format new content
- Add debug logging to track operation flow
- Update executeBatchUpdateWithSplitting with detailed logging
@a-bonus a-bonus merged commit f13059a into a-bonus:main Feb 4, 2026
@a-bonus
Copy link
Copy Markdown
Owner

a-bonus commented Feb 4, 2026

sick, thank you!

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