Skip to content

Commit 97c3f57

Browse files
authored
feat: Allow specifying mdformat extensions
Issue-23: #23 PR-27: #27
1 parent c18abc1 commit 97c3f57

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ ci = [
8787
"duty>=1.6",
8888
"griffe>=2.0",
8989
"inline-snapshot>=0.6",
90+
"mdformat-tables>=1.0",
9091
"pytest>=8.2",
9192
"pytest-cov>=5.0",
9293
"pytest-randomly>=3.15",

src/griffe2md/_internal/cli.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing import Any
1717

1818
from griffe2md._internal import debug
19-
from griffe2md._internal.config import load_config
19+
from griffe2md._internal.config import ConfigDict, load_config
2020
from griffe2md._internal.main import write_package_docs
2121

2222

@@ -44,6 +44,12 @@ def get_parser() -> argparse.ArgumentParser:
4444
help="Whether to format the resulting Markdown using `mdformat`.",
4545
)
4646
parser.add_argument("-o", "--output", default=None, help="File to write to. Default: stdout.")
47+
parser.add_argument(
48+
"--mdformat-extensions",
49+
nargs="*",
50+
default=[],
51+
help="mdformat extensions to enable (e.g. 'tables'). Requires the corresponding mdformat plugin to be installed.",
52+
)
4753
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug._get_version()}")
4854
parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")
4955
return parser
@@ -64,5 +70,9 @@ def main(args: list[str] | None = None) -> int:
6470
opts = parser.parse_args(args=args)
6571
config = load_config()
6672

73+
if opts.mdformat_extensions:
74+
config = ConfigDict() if config is None else config
75+
config["mdformat_extensions"] = opts.mdformat_extensions
76+
6777
write_package_docs(opts.package, config, opts.output, format_md=opts.format_md)
6878
return 0

src/griffe2md/_internal/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ class ConfigDict(TypedDict, total=False):
103103
load_external_modules: bool
104104
"""Whether to always load external modules/packages."""
105105

106+
mdformat_extensions: list[str]
107+
"""A list of mdformat extensions to enable when formatting Markdown output.
108+
109+
For example, `["tables"]` to enable the `mdformat-tables` extension,
110+
which properly handles escaped pipes in table cells.
111+
Users are responsible for installing such extensions in the environment.
112+
"""
113+
106114
members: list[str] | bool | None
107115
"""A boolean, or an explicit list of members to render.
108116

src/griffe2md/_internal/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def render_object_docs(obj: Object, config: ConfigDict | None = None, *, format_
137137
context = prepare_context(obj, config)
138138
rendered = env.get_template(f"{obj.kind.value}.md.jinja").render(**context)
139139
if format_md:
140-
rendered = mdformat.text(rendered)
140+
mdformat_ext = (config or {}).get("mdformat_extensions", [])
141+
rendered = mdformat.text(rendered, extensions=mdformat_ext)
141142
return rendered
142143

143144

tests/test_rendering.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import griffe
2+
from inline_snapshot import snapshot
3+
4+
from griffe2md import render_object_docs
5+
6+
7+
def test_mdformat_extensions() -> None:
8+
docstring = griffe.Docstring(
9+
"""A docstring with a table.
10+
11+
Header 1 | Header 2
12+
------ | ---
13+
Cell 1 | Cell 2
14+
""",
15+
)
16+
attribute = griffe.Attribute(name="render_me", parent=None, value="...", docstring=docstring)
17+
18+
not_formatted = render_object_docs(attribute, format_md=False).strip("\n") + "\n"
19+
assert not_formatted == snapshot("""\
20+
## `render_me`
21+
22+
```python
23+
render_me = ...
24+
```
25+
26+
A docstring with a table.
27+
28+
Header 1 | Header 2
29+
------ | ---
30+
Cell 1 | Cell 2
31+
""")
32+
33+
formatted_no_table = render_object_docs(attribute, format_md=True).strip("\n") + "\n"
34+
assert formatted_no_table == snapshot("""\
35+
## `render_me`
36+
37+
```python
38+
render_me = ...
39+
```
40+
41+
A docstring with a table.
42+
43+
Header 1 | Header 2
44+
------ | ---
45+
Cell 1 | Cell 2
46+
""")
47+
formatted_table = (
48+
render_object_docs(attribute, config={"mdformat_extensions": ["tables"]}, format_md=True).strip("\n") + "\n"
49+
)
50+
assert formatted_table == snapshot("""\
51+
## `render_me`
52+
53+
```python
54+
render_me = ...
55+
```
56+
57+
A docstring with a table.
58+
59+
| Header 1 | Header 2 |
60+
| -------- | -------- |
61+
| Cell 1 | Cell 2 |
62+
""")

0 commit comments

Comments
 (0)