Skip to content

Commit 4770f20

Browse files
committed
fix: remove global variables
Based on: hukkin/mdformat-tables#22
1 parent 960e4b5 commit 4770f20

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

mdformat_mkdocs/_normalize_list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from contextlib import suppress
77
from enum import Enum
88
from itertools import starmap
9-
from typing import Callable, Literal, NamedTuple, TypeVar
9+
from typing import Any, Callable, Literal, Mapping, NamedTuple, TypeVar
1010

1111
from mdformat.renderer import RenderContext, RenderTreeNode
1212
from more_itertools import unzip, zip_equal
@@ -454,7 +454,7 @@ def normalize_list(
454454
text: str,
455455
node: RenderTreeNode,
456456
context: RenderContext,
457-
check_if_align_semantic_breaks_in_lists: Callable[[], bool], # Attach with partial
457+
check_if_align_semantic_breaks_in_lists: Callable[[Mapping[str, Any]], bool],
458458
) -> str:
459459
"""Format markdown list.
460460
@@ -473,6 +473,6 @@ def normalize_list(
473473
parsed_text = parse_text(
474474
text=text,
475475
inc_numbers=inc_numbers,
476-
use_sem_break=check_if_align_semantic_breaks_in_lists(),
476+
use_sem_break=check_if_align_semantic_breaks_in_lists(context.options),
477477
)
478478
return _join(new_lines=parsed_text.new_lines)

mdformat_mkdocs/plugin.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import argparse
66
from functools import partial
7-
from typing import Mapping
7+
from typing import Any, Mapping
88

99
from markdown_it import MarkdownIt
1010
from mdformat.renderer import DEFAULT_RENDERERS, RenderContext, RenderTreeNode
@@ -25,20 +25,26 @@
2525
pymd_abbreviations_plugin,
2626
)
2727

28-
_IGNORE_MISSING_REFERENCES = None
29-
"""user-specified flag to turn off bracket escaping when no link reference found.
28+
ContextOptions = Mapping[str, Any]
3029

31-
Addresses: https://github.com/KyleKing/mdformat-mkdocs/issues/19
3230

33-
"""
31+
def cli_is_ignore_missing_references(options: ContextOptions) -> bool:
32+
"""user-specified flag to turn off bracket escaping when no link reference found.
3433
35-
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = None
36-
"""user-specified flag for toggling semantic breaks.
34+
Addresses: https://github.com/KyleKing/mdformat-mkdocs/issues/19
3735
38-
- 3-spaces on subsequent lines in semantic numbered lists
39-
- and 2-spaces on subsequent bulleted items
36+
"""
37+
return options["mdformat"].get("ignore_missing_references", False)
38+
39+
40+
def cli_is_align_semantic_breaks_in_lists(options: ContextOptions) -> bool:
41+
"""user-specified flag for toggling semantic breaks.
4042
41-
"""
43+
- 3-spaces on subsequent lines in semantic numbered lists
44+
- and 2-spaces on subsequent bulleted items
45+
46+
"""
47+
return options["mdformat"].get("align_semantic_breaks_in_lists", False)
4248

4349

4450
def add_cli_options(parser: argparse.ArgumentParser) -> None:
@@ -62,18 +68,7 @@ def update_mdit(mdit: MarkdownIt) -> None:
6268
mdit.use(mkdocstrings_autorefs_plugin)
6369
mdit.use(pymd_abbreviations_plugin)
6470

65-
global _ALIGN_SEMANTIC_BREAKS_IN_LISTS # noqa: PLW0603
66-
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = mdit.options["mdformat"].get(
67-
"align_semantic_breaks_in_lists",
68-
False,
69-
)
70-
71-
global _IGNORE_MISSING_REFERENCES # noqa: PLW0603
72-
_IGNORE_MISSING_REFERENCES = mdit.options["mdformat"].get(
73-
"ignore_missing_references",
74-
False,
75-
)
76-
if _IGNORE_MISSING_REFERENCES:
71+
if cli_is_ignore_missing_references(mdit.options):
7772
mdit.use(mkdocstrings_crossreference_plugin)
7873

7974

@@ -120,7 +115,7 @@ def _render_with_default_renderer(
120115

121116
def _render_cross_reference(node: RenderTreeNode, context: RenderContext) -> str:
122117
"""Render a MkDocs crossreference link."""
123-
if _IGNORE_MISSING_REFERENCES:
118+
if cli_is_ignore_missing_references(context.options):
124119
return _render_meta_content(node, context)
125120
# Default to treating the matched content as a link
126121
return _render_with_default_renderer(node, context, "link")
@@ -141,14 +136,9 @@ def _render_cross_reference(node: RenderTreeNode, context: RenderContext) -> str
141136
}
142137

143138

144-
def check_if_align_semantic_breaks_in_lists() -> bool:
145-
"""Return value of global variable."""
146-
return _ALIGN_SEMANTIC_BREAKS_IN_LISTS or False
147-
148-
149139
normalize_list = partial(
150140
unbounded_normalize_list,
151-
check_if_align_semantic_breaks_in_lists=check_if_align_semantic_breaks_in_lists,
141+
check_if_align_semantic_breaks_in_lists=cli_is_align_semantic_breaks_in_lists,
152142
)
153143

154144
# A mapping from `RenderTreeNode.type` to a `Postprocess` that does

0 commit comments

Comments
 (0)