Skip to content

Commit 3efa463

Browse files
Merge pull request #255 from adamtheturtle/admonition-support
Add support for `.. admonition`
2 parents 2e4646b + 102a47f commit 3efa463

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

sample/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ This text has :strike:`strike` formatting and :del:`deleted text` as well.
103103

104104
This is an important admonition that highlights important information.
105105

106+
.. admonition:: Custom Admonition Title
107+
108+
This is a generic admonition with a custom title. You can use this for
109+
any type of callout that doesn't fit the standard admonition types.
110+
111+
It supports all the same features:
112+
113+
* Bullet points
114+
* **Bold text** and *italic text*
115+
* ``Code snippets``
116+
106117
.. collapse:: Click to expand this section
107118

108119
This content is hidden by default and can be expanded by clicking the toggle.

sample/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
reStructuredText
2+
callout

src/sphinx_notion/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,46 @@ def _(
867867
)
868868

869869

870+
@beartype
871+
@_process_node_to_blocks.register
872+
def _(
873+
node: nodes.admonition,
874+
*,
875+
section_level: int,
876+
) -> list[Block]:
877+
"""Process generic admonition nodes by creating Notion Callout blocks.
878+
879+
Generic admonitions have a title as the first child, followed by
880+
content. The title becomes the callout text, and all content becomes
881+
nested blocks.
882+
"""
883+
del section_level
884+
885+
# Extract the title from the first child (admonitions always have title
886+
# as first child)
887+
title_node = node.children[0]
888+
assert isinstance(title_node, nodes.title)
889+
title_text = title_node.astext()
890+
# All remaining children become nested blocks
891+
content_children = node.children[1:]
892+
893+
block = UnoCallout(
894+
text=text(text=title_text),
895+
icon=Emoji(emoji="💬"),
896+
color=BGColor.GRAY,
897+
)
898+
899+
for child in content_children:
900+
block.append(
901+
blocks=_process_node_to_blocks(
902+
child,
903+
section_level=1,
904+
)
905+
)
906+
907+
return [block]
908+
909+
870910
@beartype
871911
@_process_node_to_blocks.register
872912
def _(

tests/test_integration.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,52 @@ def test_admonition_with_bullet_points(
979979
)
980980

981981

982+
def test_generic_admonition(
983+
*,
984+
make_app: Callable[..., SphinxTestApp],
985+
tmp_path: Path,
986+
) -> None:
987+
"""Generic admonitions set callout text to the first line of the callout.
988+
989+
Generic admonitions require a title so are different from other
990+
admonitions.
991+
"""
992+
rst_content = """
993+
.. admonition:: Important Information
994+
995+
This is the first paragraph.
996+
997+
This is the second paragraph.
998+
"""
999+
1000+
callout = UnoCallout(
1001+
text=text(text="Important Information"),
1002+
icon=Emoji(emoji="💬"),
1003+
color=BGColor.GRAY,
1004+
)
1005+
1006+
nested_paragraph1 = UnoParagraph(
1007+
text=text(text="This is the first paragraph.")
1008+
)
1009+
nested_paragraph2 = UnoParagraph(
1010+
text=text(text="This is the second paragraph.")
1011+
)
1012+
1013+
callout.append(blocks=[nested_paragraph1])
1014+
callout.append(blocks=[nested_paragraph2])
1015+
1016+
expected_objects: list[Block] = [
1017+
callout,
1018+
]
1019+
1020+
_assert_rst_converts_to_notion_objects(
1021+
rst_content=rst_content,
1022+
expected_objects=expected_objects,
1023+
make_app=make_app,
1024+
tmp_path=tmp_path,
1025+
)
1026+
1027+
9821028
def test_nested_bullet_list(
9831029
*,
9841030
make_app: Callable[..., SphinxTestApp],

0 commit comments

Comments
 (0)