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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ commands = [
description = "Measure module import times. Tox sends mdformat output to stderr, so to filter use e.g. `tox -e benchmark-import 2> >(grep mdformat)`."
deps = []
commands = [
["python", "-X", "importtime", "-m", "mdformat"],
["python", "-X", "importtime", "-m", "mdformat", "--version"],
]


Expand Down
4 changes: 3 additions & 1 deletion src/mdformat/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from mdformat._conf import DEFAULT_OPTS
from mdformat._util import EMPTY_MAP, NULL_CTX, build_mdit, detect_newline_type
from mdformat.renderer import MDRenderer


def text(
Expand All @@ -21,6 +20,9 @@ def text(
_filename: str = "",
) -> str:
"""Format a Markdown string."""
# Lazy import to improve module import time
from mdformat.renderer import MDRenderer

with _first_pass_contextmanager:
mdit = build_mdit(
MDRenderer,
Expand Down
6 changes: 4 additions & 2 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from mdformat._conf import DEFAULT_OPTS, InvalidConfError, read_toml_opts
from mdformat._util import detect_newline_type, is_md_equal
import mdformat.plugins
import mdformat.renderer


class RendererWarningPrinter(logging.Handler):
Expand Down Expand Up @@ -127,13 +126,16 @@ def run(cli_args: Sequence[str], cache_toml: bool = True) -> int: # noqa: C901
path_str = "-"
original_str = sys.stdin.read()

# Lazy import to improve module import time
from mdformat.renderer import LOGGER as RENDERER_LOGGER

formatted_str = mdformat.text(
original_str,
options=opts,
extensions=enabled_parserplugins,
codeformatters=enabled_codeformatters,
_first_pass_contextmanager=log_handler_applied(
mdformat.renderer.LOGGER, renderer_warning_printer
RENDERER_LOGGER, renderer_warning_printer
),
_filename=path_str,
)
Expand Down
14 changes: 10 additions & 4 deletions src/mdformat/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from contextlib import nullcontext
import re
from types import MappingProxyType
from typing import Any, Literal

from markdown_it import MarkdownIt
from markdown_it.renderer import RendererHTML
from typing import TYPE_CHECKING, Any, Literal

import mdformat.plugins

if TYPE_CHECKING:
from markdown_it import MarkdownIt

NULL_CTX = nullcontext()
EMPTY_MAP: MappingProxyType = MappingProxyType({})

Expand All @@ -26,6 +26,9 @@ def build_mdit(
extensions: Iterable[str] = (),
codeformatters: Iterable[str] = (),
) -> MarkdownIt:
# Lazy import to improve module import time
from markdown_it import MarkdownIt

mdit = MarkdownIt(renderer_cls=renderer_cls)
mdit.options["mdformat"] = mdformat_opts
# store reference labels in link/image tokens
Expand Down Expand Up @@ -69,6 +72,9 @@ def is_md_equal(
not a perfect solution, as there can be meaningful whitespace in
HTML, e.g. in a <code> block.
"""
# Lazy import to improve module import time
from markdown_it.renderer import RendererHTML

html_texts = {}
mdit = build_mdit(RendererHTML, mdformat_opts=options, extensions=extensions)
for key, text in [("md1", md1), ("md2", md2)]:
Expand Down