Skip to content

Commit a2f098a

Browse files
Preserve admonition fence nesting
1 parent 7f3b7e9 commit a2f098a

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/pydoc_markdown/contrib/processors/google.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,27 @@ def _format_section(self, keyword: str, raw_lines: t.List[str]) -> t.List[str]:
202202
codeblock_indent = min(max(section_indent - preserve_indent, 0), self._get_indentation(raw_line))
203203
rebased = self._remove_indentation(raw_line, codeblock_indent).rstrip()
204204
rebased_indent = self._get_indentation(rebased)
205-
list_content_indent: t.Optional[int] = None
205+
container_content_indent: t.Optional[int] = None
206206
for previous_index in range(len(result) - 1, -1, -1):
207207
previous = result[previous_index]
208208
if not previous.strip():
209209
continue
210210
previous_indent = self._get_indentation(previous)
211211
if previous_indent > rebased_indent:
212212
continue
213+
is_nested = self._get_indentation(raw_line) > self._get_indentation(raw_lines[previous_index])
213214
list_match = re.match(r"^\s*(?:[-+*]|\d{1,9}[.)])(?:[ \t]+|$)", previous)
214-
if list_match and self._get_indentation(raw_line) > self._get_indentation(
215-
raw_lines[previous_index]
216-
):
217-
list_content_indent = len(list_match.group().expandtabs(4))
215+
if list_match and is_nested:
216+
container_content_indent = len(list_match.group().expandtabs(4))
218217
if list_match.end() == len(previous):
219-
list_content_indent += 1
218+
container_content_indent += 1
219+
admonition_match = re.match(r"^\s*(?:!!!|\?{3}\+?)(?:[ \t]+|$)", previous)
220+
if admonition_match and is_nested:
221+
container_content_indent = previous_indent + 4
220222
break
221223
codeblock_prefix = (
222-
" " * max(list_content_indent - rebased_indent, 0)
223-
if list_content_indent is not None and rebased_indent
224+
" " * max(container_content_indent - rebased_indent, 0)
225+
if container_content_indent is not None and rebased_indent
224226
else ""
225227
)
226228
result.append(codeblock_prefix + rebased)

test/processors/test_google.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,11 @@ def test_google_processor_keeps_blockquoted_fence_sibling_to_list():
331331
"Examples:\n - Plain item\n > ```python\n > print('quoted')\n > ```",
332332
"**Examples**:\n\n - Plain item\n > ```python\n > print('quoted')\n > ```",
333333
)
334+
335+
336+
def test_google_processor_preserves_admonition_fence_nesting():
337+
assert_processor_result(
338+
GoogleProcessor(),
339+
"Examples:\n !!! note\n ```python\n print('nested')\n ```",
340+
"**Examples**:\n\n !!! note\n ```python\n print('nested')\n ```",
341+
)

0 commit comments

Comments
 (0)