Skip to content

Avoid merged delimiters for nested emphasis and adjacent code spans - #302

Open
hemtilva wants to merge 1 commit into
matthewwithanm:developfrom
hemtilva:fix-adjacent-inline-markup
Open

hemtilva wants to merge 1 commit into
matthewwithanm:developfrom
hemtilva:fix-adjacent-inline-markup

Conversation

@hemtilva

Copy link
Copy Markdown

Fixes #301

This fixes two cases where inline delimiters run together and the output renders wrongly.

Nested elements with the same markup. abstract_inline_conversion now returns the text unwrapped when an ancestor already uses the same markup. It finds that out from parent_tags: each generated converter stores its markup_fn, and the check compares the parent's markup with its own. So <b> inside <strong> and <em> inside <i> are both handled, as is a subclass that defines its own converter with abstract_inline_conversion. HTML-tag markup such as sup_symbol='<sup>' is left alone, since nesting it is valid.

<b>x<b>y</b></b>        before: **x**y****   after: **xy**
<i>a<em>b</em></i>      before: *a*b**       after: *ab*

Adjacent code spans. If a <code>, <kbd> or <samp> comes straight after another one, convert_code puts a space before it. Otherwise the two sets of backticks join up and the result renders as one code span. Markdown has no way to put two code spans directly next to each other, so a space seemed like the smallest change that keeps both spans.

<kbd>Ctrl</kbd><kbd>C</kbd>   before: `Ctrl``C`   after: `Ctrl` `C`

I checked the new outputs with markdown-it-py (CommonMark) and Python-Markdown. Both now give the expected <strong>/<em>/<code> elements.

Not included: sibling emphasis (<i>a</i><i>b</i> → *a**b*). A space would change the text, so I'd rather agree on an approach first (see the issue).

Tests: I added cases to test_code and a new test_nested_same_markup in tests/test_conversions.py. pytest (84 passed), flake8 --ignore=E501,W503 markdownify tests and restructuredtext-lint README.rst all pass.

- Don't re-wrap an inline element whose ancestor already uses the same
  markup, e.g. <b>x<b>y</b></b> now gives **xy** instead of **x**y****.
- Separate a code span from an immediately preceding code span with a
  space so <kbd>Ctrl</kbd><kbd>C</kbd> gives `Ctrl` `C` instead of
  `Ctrl``C`, which renders as a single code span.
Copilot AI balanced review requested due to automatic review settings November 15, 2026 22:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

Adjacent or nested inline elements produce merged delimiters

2 participants