Skip to content

fix(code-block): keep indentation when copying highlighted code - #271

Open
JeetuSK0808 wants to merge 1 commit into
mintlify:mainfrom
JeetuSK0808:fix/copy-keeps-indentation
Open

JeetuSK0808 wants to merge 1 commit into
mintlify:mainfrom
JeetuSK0808:fix/copy-keeps-indentation

Conversation

@JeetuSK0808

@JeetuSK0808 JeetuSK0808 commented Nov 11, 2026 •

Copy link
Copy Markdown

Summary

Since #245, getCodeString in packages/components/src/utils/shiki/lib.ts runs dedent() on the copy text of every code block, including shiki-highlighted ones. The dedent was meant for raw string children inside JSX/MDX, which pick up indentation from the surrounding markup. Highlighted children already hold the code exactly as it is shown, so the copy button strips real indentation from them. The block renders correctly, but what gets copied is broken:

  • def handler(event): followed by an indented return {"ok": True} copies with the return at column 0. A lone Python function or class becomes an IndentationError.
  • A YAML snippet with a single top-level key and indented children loads as a different structure.

This change only dedents raw string children (isShiki ? codeString : dedent(codeString)). The cases from #226 that #245 fixed still work.

Test Plan

  • Ran the real getCodeString on highlighted children built with shiki 3.21 for 2,000 generated snippets (Python, YAML and others), comparing the copied text with what is displayed. Python was checked with ast.parse and YAML with js-yaml.
    • Before: the copied text differs from the displayed code in 1,218 of 2,000 snippets, and the copy is actually broken in 851.
    • After: 0 and 0.
  • Re-checked the CodeBlock mis-indents string children in MDX #226 string-children cases: still dedented as before.
  • pnpm lint (biome) and pnpm build pass.

🤖 Generated with Claude Code


Note

Low Risk
Narrow change to copy-text extraction in getCodeString; display and non-Shiki dedent behavior are unchanged.

Overview
Copying from highlighted code blocks again preserves the indentation shown on screen. getCodeString still returns early for Shiki-rendered blocks in the normal path, but when copy forces extraction (forceExtract: true) it no longer runs dedent() on that text—only on raw string MDX/JSX children that pick up markup padding.

Raw string blocks keep the #245 behavior so pasted snippets stay left-aligned without stray leading spaces.

Reviewed by Cursor Bugbot for commit 86f4024. Bugbot is set up for automated code reviews on this repo. Configure here.

Since mintlify#245, getCodeString runs dedent() on the copy text of every code
block, including shiki-highlighted ones. Highlighted children already hold
the code exactly as shown, so the copy button stripped real indentation:
a lone Python function or a single top-level YAML key copied as broken
code. Only dedent raw string children, which is what mintlify#245 was for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant