API

Parsing and linting

parse(), lint() and format() from @zplkit/core, and what they return.

import { parse, lint, format } from "@zplkit/core";

Parsing ZPL and Linting explain how to use them, and every rule.

parse

parse(zpl: string): ParseResult

Reads ZPL into command nodes. Supported commands become typed nodes, and everything else is kept as Raw nodes, so no input is lost. Returns a ParseResult:

PropertyTypeDescription
commandsCommand[]

Typed nodes for supported commands, Raw nodes for everything else

diagnosticsDiagnostic[]

Parameters that couldn't be read, and text the printer skips

Diagnostic

PropertyTypeDescription
severity"warning"

Parse problems are always warnings: the printer still prints

rule"ignored-text" | "invalid-parameter"

A parameter that couldn't be read, or text the printer skips

messagestring

What's wrong, in a sentence

spanSpan

Where in the text, as character offsets

lint

lint(commands: readonly Command[], media?: LintMedia): LintDiagnostic[]

Finds problems the printer wouldn't report in commands from parse() or label.commands, and returns a list of LintDiagnostics. media is the printer's media size, for labels that don't set ^PW or ^LL:

PropertyTypeDescription
width?number

Media width in dots

length?number

Media length in dots

LintDiagnostic

PropertyTypeDescription
ruleLintRule

Which check found it; the linting docs explain each one

severity"error" | "warning"

Errors stop the label from printing as intended, warnings change how it prints

messagestring

What's wrong, in a sentence

indexnumber

Position of the command in the list

span?Span

Position in the source text, for parsed ZPL

serialize

serialize(commands: Command[], options?: { minify?: boolean }): string

Turns command nodes back into ZPL. Pass { minify: false } to put each command on its own line.

format

format(zpl: string): string

Lays out ZPL to read: one field or setting per line, and a blank line between labels. Unlike serialize(), it keeps the ZPL as written, only moving line breaks, so the labels print the same.

format("^XA^PW406^FO20,20^A0N,40^FDHi^FS^XZ");
// ^XA
// ^PW406
// ^FO20,20^A0N,40^FDHi^FS
// ^XZ