Skip to content

convertDocument

convertDocument(text, options): Promise<ConvertResult>

Defined in: packages/core/src/convert/index.ts:96

Convert document text to YAML, JSON or TOML.

ParameterType
textstring
optionsConvertOptions

Promise<ConvertResult>

The output is the authored document, not the validated one: no defaults are filled in and no sugar is expanded. Any document that parses converts, whether or not it validates; validation says which.

Losses are reported, not fatal: comments (with their lines), each alias and merge key (written out in full), and, going to TOML, a null where leaving the key out means the same thing (a setFilter action’s filter, a GeoJSON feature’s property). The conversion fails, naming the path, for any other null going to TOML, inf or nan going to JSON, and an integer past 2^53. !html converts to {"$html": …} and back, so it is not a loss.

The promise never rejects because of what the text says. It rejects only when a reader chunk cannot be downloaded (a FormatError of code "chunk").

const result = await convertDocument(yamlText, { to: "toml" });
if (result.success) {
for (const loss of result.losses) console.warn(loss.message);
console.log(result.text);
}