Skip to content

fix: set prev token in render_list_item and add block_text to ignore_blocks#456

Merged
lepture merged 2 commits into
lepture:mainfrom
gaoflow:fix/rst-list-blockquote-prev-keyerror
Jun 26, 2026
Merged

fix: set prev token in render_list_item and add block_text to ignore_blocks#456
lepture merged 2 commits into
lepture:mainfrom
gaoflow:fix/rst-list-blockquote-prev-keyerror

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

When a blockquote appears inside a list item, the RST renderer's block_quote method accesses token.prev to decide whether to emit a .. separator. However, render_list_item called renderer.render_token directly without tracking prev across child tokens — unlike RSTRenderer.iter_tokens which sets prev for top-level tokens.

This meant blockquote tokens inside list items never received a prev reference. In older versions this could cause a KeyError; in the current version it silently degrades the output with missing or incorrect .. separators between blocks inside list items.

Changes:

  • Track prev in render_list_item (src/mistune/renderers/_list.py), mirroring how RSTRenderer.iter_tokens sets prev for top-level tokens
  • Add block_text to the ignore_blocks tuple in RSTRenderer.block_quote (src/mistune/renderers/rst.py), since block_text is the list-internal equivalent of paragraph and should be ignored when deciding whether to emit .. separators

Fixes: KeyError('prev') when rendering markdown containing blockquotes inside list items with the RST renderer

…blocks

The RSTRenderer.block_quote method accesses token.prev to decide
whether to emit a .. separator before the blockquote.  When a
blockquote appears inside a list item, render_list_item called
renderer.render_token directly without tracking prev, so
blockquote tokens never received a prev reference.  This caused a
KeyError in older versions and silently degraded output in the
current version (missing or incorrect .. separators).

- Track prev in render_list_item, mirroring how RSTRenderer.iter_tokens
  already does for top-level tokens.
- Add block_text (the list-internal paragraph token type) to the
  ignore_blocks tuple so a blockquote following list-internal text
  does not get a spurious .. separator.
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.04%. Comparing base (90b1a73) to head (def8a6d).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #456      +/-   ##
==========================================
+ Coverage   91.01%   91.04%   +0.03%     
==========================================
  Files          34       34              
  Lines        3395     3407      +12     
  Branches      670      672       +2     
==========================================
+ Hits         3090     3102      +12     
  Misses        184      184              
  Partials      121      121              
Flag Coverage Δ
unittests 91.01% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lepture

lepture commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Hi @gaoflow thanks for the patch. Can you add a test for this case?

@gaoflow

gaoflow commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Added in def8a6d.

The fixture now covers a list item containing normal item text followed by two separate block quotes. That case exercises both pieces of the fix: no separator before the first quote after block_text, and a .. separator before the second quote after a previous block_quote.

Local verification:

uv run --group dev python -m pytest tests/test_renderers.py -q
uv run --group dev python -m pytest -q
uv run --group dev ruff check src/mistune/renderers/_list.py src/mistune/renderers/rst.py tests/test_renderers.py
git diff --check

@lepture lepture merged commit 0799e19 into lepture:main Jun 26, 2026
27 checks passed
@lepture

lepture commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Thanks.

@sonarqubecloud

Copy link
Copy Markdown

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