From 0d517eda389e8f4bda289c299a4f6e4fd5c81812 Mon Sep 17 00:00:00 2001 From: hemtilva Date: Mon, 16 Nov 2026 03:31:02 +0530 Subject: [PATCH] Avoid merged delimiters for nested emphasis and adjacent code spans - Don't re-wrap an inline element whose ancestor already uses the same markup, e.g. xy now gives **xy** instead of **x**y****. - Separate a code span from an immediately preceding code span with a space so CtrlC gives `Ctrl` `C` instead of `Ctrl``C`, which renders as a single code span. --- markdownify/__init__.py | 18 ++++++++++++++++++ tests/test_conversions.py | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 95eee9e..a55c241 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -107,13 +107,26 @@ def implementation(self, el, text, parent_tags): markup_suffix = markup_prefix if '_noformat' in parent_tags: return text + # If an ancestor is already wrapped in the same markup (e.g. inside + # ), don't wrap again: '**x**y****' does not render as intended. + if markup_prefix and markup_suffix == markup_prefix: + for tag in parent_tags: + parent_fn = getattr(self.get_conv_fn_cached(tag), 'markup_fn', None) + if parent_fn is not None and parent_fn(self) == markup_prefix: + return text prefix, suffix, text = chomp(text) if not text: return '' return '%s%s%s%s%s' % (prefix, markup_prefix, text, markup_suffix, suffix) + implementation.markup_fn = markup_fn return implementation +def _is_code_span(el): + """Return whether el is a tag that is converted to an inline code span.""" + return isinstance(el, Tag) and el.name in ('code', 'kbd', 'samp') + + def _todict(obj): return dict((k, getattr(obj, k)) for k in dir(obj) if not k.startswith('_')) @@ -499,6 +512,11 @@ def convert_code(self, el, text, parent_tags): if max_backticks > 0: text = " " + text + " " + # A code span directly following another one would merge with it + # ('`foo``bar`'), so separate them with a space. + if not prefix and _is_code_span(el.previous_sibling): + prefix = ' ' + return '%s%s%s%s%s' % (prefix, markup_delimiter, text, markup_delimiter, suffix) convert_del = abstract_inline_conversion(lambda self: '~~') diff --git a/tests/test_conversions.py b/tests/test_conversions.py index c95483c..09aa210 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -107,6 +107,10 @@ def test_code(): assert md('foo`bar`baz') == 'foo`` `bar` ``baz' assert md('foo``bar``baz') == 'foo``` ``bar`` ```baz' assert md('foo `bar` baz') == 'foo `` `bar` `` baz' + assert md('foobar') == '`foo` `bar`' + assert md('CtrlC') == '`Ctrl` `C`' + assert md('foo bar') == '`foo` `bar`' + assert md('a`bc') == '`` a`b `` `c`' def test_dl(): @@ -336,6 +340,15 @@ def test_strong(): assert md('Hello') == '**Hello**' +def test_nested_same_markup(): + assert md('foobar') == '**foobar**' + assert md('foo bar baz') == '**foo bar baz**' + assert md('foobar') == '*foobar*' + assert md('foobar') == '~~foobar~~' + assert md('foobarbaz') == '**foo*barbaz***' + assert md('foobar', sup_symbol='') == 'foobar' + + def test_strong_em_symbol(): assert md('Hello', strong_em_symbol=UNDERSCORE) == '__Hello__' assert md('Hello', strong_em_symbol=UNDERSCORE) == '__Hello__'