Skip to content
Merged
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
1 change: 0 additions & 1 deletion mdformat_mkdocs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# ruff: noqa: RUF067
"""An mdformat plugin for `mkdocs`."""

__version__ = "5.2.0b2"
Expand Down
67 changes: 56 additions & 11 deletions mdformat_mkdocs/_normalize_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
rstrip_result,
separate_indent,
)
from .mdit_plugins import MATERIAL_ADMON_MARKERS, MATERIAL_CONTENT_TAB_MARKERS
from .mdit_plugins import (
INJECTION_PATTERN,
MATERIAL_ADMON_MARKERS,
MATERIAL_CONTENT_TAB_MARKERS,
)

if TYPE_CHECKING:
from collections.abc import Mapping
Expand Down Expand Up @@ -71,6 +75,7 @@ class Syntax(Enum):
START_MARKED = "START_MARKED"
EDGE_CODE = "EDGE_CODE"
HTML = "HTML"
INJECTION = "INJECTION"

@classmethod
def from_content(cls, content: str) -> Syntax | None:
Expand All @@ -85,13 +90,17 @@ def from_content(cls, content: str) -> Syntax | None:
if match["item"].startswith("```"):
return cls.CODE_NUMBERED if is_numbered else cls.CODE_BULLETED
return cls.LIST_NUMBERED if is_numbered else cls.LIST_BULLETED

result = None
if any(content.startswith(f"{marker} ") for marker in MARKERS):
return cls.START_MARKED
if content.startswith("```"):
return cls.EDGE_CODE
if content.startswith("<"):
return cls.HTML
return None
result = cls.START_MARKED
elif content.startswith("```"):
result = cls.EDGE_CODE
elif content.startswith("<"):
result = cls.HTML
elif INJECTION_PATTERN.match(content):
result = cls.INJECTION
return result


SYNTAX_CODE_LIST = {Syntax.CODE_BULLETED, Syntax.CODE_NUMBERED}
Expand Down Expand Up @@ -254,6 +263,32 @@ def _parse_html_line(last: BlockIndent | None, line: LineResult) -> BlockIndent
return result


def _parse_injection_block(
last: BlockIndent | None,
line: LineResult,
) -> BlockIndent | None:
"""Identify mkdocstrings injection sections."""
if last is not None:
if line.parsed.content and len(line.parsed.indent) <= len(last.raw_indent):
if line.parsed.syntax == Syntax.INJECTION:
return BlockIndent(
start_line=line.parsed.line_num,
raw_indent=line.parsed.indent,
indent_depth=len(line.parents),
kind="code",
)
return None
return last
if line.parsed.syntax == Syntax.INJECTION:
return BlockIndent(
start_line=line.parsed.line_num,
raw_indent=line.parsed.indent,
indent_depth=len(line.parents),
kind="code",
)
return None


# ======================================================================================
# Semantic Indent Handling

Expand Down Expand Up @@ -420,15 +455,25 @@ def parse_text(
indent if (indent and code_indents[indent.start_line] is None) else None
for indent in map_lookback(_parse_html_line, lines, None)
]
# When both, code_indents take precedence
injection_indents = [
# Any indents initiated from within a `code_block_indents` should be ignored
indent if (indent and code_indents[indent.start_line] is None) else None
for indent in map_lookback(_parse_injection_block, lines, None)
]
# When multiple match, code_indents take precedence, then html_indents
block_indents = [
c_ or h_ for c_, h_ in zip(code_indents, html_indents, strict=True)
c_ or h_ or i_
for c_, h_, i_ in zip(code_indents, html_indents, injection_indents, strict=True)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
]
new_indents = [*starmap(_format_new_indent, zip(lines, block_indents, strict=True))]

new_contents = [
_format_new_content(line, inc_numbers, ci is not None)
for line, ci in zip(lines, code_indents, strict=True)
_format_new_content(
line,
inc_numbers,
block_indent is not None and block_indent.kind == "code",
)
for line, block_indent in zip(lines, block_indents, strict=True)
]

if use_sem_break:
Expand Down
2 changes: 2 additions & 0 deletions mdformat_mkdocs/mdit_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
mkdocstrings_crossreference_plugin,
)
from ._mkdocstrings_injection import (
INJECTION_PATTERN,
MKDOCSTRINGS_INJECTION_PREFIX,
mkdocstrings_injection_plugin,
)
Expand All @@ -48,6 +49,7 @@
"DOLLARMATH_BLOCK",
"DOLLARMATH_BLOCK_LABEL",
"DOLLARMATH_INLINE",
"INJECTION_PATTERN",
"MATERIAL_ADMON_MARKERS",
"MATERIAL_CONTENT_TAB_MARKERS",
"MKDOCSTRINGS_AUTOREFS_PREFIX",
Expand Down
1 change: 0 additions & 1 deletion mdformat_mkdocs/mdit_plugins/_material_deflist.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from mdit_py_plugins.deflist import deflist_plugin

if TYPE_CHECKING:
from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Render

Expand Down
12 changes: 8 additions & 4 deletions mdformat_mkdocs/mdit_plugins/_mkdocstrings_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from markdown_it import MarkdownIt
from markdown_it.rules_block import StateBlock

_INJECTION_PATTERN = re.compile(r"^:::\s+\S")
INJECTION_PATTERN = re.compile(r"^:::\s+\S")
MKDOCSTRINGS_INJECTION_PREFIX = "mkdocstrings_injection"


Expand All @@ -40,9 +40,9 @@ def _mkdocstrings_injection(
if is_code_block(state, start_line):
return False

base_indent = state.blkIndent
base_indent = state.tShift[start_line]
header = _get_line(state, start_line, base_indent)
if not _INJECTION_PATTERN.match(header):
if not INJECTION_PATTERN.match(header):
return False

if silent:
Expand All @@ -51,11 +51,15 @@ def _mkdocstrings_injection(
lines = [header]
next_line = start_line + 1
while next_line < end_line:
if state.tShift[next_line] <= base_indent:
if not state.isEmpty(next_line) and state.tShift[next_line] <= base_indent:
break
lines.append(_get_line(state, next_line, base_indent))
next_line += 1

while len(lines) > 1 and not lines[-1].strip():
lines.pop()
next_line -= 1

token = state.push(MKDOCSTRINGS_INJECTION_PREFIX, "", 0)
token.content = "\n".join(lines)
token.block = True
Expand Down
1 change: 0 additions & 1 deletion mdformat_mkdocs/mdit_plugins/_python_markdown_attr_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from mdformat_mkdocs._synced.admon_factories import new_token

if TYPE_CHECKING:
from markdown_it import MarkdownIt
from markdown_it.rules_inline import StateInline

_ATTR_LIST_PATTERN = re.compile(r"{:? (?P<attrs>[^}]+) }")
Expand Down
Loading
Loading