Skip to main content

Fix Codeblocks Command

Automatically adds or removes code block flags (expandable, lines, wrap) in MDX documentation files.

Usage​

writechoice fix codeblocks [options]

Options​

OptionAliasDescriptionDefault
--file <path>-fFix a single MDX file-
--dir <path>-dFix MDX files in a specific directory-
--threshold <number>-tLine count threshold for expandable15
--no-expandable-Skip expandable threshold processing-
--lines-Add lines to all code blocks that lack it-
--remove-lines-Remove lines from all code blocks that have it-
--wrap-Add wrap to all code blocks that lack it-
--remove-wrap-Remove wrap from all code blocks that have it-
--dry-run-Preview changes without writing filesfalse
--quiet-Suppress terminal outputfalse

How It Works​

Scans MDX files for fenced code blocks and modifies their info strings (the text after the opening triple backtick). Each flag is handled independently.

expandable​

Controlled by --threshold (default: 15). Automatically adds or removes expandable based on line count:

  • Has expandable and lines < threshold → remove it
  • Lacks expandable and lines > threshold → add it
  • Use --no-expandable to skip this flag entirely

lines​

Shows line numbers in the rendered code block.

  • --lines — adds lines to every block that lacks it
  • --remove-lines — removes lines from every block that has it

wrap​

Enables word wrapping in the rendered code block.

  • --wrap — adds wrap to every block that lacks it
  • --remove-wrap — removes wrap from every block that has it

Examples​

# Preview changes without writing
writechoice fix codeblocks --dry-run

# Add lines and wrap to all code blocks
writechoice fix codeblocks --lines --wrap

# Use a custom expandable threshold
writechoice fix codeblocks --threshold 20

# Remove lines from all code blocks
writechoice fix codeblocks --remove-lines

# Fix only a specific directory
writechoice fix codeblocks -d docs/api --lines

# Skip expandable and only manage lines
writechoice fix codeblocks --no-expandable --lines

Config File​

{
"codeblocks": {
"threshold": 20,
"lines": "add",
"wrap": null,
"expandable": true
}
}
FieldTypeDescriptionDefault
thresholdnumberLine count threshold for expandable15
expandablebooleanEnable expandable threshold processingtrue
lines"add" | "remove" | nullManage lines flagnull
wrap"add" | "remove" | nullManage wrap flagnull

CLI flags always take precedence over config values.

Safety​

  • Only modifies code block info strings — never the code content
  • Idempotent: running twice produces the same result
  • Use --dry-run to preview before writing
  • Revert with git checkout . if needed