MkImage
Image including optional caption.¶
Example: With caption¶
Example: Right-aligned¶
Example: Fixed width¶
Example: Hyperlinked¶
Example: Separate dark mode image¶
Bases: MkNode
__init__
¶
__init__(
path: str,
*,
target: LinkableType | None = None,
caption: str = "",
title: str = "",
align: Literal["left", "right"] | None = None,
width: int | None = None,
lazy: bool = False,
path_dark_mode: str | None = None,
**kwargs: Any
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
path of the image |
required |
target
|
LinkableType | None
|
Optional URL or node the image should link to |
None
|
caption
|
str
|
Image caption |
''
|
title
|
str
|
Image title |
''
|
align
|
Literal['left', 'right'] | None
|
Image alignment |
None
|
width
|
int | None
|
Image width in pixels |
None
|
lazy
|
bool
|
Whether to lazy-load image |
False
|
path_dark_mode
|
str | None
|
Optional alternative image for dark mode |
None
|
kwargs
|
Any
|
Keyword arguments passed to parent |
{}
|
Name | Children | Inherits |
---|---|---|
MkBinaryImage mknodes.basenodes.mkbinaryimage Image carrying the data by itself. |
||
MkBadge mknodes.templatenodes.mkbadge Node for a locally-created badge (based on "anybadge"). |
Name | Children | Inherits |
---|---|---|
MkNode mknodes.basenodes.mknode Base class for everything which can be expressed as Markup. |
graph TD
94721307729824["mkimage.MkImage"]
94721308848336["mknode.MkNode"]
94721311766592["node.Node"]
140564252373184["builtins.object"]
94721308848336 --> 94721307729824
94721311766592 --> 94721308848336
140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkimage/metadata.toml
[metadata]
icon = "mdi:image"
name = "MkImage"
group = "image"
[examples.with_caption]
title = "With caption"
jinja = """
{{ "https://picsum.photos/200" | MkImage(caption="A caption!") }}
"""
[examples.right_aligned]
title = "Right-aligned"
jinja = """
{{ "https://picsum.photos/200" | MkImage(align="right") }}
"""
[examples.fixed_width]
title = "Fixed width"
jinja = """
{{ "https://picsum.photos/200" | MkImage(width=500) }}
"""
[examples.hyperlinked]
title = "Hyperlinked"
jinja = """
{{ "https://picsum.photos/200" | MkImage(target="https://phil65.github.io/mknodes") }}
"""
[examples.dark_mode_image]
title = "Separate dark mode image"
jinja = """
{{ "https://picsum.photos/200" | MkImage(path_dark_mode="https://picsum.photos/300") }}
"""
[fragments]
image = """
![{{ node.title ~ "](" ~ path ~ (mode | add("#only-")) ~ ")" ~ (node.align | add("{ align=", " }")) ~ (node.width | add('{ width="', '" }')) }}{% if node.lazy %}{ loading=lazy }{% endif %}
"""
linked_image = """
{% if node.url %}
[{{ "fragments/image" | render_template(node=node, path=path, mode=mode) }}]({{ node.url }}){% else %}
{{ "fragments/image" | render_template(node=node, path=path, mode=mode) }}{% endif %}
"""
[output.markdown]
template = """
{% if node.path_dark_mode %}
{% set link_dark = "fragments/linked_image" | render_template(node=node, path=node.path_dark_mode, mode="dark") %}
{% set link_light = "fragments/linked_image" | render_template(node=node, path=node.path, mode="light") %}
{% set markdown_link = link_dark ~ " " ~ link_light %}
{% else %}
{% set markdown_link = "fragments/linked_image" | render_template(node=node, path=node.path, mode=None) %}
{% endif %}
{% if node.caption %}
<figure markdown>
{{ markdown_link }}
<figcaption>{{ node.caption }}</figcaption>
</figure>
{% else %}
{{ markdown_link }}{% endif %}
"""
[output.github]
template = """
<picture>
{% if node.path_dark_mode %}
<source media="(prefers-color-scheme: dark)" srcset="{{ node.path_dark_mode }}">
<source media="(prefers-color-scheme: light)" srcset="{{ node.path }}">
{% endif %}
<img alt="{{ node.caption }}" src="{{ node.path }}">
</picture>
"""
[output.rst]
template = """
.. image:: {{ node.path }}
{% if node.width %} :width: {{ node.width }}{% endif %}
{% if node.caption %} :alt: {{ node.caption }}{% endif %}
{% if node.align %} :align: {{ node.align }}{% endif %}
{% if node.url %} :target: {{ node.url }}{% endif %}
"""