<img> 图片元素
<img> 元素用于在正文、引用、列表、表格单元格或注释详情中插入块级图片。与 HTML 的单行 <img src="..."/> 不同,森空岛 Wiki XML 的 <img> 通过一组子元素完整描述图片尺寸、文件大小、资源地址、可选标识和图注文本。
本页内容
本页涵盖 <img> 及其七个子元素:
<url>(必填):图片资源的完整 URL。<width>(必填):图片像素宽度字符串(位于复杂表格单元格内时必须可解析为有限数值)。<height>(必填):图片像素高度字符串。<size>(必填):图片文件大小或尺寸标识字符串(通常为字节数,转换器按字符串透传)。<description>(必填):图片下方的图注文本,可为空,支持<br>换行。<id>(手写可省略,渲染器总输出):图片资源 ID。<format>(手写可省略,渲染器总输出):图片扩展名格式(如png、webp)。
试一试
展开交互编辑器,尝试省略 <id> 和 <format> 并使用 id_hash.ext 风格的 URL,观察转换器如何自动推断这两个字段。
试一试
属性
<img> 及其七个子元素均不定义任何属性。
示例
以下示例展示 URL 自动推断写法、显式 <id>/<format> 写法以及两类常见错误。
输入(省略 <id> 与 <format>)与规范化输出
当 <url> 的文件名符合 {id}_{hash}.{ext} 模式时,手写 XML 可以省略 <id> 和 <format>,转换器会自动按第一个 _ 前的部分推断 id,按最后一个 . 后的部分推断 format。
<?xml version="1.0" encoding="UTF-8"?>
<sklandDocument>
<itemId>111</itemId>
<metainfo>
<name>自动推断图片字段</name>
<cover/>
<subTypes/>
</metainfo>
<description>
<img>
<width>800</width>
<height>450</height>
<size>42000</size>
<url>https://example.com/assets/abc123_deadbeef.png</url>
<description>角色技能演示</description>
</img>
</description>
</sklandDocument>经过转换后再由 renderXml() 渲染的规范化输出,总会显式带上 <id>abc123</id> 和 <format>png</format>:
<?xml version="1.0" encoding="UTF-8"?>
<sklandDocument>
<itemId>111</itemId>
<metainfo>
<name>自动推断图片字段</name>
<cover showInDetail="false"></cover>
<subTypes></subTypes>
</metainfo>
<description>
<img>
<id>abc123</id>
<format>png</format>
<width>800</width>
<height>450</height>
<size>42000</size>
<url>https://example.com/assets/abc123_deadbeef.png</url>
<description>角色技能演示</description>
</img>
</description>
</sklandDocument>无法从 URL 推断且未显式提供 <id> 与 <format>(错误写法)
当 <url> 文件名不包含 _ 或 .(例如 photo.png 缺少下划线)时,省略 <id> 或 <format> 会导致转换失败。
<sklandDocument>
<itemId>112</itemId>
<metainfo>
<name>无法推断图片元信息</name>
<cover/>
<subTypes/>
</metainfo>
<description>
<img>
<width>100</width>
<height>100</height>
<size>1024</size>
<url>https://example.com/photo.png</url>
<description/>
</img>
</description>
</sklandDocument>转换器抛出 XmlWikiConversionError: Could not infer image id and format from url 'https://example.com/photo.png'.。在 <img> 内显式同时提供 <id> 和 <format> 即可修复。
复杂表格单元格内的图片宽度非数字(错误写法)
当 <img> 位于复杂表格单元格内(包括单元格内的引用、列表或注释中)时,validateComplexTableBlock() 会校验 <width> 必须可解析为有限数值。
<sklandDocument>
<itemId>113</itemId>
<metainfo>
<name>表内图片非法宽度</name>
<cover/>
<subTypes/>
</metainfo>
<description>
<table widths="300">
<tr>
<td>
<img>
<id>img1</id>
<format>png</format>
<width>auto</width>
<height>100</height>
<size>1024</size>
<url>https://example.com/photo.png</url>
<description/>
</img>
</td>
</tr>
</table>
</description>
</sklandDocument>转换器抛出 XmlWikiConversionError: Image width 'auto' is not numeric.。将 <width> 修改为纯数字字符串(例如 254)即可通过校验。
技术摘要
| 项目 | 说明 |
|---|---|
| 内容类别 | 块级图片元素 |
| 允许的内容 | 必填 <width>、<height>、<size>、<url>、<description>;可选 <id>、<format>(解析不限制子元素顺序) |
| 允许的父元素 | 混合块容器(注意:对话章节中的图片使用 <image src="..."/>,而非 <img>) |
| 是否可为空 | 否 |
| 对应 JSON 结构 | kind: "image", image: { id, url, width, height, size, format, kind: "", description, status: "", infos: [] } |
实现说明
- 与 README 的已知差异:
@eihrteam/xml仓库的 README 曾写明「<id>、<format>仅在无法从 URL 反推时保存」,而v1.0.0实际源码中的renderBlock()总会输出<id>和<format>;解析阶段才允许省略它们,并在缺少任意一项时调用inferImageParts(url)按{id}_{hash}.{ext}规则推断。本文以代码实际行为为准。 - 空白处理:
<id>、<format>、<width>、<height>、<size>、<url>均做空白折叠与首尾裁剪;<description>通过contentTextWithBreaks()保留显式<br>换行并在输出时统一写成<br></br>,不允许包含<emoji>或<annotation>。