When building tools that manipulate YAML files, I would encounter issues where they wouldn't respect the formatting rules. Instead of building another YAML formatter, I wanted to try to include the formatter directly from JS. Using WASM, this tool calls yamlfmt directly with the specified config.
Explain what is needed to run the project
npm i @imoverlord/yamlfmt
import { formatYAML } from "./index.ts"
const yaml = `
app:
versions:
- name: cake
version: 1.0.0
- name: cookie
version: 2.0.0
`
await formatYAML(yaml)
formatYAML
will start the Go wasm if needed and the directly pass the string to yamlfmt go formatter.
You can specify a config if needed. The config should support all options from yamlfmt config.
import { formatYAML } from "./index.ts"
const yaml = `
app:
versions:
- name: cake
version: 1.0.0
- name: cookie
version: 2.0.0
`
await formatYAML(yaml, { indentlessArrays: true })
Name | Type | Default | Description |
---|---|---|---|
indent |
int | 2 | The indentation level in spaces to use for the formatted yaml. |
includeDocumentStart |
bool | false | Include --- at document start. |
lineEnding |
lf or crlf
|
crlf on Windows, lf otherwise |
Parse and write the file with "lf" or "crlf" line endings. This setting will be overwritten by the global line_ending . |
retainLineBreaks |
bool | false | Retain line breaks in formatted yaml. |
retainLineBreaksSingle |
bool | false | (NOTE: Takes precedence over retain_line_breaks ) Retain line breaks in formatted yaml, but only keep a single line in groups of many blank lines. |
disallowAnchors |
bool | false | If true, reject any YAML anchors or aliases found in the document. |
maxLineLength |
int | 0 | Set the maximum line length (see note below). if not set, defaults to 0 which means no limit. |
scanFoldedAsLiteral |
bool | false | Option that will preserve newlines in folded block scalars (blocks that start with > ). |
indentlessArrays |
bool | false | Render - array items (block sequence items) without an increased indent. |
dropMergeTag |
bool | false | Assume that any well formed merge using just a << token will be a merge, and drop the !!merge tag from the formatted result. |
padLineComments |
int | 1 | The number of padding spaces to insert before line comments. |
trimTrailingWhitespace |
bool | false | Trim trailing whitespace from lines. |
eofNewline |
bool | false | Always add a newline at end of file. Useful in the scenario where retainLineBreaks is disabled but the trailing newline is still needed. |
stripDirectives |
bool | false | YAML Directives are not supported by this formatter. This feature will attempt to strip the directives before formatting and put them back. Use this feature at your own risk. |
Table was taken directly from yamlfmt config documentation. The keys were adapted to be typescript friendly camel case instead of pascal case.
This project is licensed under the Apache-2.0 license.
See LICENSE for more information.