# 转为 kebab-case

> 逐行转为 kebab-case，在空格、连字符、下划线和大小写变化处分词

版本：1.0.0 · 作者 Deck

- 安装到 Deck：deck://install-plugin?id=to-kebab-case&url=https%3A%2F%2Fgithub.com%2Fyuzeguitarist%2Fdeck-plugins%2Freleases%2Fdownload%2Fplugins%2Fto-kebab-case-1.0.0.zip
- 下载 .zip：https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/to-kebab-case-1.0.0.zip
- 在 GitHub 上查看：https://github.com/yuzeguitarist/deck-plugins/tree/main/plugins/to-kebab-case

也可以用终端安装：

```sh
mkdir -p ~/.deck/scripts/to-kebab-case && curl -fsSL https://github.com/yuzeguitarist/deck-plugins/releases/download/plugins/to-kebab-case-1.0.0.zip | tar -xf - -C ~/.deck/scripts/to-kebab-case
```

## 脚本

```js
// Converts each line to kebab-case, splitting words at spaces, hyphens, underscores, other punctuation and case changes.
function words(line) {
  const result = [];
  const runs = line.replace(/['\u2019]/g, "").match(/[\p{L}\p{M}\p{N}]+/gu) ?? [];
  for (const run of runs) {
    const spaced = run.replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1 $2").replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2");
    for (const word of spaced.split(" ")) result.push(word.toLowerCase());
  }
  return result;
}

// A line without letters or digits stays as it is.
function convert(line) {
  const list = words(line);
  return list.length === 0 ? line : list.join("-");
}

function transform(input) {
  return String(input ?? "").split(/(\r\n|\r|\n)/).map((part, k) => (k % 2 === 0 ? convert(part) : part)).join("");
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/to-kebab-case
- English：https://deckclip.app/plugins/to-kebab-case.md

© 2026 潘禹泽
