安装与概览
@eihrteam/xml(当前文档基于 v1.0.0)是森空岛 Wiki XML 与官方 Wiki JSON 之间的零依赖双向转换库,同时内置命令行工具 xml。所有解析、校验与渲染逻辑均在本地完成,不依赖原生扩展,也不发起任何网络请求。
环境要求与安装
@eihrteam/xml 以纯 ESM 形式发布,要求 Node.js 20 及以上版本,也可以在现代浏览器与打包工具(如 Vite、Webpack、Next.js)中直接导入使用(仅 CLI 入口依赖 node:fs、node:util 与 node:stream/consumers)。
使用任意包管理器安装:
pnpm add @eihrteam/xmlnpm install @eihrteam/xml如需在终端全局使用 xml 命令:
pnpm add -g @eihrteam/xml支持的格式与转换矩阵
转换器在三种序列化格式之间通过统一的内部 DocumentModel 互转:
| 格式 | API 标识符 / 函数前缀 | CLI 参数值 | 说明 |
|---|---|---|---|
| Wiki JSON(展示态) | 'wiki-json' / wikiJson* | json | item/info 接口返回的 InfoRoot 或内层 InfoItem |
| 提交态 JSON | submitJson* | submit-json | item/update 接口使用的SubmitRoot 载荷 |
| 森空岛 Wiki XML | 'xml' / xml* | xml | 根元素为 <sklandDocument> 的结构化文档 |
导出分组速查
主入口 @eihrteam/xml 将导出划分为四组:
- 一步式转换函数:
convert、wikiJsonToXml、xmlToWikiJson、submitJsonToXml、xmlToSubmitJson、wikiJsonToSubmitJson、submitJsonToWikiJson,以及批量函数wikiJsonToXmlBatch和xmlToWikiJsonBatch。 - 底层解析与渲染函数:
parseWikiJson、parseWikiJsonWithWarnings、parseXml、parseXmlWithWarnings、parseSubmitJson、renderXml、renderWikiJson、renderSubmitJson、documentToWikiJsonObject。 - 错误类:
XmlWikiConversionError,转换器自身抛出的结构与校验错误均使用此类型。 - TypeScript 类型:包括
DocumentModel、ChapterGroup、Chapter、Block、Inline、ConvertOptions、RenderWikiJsonOptions、RenderJsonOptions、ConversionResult与批量处理相关类型。
最小使用示例
以下示例演示在 TypeScript 中把 XML 字符串转换为展示态 Wiki JSON 与提交态 JSON:
import { xmlToSubmitJson, xmlToWikiJson } from "@eihrteam/xml";
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<sklandDocument>
<itemId>101</itemId>
<metainfo>
<name>示例条目</name>
<cover showInDetail="false"></cover>
<subTypes></subTypes>
</metainfo>
<description>一段示例简介。</description>
</sklandDocument>
`;
const infoResult = xmlToWikiJson(xml);
const submitResult = xmlToSubmitJson(xml);
console.log(infoResult.text);
console.log(submitResult.warnings);两次调用均返回 { text, warnings } 对象:text 是缩进 4 空格并带结尾换行符的 JSON 字符串,warnings 是非致命提示数组。
另见
convert()与转换函数:查阅全部转换、解析与渲染函数的参数和返回值- 类型与选项参考:查阅
DocumentModel、RenderWikiJsonOptions等接口定义 - CLI 命令行参考:了解
xml convert子命令与管道用法 XmlWikiConversionError:了解错误捕获与类型区分- Language Server Protocol 概览:了解面向编辑器的
@eihrteam/xml-lsp