Skip to content

MkMaterialBadge

:fa-brands-github: Show source on GitHub

Node for a CSS-based badge a la MkDocs-Material.

Example: Regular

Jinja

{{ "mdi:wrench" | MkMaterialBadge(text="text") }}

Python

MkMaterialBadge('mdi:wrench', 'text')

:mdi-wrench: text

<span class="md-typeset mdx-badge">
  <span class="mdx-badge__icon">:mdi-wrench:</span>
  <span class="mdx-badge__text">text</span>
</span>
<p><span class="md-typeset mdx-badge">
  <span class="mdx-badge__icon"><span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="m22.7 19l-9.1-9.1c.9-2.3.4-5-1.5-6.9c-2-2-5-2.4-7.4-1.3L9 6L6 9L1.6 4.7C.4 7.1.9 10.1 2.9 12.1c1.9 1.9 4.6 2.4 6.9 1.5l9.1 9.1c.4.4 1 .4 1.4 0l2.3-2.3c.5-.4.5-1.1.1-1.4"/></svg></span></span>
  <span class="mdx-badge__text">text</span>
</span></p>
<img src="https://api.iconify.design/mdi/wrench.svg?height=16" alt="wrench"> <code>text</code>

Example: Heart type

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", animated=True) }}

Python

MkMaterialBadge('mdi:file', 'text', animated=True)

:mdi-file: text

<span class="md-typeset mdx-badge mdx-badge--heart">
  <span class="mdx-badge__icon">:mdi-file:</span>
  <span class="mdx-badge__text">text</span>
</span>
<p><span class="md-typeset mdx-badge mdx-badge--heart">
  <span class="mdx-badge__icon"><span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M13 9V3.5L18.5 9M6 2c-1.11 0-2 .89-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z"/></svg></span></span>
  <span class="mdx-badge__text">text</span>
</span></p>
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>

Example: Right aligned

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", align_right=True) }}

Python

MkMaterialBadge('mdi:file', 'text', align_right=True)

:mdi-file: text

<span class="md-typeset mdx-badge mdx-badge--right">
  <span class="mdx-badge__icon">:mdi-file:</span>
  <span class="mdx-badge__text">text</span>
</span>
<p><span class="md-typeset mdx-badge mdx-badge--right">
  <span class="mdx-badge__icon"><span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M13 9V3.5L18.5 9M6 2c-1.11 0-2 .89-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z"/></svg></span></span>
  <span class="mdx-badge__text">text</span>
</span></p>
<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>

Jinja

{{ "mdi:file" | MkMaterialBadge(text="text", target="https://phil65.github.io/mknodes") }}

Python

MkMaterialBadge('mdi:file', 'text', target='https://phil65.github.io/mknodes')

:mdi-file: text

<span class="md-typeset mdx-badge">
  <span class="mdx-badge__icon">[:mdi-file:](https://phil65.github.io/mknodes)</span>
  <span class="mdx-badge__text">text</span>
</span>
<p><span class="md-typeset mdx-badge">
  <span class="mdx-badge__icon"><a href="https://phil65.github.io/mknodes"><span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M13 9V3.5L18.5 9M6 2c-1.11 0-2 .89-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z"/></svg></span></a></span>
  <span class="mdx-badge__text">text</span>
</span></p>
[<img src="https://api.iconify.design/mdi/file.svg?height=16" alt="file"> <code>text</code>](https://phil65.github.io/mknodes)

Bases: MkTemplate

__init__

__init__(
    icon: str,
    text: str = "",
    *,
    animated: bool = False,
    align_right: bool = False,
    target: LinkableType | None = None,
    **kwargs: Any
) -> None

Parameters:

Name Type Description Default
icon str

Icon to display. Can either be an iconify or an emoji slug

required
text str

Text to display

''
animated bool

Optional animated style

False
align_right bool

Right-align badge

False
target LinkableType | None

An optional URL / page target for the badge

None
kwargs Any

Keyword arguments passed to parent

{}
graph TD
  3179977919["mkmaterialbadge.MkMaterialBadge"]
  1289759389["mktemplate.MkTemplate"]
  3160803176["rendercache.TemplateRenderMixin"]
  2751680898["builtins.object"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  1289759389 --> 3179977919
  3160803176 --> 1289759389
  2751680898 --> 3160803176
  835538629 --> 1289759389
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkmaterialbadge/metadata.toml
[metadata]
name = "MkMaterialBadge"
icon = "simple-icons:shieldsdotio"
group = "badge"

[[resources.css]]
filename = "materialbadge.css"

[examples.regular]
title = "Regular"
jinja = """
{{ "mdi:wrench" | MkMaterialBadge(text="text") }}
"""

[examples.heart_type]
title = "Heart type"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", animated=True) }}
"""

[examples.right_aligned]
title = "Right aligned"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", align_right=True) }}
"""

[examples.linked]
title = "With link"
jinja = """
{{ "mdi:file" | MkMaterialBadge(text="text", target="https://phil65.github.io/mknodes") }}
"""

[output.html]
template = """
<span class="md-typeset mdx-badge{{ " mdx-badge--right" if node.align_right else "" }}{{ " mdx-badge--heart" if node.animated else "" }}">
{% set icon = "[" ~ node.icon | get_emoji_slug ~ "](" ~ node.target | get_url ~ ")" if node.target else node.icon | get_emoji_slug %}
{% if node.icon %}  <span class="mdx-badge__icon">{{ icon }}</span>
{% endif %}
{% if node.get_text() %}  <span class="mdx-badge__text">{{ node.get_text() }}</span>
{% endif %}
</span>
"""
mknodes.basenodes.mkmaterialbadge.MkMaterialBadge
class MkMaterialBadge(mktemplate.MkTemplate):
    """Node for a CSS-based badge a la MkDocs-Material."""

    ICON = "simple/shieldsdotio"
    CSS = [resources.CSSFile("materialbadge.css")]

    def __init__(
        self,
        icon: str,
        text: str = "",
        *,
        animated: bool = False,
        align_right: bool = False,
        target: linkprovider.LinkableType | None = None,
        **kwargs: Any,
    ) -> None:
        """Constructor.

        Args:
            icon: Icon to display. Can either be an iconify or an emoji slug
            text: Text to display
            animated: Optional animated style
            align_right: Right-align badge
            target: An optional URL / page target for the badge
            kwargs: Keyword arguments passed to parent
        """
        super().__init__("output/html/template", **kwargs)
        self.icon = icon
        self._text = text
        self.animated = animated
        self.align_right = align_right
        self.target = target

    async def get_text(self) -> str:
        return self._text
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes
def create_section_for_nodes(
    nav: mk.MkNav,
    klasses: Sequence[type[mk.MkNode]],
) -> mk.MkTable:
    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
    for kls in klasses:
        tpl = "classpage_custom.jinja"
        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
        nav += page
        if kls.STATUS:
            page.metadata.status = kls.STATUS
        elif kls.JS_FILES:
            page.metadata.status = "js"
        elif kls.CSS:
            page.metadata.status = "css"
        page.created_by = create_section_for_nodes
        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
        table.add_row((link, kls.__doc__, extensions))
    return table
Resources
Resources(css=[],
          markdown_extensions={'admonition': {},
                               'pymdownx.details': {},
                               'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
                                                                           'format': <function fence_code_format at 0x703933662e50>,
                                                                           'name': 'mermaid'}]},
                               'pymdownx.tabbed': {},
                               'tables': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[Asset('materialbadge_31c4bf6.css')],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
  source_function: create_section_for_nodes
  source_line_no: 95
hide:
- toc
icon: simple/shieldsdotio
status: new
title: MkMaterialBadge