Skip to content

Commit f410ed3

Browse files
committed
feat: use the new plugin options group
1 parent 4523888 commit f410ed3

File tree

7 files changed

+64
-31
lines changed

7 files changed

+64
-31
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repos:
2626
- id: trailing-whitespace
2727
exclude: __snapshots__/.*\.ambr
2828
- repo: https://github.com/executablebooks/mdformat
29-
rev: 0.7.18
29+
rev: 0.7.19
3030
hooks:
3131
- id: mdformat
3232
additional_dependencies:

README.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Add this package wherever you use `mdformat` and the plugin will be auto-recogni
4949
```yaml
5050
repos:
5151
- repo: https://github.com/executablebooks/mdformat
52-
rev: 0.7.18
52+
rev: 0.7.19
5353
hooks:
5454
- id: mdformat
5555
additional_dependencies:
@@ -105,21 +105,33 @@ md.render(text)
105105
# </ul>
106106
```
107107

108-
## CLI Options
108+
## Configuration
109109

110-
`mdformat-mkdocs` adds the CLI argument `--align-semantic-breaks-in-lists` to optionally align line breaks in numbered lists to 3-spaces. If not specified, the default of 4-indents is followed universally.
110+
`mdformat-mkdocs` adds the CLI arguments:
111111

112-
```txt
113-
# with: mdformat
114-
1. Semantic line feed where the following line is
115-
three spaces deep
112+
- `--align-semantic-breaks-in-lists` to optionally align line breaks in numbered lists to 3-spaces. If not specified, the default of 4-indents is followed universally.
116113

117-
# vs. with: mdformat --align-semantic-breaks-in-lists
118-
1. Semantic line feed where the following line is
119-
three spaces deep
120-
```
114+
```txt
115+
# with: mdformat
116+
1. Semantic line feed where the following line is
117+
three spaces deep
118+
119+
# vs. "mdformat --align-semantic-breaks-in-lists"
120+
1. Semantic line feed where the following line is
121+
three spaces deep
122+
```
123+
124+
- `--ignore-missing-references` if set, do not escape link references when no definition is found. This is required when references are dynamic, such as with python mkdocstrings
121125
122-
Note: the `align-semantic-breaks-in-lists` setting is not supported in the configuration file yet (https://github.com/executablebooks/mdformat/issues/378)
126+
You can also use the toml configuration (https://mdformat.readthedocs.io/en/stable/users/configuration_file.html):
127+
128+
```toml
129+
# .mdformat.toml
130+
131+
[plugin.mkdocs]
132+
align_semantic_breaks_in_lists = true
133+
ignore_missing_references = true
134+
```
123135

124136
## Contributing
125137

mdformat_mkdocs/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
__version__ = "4.1.0"
44

5+
__plugin_name__ = "mkdocs"
6+
57
# FYI see source code for available interfaces:
68
# https://github.com/executablebooks/mdformat/blob/5d9b573ce33bae219087984dd148894c774f41d4/src/mdformat/plugins.py
7-
from .plugin import POSTPROCESSORS, RENDERERS, add_cli_options, update_mdit
9+
from .plugin import POSTPROCESSORS, RENDERERS, add_cli_argument_group, update_mdit
810

9-
__all__ = ("POSTPROCESSORS", "RENDERERS", "add_cli_options", "update_mdit")
11+
__all__ = ("POSTPROCESSORS", "RENDERERS", "add_cli_argument_group", "update_mdit")

mdformat_mkdocs/_helpers.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
from __future__ import annotations
44

55
import re
6+
from collections.abc import Mapping
67
from functools import wraps
7-
from typing import Callable
8+
from typing import Any, Callable
9+
10+
from . import __plugin_name__
811

912
EOL = "\n"
1013
"""Line delimiter."""
@@ -42,3 +45,16 @@ def separate_indent(line: str) -> tuple[str, str]:
4245
match = re_indent.match(line)
4346
assert match # for pyright
4447
return (match["indent"], match["content"])
48+
49+
50+
ContextOptions = Mapping[str, Any]
51+
52+
53+
def get_conf(options: ContextOptions, key: str) -> bool | str | int | None:
54+
"""Read setting from mdformat configuration Context."""
55+
cli_or_toml = (
56+
options["mdformat"].get("plugin", {}).get(__plugin_name__, {}).get(key)
57+
)
58+
if cli_or_toml is None:
59+
return options["mdformat"].get(key) # From API
60+
return cli_or_toml

mdformat_mkdocs/_normalize_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
EOL,
1717
FILLER_CHAR,
1818
MKDOCS_INDENT_COUNT,
19+
get_conf,
1920
rstrip_result,
2021
separate_indent,
2122
)
@@ -469,7 +470,7 @@ def normalize_list(
469470
return text
470471

471472
# Retrieve user-options
472-
inc_numbers = bool(context.options["mdformat"].get("number"))
473+
inc_numbers = bool(get_conf(context.options, "number"))
473474

474475
parsed_text = parse_text(
475476
text=text,

mdformat_mkdocs/_postprocess_inline.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from mdformat.renderer import RenderContext, RenderTreeNode
66

7-
from ._helpers import FILLER_CHAR, MKDOCS_INDENT_COUNT, rstrip_result
7+
from ._helpers import FILLER_CHAR, MKDOCS_INDENT_COUNT, get_conf, rstrip_result
88

99
FILLER = FILLER_CHAR * (MKDOCS_INDENT_COUNT - 2) # `mdformat` default is two spaces
1010
"""A spacer that is inserted and then removed to ensure proper word wrap."""
@@ -28,7 +28,7 @@ def postprocess_list_wrap(
2828
"""
2929
if not context.do_wrap:
3030
return text
31-
wrap_mode = context.options["mdformat"]["wrap"]
31+
wrap_mode = get_conf(context.options, "wrap")
3232
if (
3333
not isinstance(wrap_mode, int) # noqa: PLR0916
3434
or FILLER_CHAR in text

mdformat_mkdocs/plugin.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
from __future__ import annotations
44

5+
import argparse
56
import textwrap
6-
from argparse import ArgumentParser
77
from collections.abc import Mapping
88
from functools import partial
9-
from typing import Any
109

1110
from markdown_it import MarkdownIt
1211
from mdformat.renderer import DEFAULT_RENDERERS, RenderContext, RenderTreeNode
1312
from mdformat.renderer.typing import Postprocess, Render
1413

14+
from ._helpers import ContextOptions, get_conf
1515
from ._normalize_list import normalize_list as unbounded_normalize_list
1616
from ._postprocess_inline import postprocess_list_wrap
1717
from .mdit_plugins import (
@@ -29,16 +29,14 @@
2929
pymd_snippet_plugin,
3030
)
3131

32-
ContextOptions = Mapping[str, Any]
33-
3432

3533
def cli_is_ignore_missing_references(options: ContextOptions) -> bool:
3634
"""user-specified flag to turn off bracket escaping when no link reference found.
3735
3836
Addresses: https://github.com/KyleKing/mdformat-mkdocs/issues/19
3937
4038
"""
41-
return options["mdformat"].get("ignore_missing_references", False)
39+
return bool(get_conf(options, "ignore_missing_references")) or False
4240

4341

4442
def cli_is_align_semantic_breaks_in_lists(options: ContextOptions) -> bool:
@@ -48,17 +46,21 @@ def cli_is_align_semantic_breaks_in_lists(options: ContextOptions) -> bool:
4846
- and 2-spaces on subsequent bulleted items
4947
5048
"""
51-
return options["mdformat"].get("align_semantic_breaks_in_lists", False)
49+
return bool(get_conf(options, "align_semantic_breaks_in_lists")) or False
50+
5251

52+
def add_cli_argument_group(group: argparse._ArgumentGroup) -> None:
53+
"""Add options to the mdformat CLI.
5354
54-
def add_cli_options(parser: ArgumentParser) -> None:
55-
"""Add options to the mdformat CLI, to be stored in `mdit.options["mdformat"]`."""
56-
parser.add_argument(
55+
Stored in `mdit.options["mdformat"]["plugin"]["tables"]`
56+
57+
"""
58+
group.add_argument(
5759
"--align-semantic-breaks-in-lists",
5860
action="store_true",
5961
help="If specified, align semantic indents in numbered and bulleted lists to the text", # noqa: E501
6062
)
61-
parser.add_argument(
63+
group.add_argument(
6264
"--ignore-missing-references",
6365
action="store_true",
6466
help="If set, do not escape link references when no definition is found. This is required when references are dynamic, such as with python mkdocstrings", # noqa: E501
@@ -187,7 +189,7 @@ def add_extra_admon_newline(node: RenderTreeNode, context: RenderContext) -> str
187189

188190

189191
normalize_list = partial(
190-
unbounded_normalize_list,
192+
unbounded_normalize_list, # type: ignore[has-type]
191193
check_if_align_semantic_breaks_in_lists=cli_is_align_semantic_breaks_in_lists,
192194
)
193195

@@ -198,6 +200,6 @@ def add_extra_admon_newline(node: RenderTreeNode, context: RenderContext) -> str
198200
# will run in series.
199201
POSTPROCESSORS: Mapping[str, Postprocess] = {
200202
"bullet_list": normalize_list,
201-
"inline": postprocess_list_wrap,
203+
"inline": postprocess_list_wrap, # type: ignore[has-type]
202204
"ordered_list": normalize_list,
203205
}

0 commit comments

Comments
 (0)