Skip to content

Commit 0675170

Browse files
committed
feat: expand types for MarkdownConverter
1 parent 3b2b402 commit 0675170

File tree

2 files changed

+81
-25
lines changed

2 files changed

+81
-25
lines changed

markdownify/__init__.pyi

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from _typeshed import Incomplete
2+
from typing import TypedDict, Unpack
23

34
ATX: str
45
ATX_CLOSED: str
@@ -13,29 +14,38 @@ RSTRIP: str
1314
STRIP: str
1415
STRIP_ONE: str
1516

16-
def markdownify(
17-
html: str,
18-
strip: list[str] = ...,
19-
convert: list[str] = ...,
20-
autolinks: bool = ...,
21-
bullets: str = ..., # An iterable of bullet types.
22-
code_language: str = ...,
23-
code_language_callback: Incomplete = ...,
24-
default_title: bool = ...,
25-
escape_asterisks: bool = ...,
26-
escape_underscores: bool = ...,
27-
escape_misc: bool = ...,
28-
heading_style: str = ...,
29-
keep_inline_images_in: list[str] = ...,
30-
newline_style: str = ...,
31-
strip_document: str | None = ...,
32-
strong_em_symbol: str = ...,
33-
sub_symbol: str = ...,
34-
sup_symbol: str = ...,
35-
table_infer_header: bool = ...,
36-
wrap: bool = ...,
37-
wrap_width: int = ...,
38-
) -> str: ...
17+
18+
class Options(TypedDict, total=False):
19+
autolinks: bool
20+
bs4_options: str
21+
bullets: str # An iterable of bullet types.
22+
code_language: str
23+
code_language_callback: Incomplete | None
24+
convert: list[str] | None
25+
default_title: bool
26+
escape_asterisks: bool
27+
escape_underscores: bool
28+
escape_misc: bool
29+
heading_style: str
30+
keep_inline_images_in: list[str]
31+
newline_style: str
32+
strip: list[str] | None
33+
strip_document: str | None
34+
strip_pre: str
35+
strong_em_symbol: str
36+
sub_symbol: str
37+
sup_symbol: str
38+
table_infer_header: bool
39+
wrap: bool
40+
wrap_width: int
41+
42+
43+
def markdownify(html: str, **kwargs: Unpack[Options]) -> str: ...
44+
3945

4046
class MarkdownConverter:
41-
pass
47+
def __init__(self, **kwargs: Unpack[Options]) -> None:
48+
...
49+
50+
def convert(self, html: str) -> str:
51+
...

tests/types.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
1-
from markdownify import markdownify, LSTRIP, RSTRIP, STRIP
1+
from markdownify import markdownify, ASTERISK, ATX, BACKSLASH, LSTRIP, RSTRIP, SPACES, STRIP, UNDERLINED, UNDERSCORE, MarkdownConverter
22

33
markdownify("<p>Hello</p>") == "Hello" # test default of STRIP
44
markdownify("<p>Hello</p>", strip_document=LSTRIP) == "Hello\n\n"
55
markdownify("<p>Hello</p>", strip_document=RSTRIP) == "\n\nHello"
66
markdownify("<p>Hello</p>", strip_document=STRIP) == "Hello"
77
markdownify("<p>Hello</p>", strip_document=None) == "\n\nHello\n\n"
8+
9+
# default options
10+
MarkdownConverter(
11+
autolinks=True,
12+
bs4_options='html.parser',
13+
bullets='*+-',
14+
code_language='',
15+
code_language_callback=None,
16+
convert=None,
17+
default_title=False,
18+
escape_asterisks=True,
19+
escape_underscores=True,
20+
escape_misc=False,
21+
heading_style=UNDERLINED,
22+
keep_inline_images_in=[],
23+
newline_style=SPACES,
24+
strip=None,
25+
strip_document=STRIP,
26+
strip_pre=STRIP,
27+
strong_em_symbol=ASTERISK,
28+
sub_symbol='',
29+
sup_symbol='',
30+
table_infer_header=False,
31+
wrap=False,
32+
wrap_width=80,
33+
).convert("")
34+
35+
# custom options
36+
MarkdownConverter(
37+
strip_document=None,
38+
bullets="-",
39+
escape_asterisks=True,
40+
escape_underscores=True,
41+
escape_misc=True,
42+
autolinks=True,
43+
default_title=True,
44+
newline_style=BACKSLASH,
45+
sup_symbol='^',
46+
sub_symbol='^',
47+
keep_inline_images_in=['h3'],
48+
wrap=True,
49+
wrap_width=80,
50+
strong_em_symbol=UNDERSCORE,
51+
code_language='python',
52+
code_language_callback=None
53+
).convert("")

0 commit comments

Comments
 (0)