Skip to content

addYamlTagDirective

addYamlTagDirective(source, handle, prefix): string

Defined in: packages/format-yaml/src/add-tag-directive.ts:24

Ensures every document in source declares handle as a %TAG prefix, returning the updated source.

A %TAG directive is per-document and must precede that document’s ---, so a handle cannot be injected once for a multi-document stream: each document is inspected and patched on its own. A document already declaring the same handle, or the same prefix under a different handle, is left alone.

Existing document boundaries, directives and comments are preserved; a ... end marker is inserted where the YAML grammar requires one before a directive that follows another document. An empty source yields a single empty document carrying the directive.

Linear in the token count of source, over one yaml.Parser pass.

string

YAML source text.

string

Tag handle to ensure, e.g. "!inv!".

string

Tag prefix the handle expands to, e.g. "tag:example.com,2024:".

string