Skip to content

Commit cb94a30

Browse files
authored
Add support for icons in cards
1 parent e031b25 commit cb94a30

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

neoteroi/mkdocs/cards/html.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import xml.etree.ElementTree as etree
33
from dataclasses import dataclass
44

5+
from neoteroi.mkdocs.markdown.images import build_icon_html
56
from neoteroi.mkdocs.markdown.images import build_image_html
67

78
from .domain import CardItem, Cards
@@ -64,7 +65,12 @@ def build_item_html(self, parent, item: CardItem):
6465

6566
wrapper_element = etree.SubElement(first_child, "div", {})
6667

67-
self.build_image_html(wrapper_element, item)
68+
if item.image:
69+
self.build_image_html(wrapper_element, item)
70+
elif item.icon:
71+
build_icon_html(etree.SubElement(
72+
wrapper_element, "div", {"class": "nt-card-icon"}
73+
), item.icon)
6874

6975
text_wrapper = etree.SubElement(
7076
wrapper_element, "div", {"class": "nt-card-content"}

styles/cards.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
margin-top: 5px;
113113
}
114114

115+
.nt-card .nt-card-icon {
116+
text-align: center;
117+
padding-top: 12px;
118+
min-height: 120px;
119+
}
120+
121+
.nt-card .nt-card-icon .icon {
122+
font-size: 95px;
123+
line-height: 1;
124+
}
115125

116126
.nt-card a:hover,
117127
.nt-card a:focus {

tests/test_cards.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767
</div>
6868
"""
6969

70+
EXAMPLE_6 = """
71+
<div class="nt-cards nt-grid cols-3">
72+
<div class="nt-card"><a href="/some-path/a"><div><div class="nt-card-icon"><img alt="step icon" class="icon" src="/img/icons/icon.svg" /></div><div class="nt-card-content"><p class="nt-card-title">Title A</p><p class="nt-card-text">Lorem ipsum dolor sit amet 1.</p></div></div></a></div>
73+
<div class="nt-card"><a href="/some-path/b"><div><div class="nt-card-icon"><i class="fa-solid fa-star icon"></i></div><div class="nt-card-content"><p class="nt-card-title">Title B</p><p class="nt-card-text">Lorem ipsum dolor sit amet 2.</p></div></div></a></div>
74+
<div class="nt-card"><a href="/some-path/c"><div><div class="nt-card-icon"><span class="icon">:octicons-bug-16:</span></div><div class="nt-card-content"><p class="nt-card-title">Title C</p><p class="nt-card-text">Lorem ipsum dolor sit amet 3.</p></div></div></a></div>
75+
</div>
76+
"""
77+
7078

7179
def test_base_cards_processor_raises_for_not_list_input():
7280
processor = BaseCardsProcessor()
@@ -248,3 +256,36 @@ def test_cards_extension_image_tags(example, expected_result):
248256
def test_cards_extension(example, expected_result):
249257
html = markdown.markdown(example, extensions=[CardsExtension(priority=100)])
250258
assert equal_html(html, expected_result)
259+
260+
261+
@pytest.mark.parametrize(
262+
"example,expected_result",
263+
[
264+
[
265+
"""
266+
::cards::
267+
268+
- title: Title A
269+
url: /some-path/a
270+
content: Lorem ipsum dolor sit amet 1.
271+
icon: /img/icons/icon.svg
272+
273+
- title: Title B
274+
url: /some-path/b
275+
content: Lorem ipsum dolor sit amet 2.
276+
icon: "fa-solid fa-star"
277+
278+
- title: Title C
279+
url: /some-path/c
280+
content: Lorem ipsum dolor sit amet 3.
281+
icon: ":octicons-bug-16:"
282+
283+
::/cards::
284+
""",
285+
EXAMPLE_6,
286+
],
287+
],
288+
)
289+
def test_cards_extension_icons(example, expected_result):
290+
html = markdown.markdown(example, extensions=[CardsExtension(priority=100)])
291+
assert html.strip() == expected_result.strip()

0 commit comments

Comments
 (0)