[BUG] Generated markdown missing required blank lines before code blocks and lists (CommonMark violation)
Describe the bug
When Claude Code generates or writes markdown files, it consistently omits required blank lines before:
- Code blocks (fenced with ```)
- Unordered lists (- items)
- Ordered lists (1. items)
This violates CommonMark spec and causes rendering issues in many markdown parsers/viewers. Users must manually fix every generated markdown file or consume tokens having Claude fix its own output.
Claude Code version: 2.1.4 (issue present since at least v)
OS: macOS (but this is model output behavior, not platform-specific)
---
The Problem
CommonMark spec requires a blank line before:
- Fenced code blocks
- List blocks (both ordered and unordered)
Without the blank line, many parsers treat these as continuation of the previous paragraph rather than new block elements.
---
Example: What Claude Generates
Here is an example:
- Item one
- Item two
And here is some code:
```bash
echo "hello"
## Example: What It Should Generate
```markdown
Here is an example:
- Item one
- Item two
And here is some code:
```bash
echo "hello"
---
## Impact
1. **Manual fixes required** - Every markdown file Claude writes needs manual editing
2. **Token waste** - Users prompt Claude to "fix the markdown formatting" repeatedly
3. **Broken rendering** - GitHub, VS Code preview, documentation sites may render incorrectly
4. **Consistency issues** - Same file may have some correct spacing (by chance) and some missing
---
## Reproduction
1. Ask Claude Code to write any markdown file with lists or code blocks
2. Observe missing blank lines before these elements
3. Validate with a CommonMark linter or observe rendering issues
**Example prompt:**
> "Create a README.md that explains how to install this project, with a prerequisites list and example commands"
**Result:** Lists and code blocks will typically be missing preceding blank lines.
---
## Frequency and Inconsistency
This is not occasional - it happens in nearly every markdown file Claude generates. In my experience over several months of daily Claude Code usage, I have to fix this in almost every markdown file.
**Critically, the behavior is inconsistent even within the same file:**
- Some lists have the blank line, others don't
- Some code blocks have the blank line, others don't
- No apparent pattern to which ones are correct vs. incorrect
This inconsistency makes it impossible to work around programmatically and forces manual review of every generated markdown file.
---
## Workaround
Users must either:
1. Manually add blank lines after Claude writes files
2. Prompt Claude to fix formatting: "Add blank lines before all lists and code blocks"
3. Add instructions to CLAUDE.md (which are inconsistently followed):
```markdown
## Markdown Formatting
Markdown lists MUST have a blank line before the first item.
Markdown code blocks MUST have a blank line before the opening fence.
Even with explicit instructions, compliance is inconsistent.
---
Suggested Fix
This appears to be a model output formatting issue. The model's markdown generation should be updated to:
- Always emit a blank line before fenced code blocks
- Always emit a blank line before list blocks
- Optionally: validate CommonMark compliance before writing markdown files
---
Related
This may be related to general markdown formatting issues:
- #4958 - Missing language tags, inconsistent line spacing
- #15289 - Markdown output issues with leading whitespace
However, this specific issue (missing blank lines before block elements) doesn't appear to be tracked separately.
This issue has 9 comments on GitHub. Read the full discussion on GitHub ↗