Skip to content
Merged
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
21 changes: 12 additions & 9 deletions mdformat_pyproject/plugin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
"""Main plugin module."""

import pathlib
from __future__ import annotations

import sys
from functools import cache
from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Tuple, Union
from pathlib import Path

import markdown_it
import mdformat

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Sequence

from mdformat.renderer.typing import Render

_ConfigOptions = dict[str, int | str | Sequence[str]]

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


_ConfigOptions = MutableMapping[str, Union[int, str, Sequence[str]]]


@cache
def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Path]:
def _find_pyproject_toml_path(search_path: Path) -> Path | None:
"""Find the pyproject.toml file that applies to the search path.

The search is done ascending through the folders tree until a pyproject.toml
Expand All @@ -39,7 +42,7 @@ def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Pat


@cache
def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]:
def _parse_pyproject(pyproject_path: Path) -> _ConfigOptions | None:
"""Extract and validate the mdformat options from the pyproject.toml file.

The options are searched inside a [tool.mdformat] key within the toml file,
Expand All @@ -57,7 +60,7 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]:


@cache
def read_toml_opts(conf_dir: pathlib.Path) -> Tuple[MutableMapping, Optional[pathlib.Path]]:
def read_toml_opts(conf_dir: Path) -> tuple[dict, Path | None]:
"""Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml.

Notice that if `.mdformat.toml` exists it is ignored.
Expand All @@ -76,7 +79,7 @@ def update_mdit(mdit: markdown_it.MarkdownIt) -> None:
pass


RENDERERS: MutableMapping[str, "Render"] = {}
RENDERERS: dict[str, Render] = {}

# Monkey patch mdformat._conf to use our own read_toml_opts version
mdformat._conf.read_toml_opts = read_toml_opts