Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions markdown_it/rules_block/html_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
]


def html_block_starts(state: StateBlock, line: int) -> bool:
"""Whether `line` begins an HTML block of any type (issue #434)."""
pos = state.bMarks[line] + state.tShift[line]
maximum = state.eMarks[line]
if pos >= maximum or state.src[pos] != "<":
return False
lineText = state.src[pos:maximum]
return any(sequence[0].search(lineText) for sequence in HTML_SEQUENCES)


def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
LOGGER.debug(
"entering html_block: %s, %s, %s, %s", state, startLine, endLine, silent
Expand Down
11 changes: 11 additions & 0 deletions markdown_it/rules_block/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

from ..common.utils import mdTrim
from .html_block import html_block_starts
from .state_block import StateBlock

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -36,6 +37,16 @@ def paragraph(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
nextLine += 1
continue

# An HTML block start on a line under-indented for the current
# container is not a lazy continuation: like cmark, end the paragraph
# here so the line can open a block in an outer container (issue
# #434). At the same container level a type-7 sequence still does not
# interrupt the paragraph, which the terminator rules below handle.
if state.sCount[nextLine] < state.blkIndent and html_block_starts(
state, nextLine
):
break

# Some tags can terminate paragraph without empty line.
terminate = False
for terminatorRule in terminatorRules:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_html_block_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Regression tests for HTML blocks interacting with list items (#434)."""

import pytest

from markdown_it import MarkdownIt


@pytest.fixture
def md() -> MarkdownIt:
return MarkdownIt("commonmark", {"html": True})


def test_html_block_under_indented_closes_list(md: MarkdownIt) -> None:
"""A type-7 HTML block at outer indentation ends the list, like cmark."""
assert md.render("## Opts\n\n- a\n<br>\n## Next\n") == (
"<h2>Opts</h2>\n<ul>\n<li>a</li>\n</ul>\n<br>\n## Next\n"
)


def test_html_block_at_item_indent_is_lazy_continuation(md: MarkdownIt) -> None:
"""At the item's content indent, type 7 cannot interrupt the paragraph."""
assert md.render("- a\n <br>\n") == "<ul>\n<li>a\n<br></li>\n</ul>\n"


def test_html_block_cannot_interrupt_top_level_paragraph(md: MarkdownIt) -> None:
assert md.render("para\n<br>\n") == "<p>para\n<br></p>\n"


def test_plain_text_is_still_lazy_continuation(md: MarkdownIt) -> None:
assert md.render("- a\nb\n") == "<ul>\n<li>a\nb</li>\n</ul>\n"


def test_type_6_html_block_closes_list(md: MarkdownIt) -> None:
assert md.render("- a\n<div>\n") == "<ul>\n<li>a</li>\n</ul>\n<div>\n"