# convert() 与转换函数 `@eihrteam/xml` 提供了一组高层一步式转换函数(覆盖展示态 Wiki JSON、提交态 JSON 与 XML 的全排列互转)以及对应的批量函数与底层解析/渲染函数。所有函数均为同步执行,并在检测到结构或数据非法时立即抛出异常。 ## 试一试 在互动转换器中直接运行 `xmlToWikiJson`、`wikiJsonToXml` 或 `xmlToSubmitJson` 查看输出: \n\n 301\n \n API 转换测试\n \n \n \n 使用 @eihrteam/xml 进行本地双向转换。\n" /> ## 语法 按通用转换、双向专有转换、批量转换和底层解析/渲染四组列出完整签名。 ### 通用转换函数 `convert()` ```ts function convert( source: string | object, options: ConvertOptions, ): ConversionResult ``` #### 参数 - `source`(`string | object`):输入源。当 `options.from` 为 `'wiki-json'` 时接受 JSON 字符串或已解析的对象;当 `options.from` 为 `'xml'` 时必须是字符串。 - `options`(`ConvertOptions`):转换配置对象,包含必填的 `from` 和 `to`(均为 `'wiki-json' | 'xml'`),以及可选的 `RenderWikiJsonOptions` 字段(`wrapInfoRoot`、`envelope`、`referenceJson`)。 #### 返回值 返回 `ConversionResult` 对象(`{ text: string; warnings: string[] }`)。当 `from === to` 时不执行解析,直接返回原样字符串(或 4 空格缩进的 JSON 字符串)与空 `warnings` 数组。 #### 异常 - `TypeError`:当 `from === 'xml'` 但 `source` 不是字符串,或 `from`/`to` 不是 `'wiki-json' | 'xml'` 时抛出。 - `XmlWikiConversionError`:当输入 XML 或 JSON 违反语法、结构或字段规则时抛出。 ### 一步式专有转换函数 ```ts function wikiJsonToXml(json: string | object): ConversionResult function xmlToWikiJson(xml: string, options?: RenderWikiJsonOptions): ConversionResult function submitJsonToXml(json: string | object): ConversionResult function xmlToSubmitJson(xml: string, options?: RenderJsonOptions): ConversionResult function wikiJsonToSubmitJson(json: string | object, options?: RenderJsonOptions): ConversionResult function submitJsonToWikiJson(json: string | object, options?: RenderWikiJsonOptions): ConversionResult ``` #### 参数 - `json`(`string | object`):展示态 `InfoRoot`/`InfoItem` 或提交态 `SubmitRoot` 的 JSON 文本或对象。 - `xml`(`string`):森空岛 Wiki XML 文档字符串。 - `options`(`RenderWikiJsonOptions` 或 `RenderJsonOptions`,可选):渲染选项,详见[类型与选项参考](../types-and-constants/)。 #### 返回值 所有函数均返回 `ConversionResult`(`{ text: string; warnings: string[] }`)。 #### 异常 输入不合法时抛出 `XmlWikiConversionError`;如果底层输入不是合法 JSON 文本,也会被包装或以 `XmlWikiConversionError` 抛出。 ### 批量转换函数 ```ts function wikiJsonToXmlBatch( entries: WikiJsonToXmlBatchEntry[], ): WikiJsonToXmlBatchResult function xmlToWikiJsonBatch( entries: ConversionBatchEntry[], options?: RenderWikiJsonOptions, ): ConversionBatchResult ``` #### 参数 - `entries`:待转换条目数组,每项包含 `source` 和可选的透传元信息 `meta`。 - `options`(仅 `xmlToWikiJsonBatch`,可选):应用于批次内全部条目的 `RenderWikiJsonOptions`。 #### 返回值 返回 `{ items, warnings }`:`items` 与输入数组一一对应,包含每个条目的 `text`、`warnings` 以及(若输入提供了 `meta`)原始 `meta`;顶层 `warnings` 汇总所有条目的警告,并在每条消息前添加 `entry : ` 前缀。 #### 异常 批量函数采用**遇错即止(fail-fast)**策略:任意一条转换抛出错误时,立即抛出普通 `Error`,消息格式为 `wikiJsonToXmlBatch entry failed: ` 或 `xmlToWikiJsonBatch entry failed: `。 ### 底层解析与渲染函数 当调用方希望直接读写中间态 `DocumentModel`(例如批量修改章节标题、统计元素数量)时,可使用底层分离函数: ```ts function parseWikiJson(source: string | object): DocumentModel function parseWikiJsonWithWarnings(source: string | object): [DocumentModel, string[]] function parseXml(source: string): DocumentModel function parseXmlWithWarnings(source: string): [DocumentModel, string[]] function parseSubmitJson(source: string | object): [DocumentModel, string[]] function renderXml(document: DocumentModel): string function renderWikiJson(document: DocumentModel, options?: RenderWikiJsonOptions): string function renderSubmitJson(document: DocumentModel, options?: RenderJsonOptions): string function documentToWikiJsonObject( document: DocumentModel, options?: RenderWikiJsonOptions, ): Record ``` - `parseWikiJson` 与 `parseXml` 直接返回 `DocumentModel`(丢弃 warnings);`*WithWarnings` 与 `parseSubmitJson` 返回元组 `[DocumentModel, string[]]`。 - `documentToWikiJsonObject` 返回尚未序列化为字符串的展示态 JSON 对象,适合需要继续拼装响应体的调用方。 ## 描述 转换函数全部围绕 `DocumentModel` 工作,具备以下共性行为: - **确定性 ID 与 ID 复用**:未传 `referenceJson` 时,渲染器根据文档内容确定性地生成组件、块和节点 ID;传入 `referenceJson` 后,相同内容的既有 ID 会被优先复用,从而最小化提交差异。 - **同格式短路**:仅 `convert()` 在 `from === to` 时跳过解析直接原样返回;不会对输入做任何合法性校验。 - **告警通道**:`warnings` 目前主要承载解析阶段遇到的非致命兼容提示,不会阻止转换完成。 ## 示例 展示两种常见场景:带 `referenceJson` 的安全往返渲染,以及附带业务 `meta` 的批量转换。 ### 带 `referenceJson` 生成提交态 JSON ```ts import { submitJsonToXml, xmlToSubmitJson } from "@eihrteam/xml"; const originalSubmit = loadSubmitJsonFromDisk(); const { text: xml } = submitJsonToXml(originalSubmit); // 修改条目标题后转回提交态 JSON,并复用原有组件与块 ID const updatedXml = xml.replace("旧标题", "新标题"); const { text: nextSubmit } = xmlToSubmitJson(updatedXml, { referenceJson: originalSubmit, }); ``` ### 使用 `wikiJsonToXmlBatch()` 批量转换并保留条目元信息 ```ts import { wikiJsonToXmlBatch } from "@eihrteam/xml"; const batch = wikiJsonToXmlBatch([ { source: itemJsonA, meta: { slug: "operator-a" } }, { source: itemJsonB, meta: { slug: "operator-b" } }, ]); for (const item of batch.items) { console.log(item.meta?.slug, item.text.length); } ``` 如果其中第二条数据有误,函数会抛出 `Error("wikiJsonToXmlBatch entry 1 failed: ...")`,调用方无需手动遍历即可定位失败索引。 ## 另见 - [类型与选项参考](../types-and-constants/):查阅 `ConvertOptions`、`RenderWikiJsonOptions` 与 `DocumentModel` - [`XmlWikiConversionError`](../xml-wiki-conversion-error/):了解错误捕获与诊断 - [API、批量与杂项错误](../../errors/api-and-batch/):排查 `convert()` 与批量转换异常 - [从 JSON 迁移到 XML](../../guides/migrate-from-json/):查看 `referenceJson` 的端到端实践