Skip to content

Commit 4618d08

Browse files
Merge pull request #298 from adamtheturtle/kbd
Add support for the :kbd: role
2 parents a718985 + 1c22e1f commit 4618d08

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

sample/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ Other Text Styles
3535

3636
The builder supports additional text styles: :text-bold:`bold text`, :text-italic:`italic text`, :text-mono:`monospace text`, :text-strike:`strikethrough text`, and :text-underline:`underlined text`.
3737

38+
Keyboard Shortcuts
39+
~~~~~~~~~~~~~~~~~~
40+
41+
The builder supports keyboard shortcuts using the standard ``:kbd:`` role: Press :kbd:`Ctrl+C` to copy, :kbd:`Ctrl+V` to paste, and :kbd:`Ctrl+Z` to undo.
42+
3843
.. note::
3944

4045
This is an important note that demonstrates the note admonition support.

src/sphinx_notion/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ def _create_styled_text_from_node(*, child: nodes.Element) -> Text:
349349

350350
is_bold = isinstance(child, nodes.strong) or "text-bold" in classes
351351
is_italic = isinstance(child, nodes.emphasis) or "text-italic" in classes
352-
is_code = isinstance(child, nodes.literal) or "text-mono" in classes
352+
is_code = (
353+
isinstance(child, nodes.literal)
354+
or "text-mono" in classes
355+
or "kbd" in classes
356+
)
353357
is_strikethrough = (
354358
isinstance(child, strike_node) or "text-strike" in classes
355359
)
@@ -361,6 +365,7 @@ def _create_styled_text_from_node(*, child: nodes.Element) -> Text:
361365
"text-mono",
362366
"text-strike",
363367
"text-underline",
368+
"kbd",
364369
*color_mapping.keys(),
365370
*bg_color_classes,
366371
}

tests/test_integration.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,54 @@ def test_inline_single_backticks(
27472747
)
27482748

27492749

2750+
def test_kbd_role(
2751+
*,
2752+
make_app: Callable[..., SphinxTestApp],
2753+
tmp_path: Path,
2754+
) -> None:
2755+
"""The ``:kbd:`` role creates keyboard input formatting.
2756+
2757+
The ``:kbd:`` role splits keyboard shortcuts into separate segments,
2758+
where each key is formatted as code but the + separator is not.
2759+
"""
2760+
rst_content = """
2761+
Press :kbd:`Ctrl+C` to copy and :kbd:`Ctrl+V` to paste.
2762+
"""
2763+
2764+
normal_text1 = text(text="Press ")
2765+
kbd_text1 = text(text="Ctrl", code=True)
2766+
plus_text1 = text(text="+", code=False)
2767+
kbd_text2 = text(text="C", code=True)
2768+
normal_text2 = text(text=" to copy and ")
2769+
kbd_text3 = text(text="Ctrl", code=True)
2770+
plus_text2 = text(text="+", code=False)
2771+
kbd_text4 = text(text="V", code=True)
2772+
normal_text3 = text(text=" to paste.")
2773+
2774+
combined_text = (
2775+
normal_text1
2776+
+ kbd_text1
2777+
+ plus_text1
2778+
+ kbd_text2
2779+
+ normal_text2
2780+
+ kbd_text3
2781+
+ plus_text2
2782+
+ kbd_text4
2783+
+ normal_text3
2784+
)
2785+
2786+
expected_paragraph = UnoParagraph(text=combined_text)
2787+
2788+
expected_objects: list[Block] = [expected_paragraph]
2789+
2790+
_assert_rst_converts_to_notion_objects(
2791+
rst_content=rst_content,
2792+
expected_objects=expected_objects,
2793+
make_app=make_app,
2794+
tmp_path=tmp_path,
2795+
)
2796+
2797+
27502798
def test_unsupported_node_types_in_rich_text(
27512799
*,
27522800
make_app: Callable[..., SphinxTestApp],

0 commit comments

Comments
 (0)