@bgotink/kdl

中文文档
0.4.1发布于 2026/10/14MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 13.3万

Modification-friendly KDL parser and serializer.

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. JSON-in-KDL (JiK)
  4. (De)Serialization
  5. KDL v1
  6. Quirks
  7. Properties
  8. Numbers
  9. License

@bgotink/kdl

This package contains a parser and stringifier for the KDL Document Language, a node-based, human-friendly configuration and serialization format.

The parser in this package focuses on parsing documents in a way that allows for format-preserving modifications. This is most useful when working with KDL files maintained by humans.

Install

yarn add @bgotink/kdl
# or
npm install @bgotink/kdl
# or
pnpm add @bgotink/kdl
# or ...

Usage

The examples below assume a Node.js environment. The @bgotink/kdl package works in any javascript runtime, including browsers, as long as it supports relatively modern browser standards. If you're trying to run these samples in a non-Node.js runtime, replace the import for assert with something equivalent in your runtime.

import {parse, format} from "@bgotink/kdl";
import assert from "node:assert/strict";

const doc = parse(String.raw`
    node "value" #"other value"# 2.0 4 #false \
            #null -0 {
        child; "child too"
    }
`);

doc.nodes[0].children.nodes[0].entries.push(
    parse(
        String.raw`/-lorem="ipsum" \
            dolor=#true`,
        {as: "entry"},
    ),
);

assert.equal(
    format(doc),
    String.raw`
    node "value" #"other value"# 2.0 4 #false \
            #null -0 {
        child /-lorem="ipsum" \
            dolor=#true; "child too"
    }
`,
);

JSON-in-KDL (JiK)

This package exports function from @bgotink/kdl/json to parse and stringify KDL documents as JSON-in-KDL (JiK) 4.0.0. For information on the format, see the JiK 4.0.0 specification.

import {parse} from "@bgotink/kdl/json";
import assert from "node:assert/strict";

assert.deepEqual(
    parse(
        String.raw`
            - {
                prop #false
                otherProp {
                    - 0
                    - 2
                    - 4
                }
            }
        `,
    ),
    {
        prop: false,
        otherProp: [0, 2, 4],
    },
);

There are four functions:

  • parse and stringify turn text into the encoded JSON value and back. These functions are useful for encoding/decoding entire JiK files.
  • toJson and fromJson turn KDL nodes into the encoded JSON value and back. These functions give more fine-grained control over the output, and can be used for e.g. encoding/decoding a JiK node embedded in a KDL document.

(De)Serialization

The package contains utilities for (de)serialization in @bgotink/kdl/dessert.

import {parse} from "@bgotink/kdl/dessert";
import assert from "node:assert/strict";

assert.deepEqual(
    parse(
        String.raw`
            name "my-package"
            
            dependency "@bgotink/kdl" range="^0.2.0"
            dependency "prettier" range="^3" dev=#true
        `,
        (ctx) => {
            const dependencies = {};
            const devDependencies = {};

            const name = ctx.child.single.required("name", (ctx) =>
                ctx.argument.required("string"),
            );

            for (const dependency of ctx.children("dependency", (ctx) => ({
                name: ctx.argument.required("string"),
                range: ctx.property("range", "string") ?? "*",
                dev: ctx.property("dev", "boolean") ?? false,
            }))) {
                (dependency.dev ? devDependencies : dependencies)[dependency.name] =
                    dependency.range;
            }

            return {name, dependencies, devDependencies};
        },
    ),
    {
        name: "my-package",
        dependencies: {
            "@bgotink/kdl": "^0.2.0",
        },
        devDependencies: {
            prettier: "^3",
        },
    },
);

KDL v1

The package exports two functions from @bgotink/kdl/v1-compat to support documents written in KDL v1:

  • parse parses a KDL v1 document and transforms all linked formatting information to turn it into a valid KDL v2 document. If the resulting document is passed to format(), the resulting string will be the same document but in KDL v2 syntax. It will include all comments and formatting applied by the original document's author.
  • parseCompat parses a document that's either KDL v2 or KDL v1 and returns a valid KDL v2 document. This is a helper function that combines the regular parse function with the v1-compat parse function into a single function that supports both formats.

Quirks

This package turns KDL documents into JavaScript objects and vice versa. It is therefore limited by the JavaScript language.

Properties

Multiple properties with the same name are allowed. All duplicated will be preserved, meaning those documents will correctly round-trip. When using node.getProperty()/node.getProperties()/node.getPropertyEntry(), the last property with that name's value will be returned, effectively shadowing any earlier duplicates. Using node.getPropertyEntries()/node.entries does expose the shadowed duplicates, leaving it up to the caller to handle these. Passing the node through clearFormat() removes these shadowed duplicates.

Numbers

JavaScript stores all numbers as 64-bit IEEE 754 floating point numbers. This limits what integer values can be used safely. These limits are lower than you might expect if you're used to working in environments that have a separate 64-bit integer data type.

The original representation of parsed numbers is retained, unless clearFormat is called on the value or any entry/node/document containing the value.

License

This package is licensed under the MIT license, which can be found in LICENSE.md.

The test suite at test/upstream is part of the KDL specification and is available under the Creative Commons Attribution-ShareAlike 4.0 International License.

版本与发布记录

@bgotink/kdl 共发布 30 个版本,首次发布于 2022/06/26;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2026/10/1425 天前
0.5.0-next.1next2026/10/031 个月前
0.5.0-next.02026/10/021 个月前
0.4.02025/11/3011 个月前
0.4.0-next.22025/04/291.5 年前
0.4.0-next.12025/03/081.7 年前

关于 @bgotink/kdl 的常见问题

@bgotink/kdl 是什么?

Modification-friendly KDL parser and serializer.

如何安装 @bgotink/kdl?

在项目目录运行 npm install @bgotink/kdl,或使用 pnpm add / yarn add / bun add。

@bgotink/kdl 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bgotink/kdl 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bgotink/kdl 可以商用吗?

@bgotink/kdl 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bgotink/kdl 的下载量有多少?

根据 npm 官方下载统计,@bgotink/kdl 在 2026/10/08 – 2026/11/06 这 30 天内被下载 132,903 次,日均约 4,430 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bgotink/kdl 还在维护吗?

最新版本 0.4.1 发布于 2026/10/14(25 天前);至今共发布 30 个版本,其中过去 12 个月发布了 4 个;首次发布于 2022/06/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。