44
55import argparse
66from functools import partial
7- from typing import Mapping
7+ from typing import Any , Mapping
88
99from markdown_it import MarkdownIt
1010from mdformat .renderer import DEFAULT_RENDERERS , RenderContext , RenderTreeNode
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
4450def 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
121116def _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-
149139normalize_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