Skip to content

Commit a07491f

Browse files
committed
feat: allow specifying mdformat extensions via config and CLI
Add `mdformat_extensions` option to ConfigDict and expose it through the CLI (`--mdformat-extensions`). This lets users enable mdformat plugins like `mdformat-tables`, which properly preserves escaped pipes in table cells and fixes broken tabular rendering for union type hints. Closes #23, fixes #22 Made-with: Cursor
1 parent 9f6f1d9 commit a07491f

11 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/griffe2md/_internal/cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def get_parser() -> argparse.ArgumentParser:
3838
parser = argparse.ArgumentParser(prog="griffe2md")
3939
parser.add_argument("package", help="The package to output Markdown docs for.")
4040
parser.add_argument("-o", "--output", default=None, help="File to write to. Default: stdout.")
41+
parser.add_argument(
42+
"--mdformat-extensions",
43+
nargs="*",
44+
default=[],
45+
help="mdformat extensions to enable (e.g. 'tables'). Requires the corresponding mdformat plugin to be installed.",
46+
)
4147
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug._get_version()}")
4248
parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")
4349
return parser
@@ -56,7 +62,10 @@ def main(args: list[str] | None = None) -> int:
5662
"""
5763
parser = get_parser()
5864
opts = parser.parse_args(args=args)
59-
config = load_config()
65+
config = load_config() or {}
66+
67+
if opts.mdformat_extensions:
68+
config["mdformat_extensions"] = opts.mdformat_extensions
6069

6170
write_package_docs(opts.package, config, opts.output)
6271
return 0

src/griffe2md/_internal/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ class ConfigDict(TypedDict):
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+
"""
112+
106113
members: list[str] | bool | None
107114
"""A boolean, or an explicit list of members to render.
108115
@@ -265,6 +272,7 @@ class ConfigDict(TypedDict):
265272
"show_docstring_functions": True,
266273
"show_docstring_modules": True,
267274
"extensions": [],
275+
"mdformat_extensions": [],
268276
"search_paths": [],
269277
}
270278
"""Default configuration values."""

src/griffe2md/_internal/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ 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+
full_config = cast("ConfigDict", {**default_config, **(config or {})})
141+
mdformat_ext = full_config.get("mdformat_extensions", [])
142+
rendered = mdformat.text(rendered, extensions=mdformat_ext)
141143
return rendered
142144

143145

src/griffe2md/templates/docstring/attributes.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description
66
---- | ---- | -----------
77
{%- for attribute in section.value %}
8-
[`{{ attribute.name }}`](#{{ obj.path }}.{{ attribute.name }}) | {% if attribute.annotation -%}{%- with expression = attribute.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ attribute.description|newline_to_br }}
8+
[`{{ attribute.name }}`](#{{ obj.path }}.{{ attribute.name }}) | {% if attribute.annotation -%}{%- with expression = attribute.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ attribute.description|newline_to_br }}
99
{%- endfor -%}
1010

1111
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/other_parameters.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description
66
---- | ---- | -----------
77
{%- for parameter in section.value %}
8-
`{{ parameter.name }}` | {% if parameter.annotation -%}{%- with expression = parameter.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ parameter.description|newline_to_br }}
8+
`{{ parameter.name }}` | {% if parameter.annotation -%}{%- with expression = parameter.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ parameter.description|newline_to_br }}
99
{%- endfor -%}
1010

1111
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/parameters.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description | Default
66
---- | ---- | ----------- | -------
77
{%- for parameter in section.value %}
8-
`{{ parameter.name }}` | {% if parameter.annotation -%}{%- with expression = parameter.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ parameter.description|newline_to_br }} | {% if parameter.default -%}{%- with expression = parameter.default -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- else -%}*required*{%- endif %}
8+
`{{ parameter.name }}` | {% if parameter.annotation -%}{%- with expression = parameter.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ parameter.description|newline_to_br }} | {% if parameter.default -%}{%- with expression = parameter.default -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- else -%}*required*{%- endif %}
99
{%- endfor -%}
1010

1111
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/raises.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Type | Description
66
---- | -----------
77
{% for raises in section.value %}
8-
{%- if raises.annotation -%}{%- with expression = raises.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif -%} | {{ raises.description|newline_to_br }}
8+
{%- if raises.annotation -%}{%- with expression = raises.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif -%} | {{ raises.description|newline_to_br }}
99
{% endfor %}
1010

1111
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/receives.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{% if name_column -%}Name | {% endif -%}Type | Description
77
{% if name_column -%}---- | {% endif -%}---- | -----------
88
{% for receives in section.value %}
9-
{%- if name_column -%}{%- if receives.name -%}`{{ receives.name }}`{%- endif %} | {% endif %}{% if receives.annotation -%}{%- with expression = receives.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ receives.description|newline_to_br }}
9+
{%- if name_column -%}{%- if receives.name -%}`{{ receives.name }}`{%- endif %} | {% endif %}{% if receives.annotation -%}{%- with expression = receives.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ receives.description|newline_to_br }}
1010
{% endfor %}
1111

1212
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/returns.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{% if name_column -%}Name | {% endif -%}Type | Description
77
{% if name_column -%}---- | {% endif -%}---- | -----------
88
{% for returns in section.value %}
9-
{%- if name_column -%}{%- if returns.name -%}`{{ returns.name }}`{%- endif %} | {% endif %}{% if returns.annotation -%}{%- with expression = returns.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ returns.description|newline_to_br }}
9+
{%- if name_column -%}{%- if returns.name -%}`{{ returns.name }}`{%- endif %} | {% endif %}{% if returns.annotation -%}{%- with expression = returns.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif %} | {{ returns.description|newline_to_br }}
1010
{% endfor %}
1111

1212
{%- elif config.docstring_section_style == "list" %}

src/griffe2md/templates/docstring/warns.md.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Type | Description
66
---- | -----------
77
{% for warns in section.value %}
8-
{%- if warns.annotation -%}{%- with expression = warns.annotation -%}<code>{% filter replace("|", "&#124;") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif -%} | {{ warns.description|newline_to_br }}
8+
{%- if warns.annotation -%}{%- with expression = warns.annotation -%}<code>{% filter replace("|", "\\|") %}{%- include "expression.md.jinja" with context -%}{% endfilter %}</code>{%- endwith -%}{%- endif -%} | {{ warns.description|newline_to_br }}
99
{% endfor %}
1010

1111
{%- elif config.docstring_section_style == "list" %}

0 commit comments

Comments
 (0)