# 合并为一行

> 将所有行合并为一行，以空格隔开并跳过空行，中文之间不加空格

版本：1.0.0 · 作者 Deck

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

也可以用终端安装：

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

## 脚本

```js
// Joins all lines into one, separated by single spaces and skipping empty lines; Chinese and Japanese join without spaces.

// Han ideographs, kana and bopomofo. Hangul is left out: Korean separates words with spaces.
const CJK = /[\u2e80-\u2fdf\u3005-\u3007\u3040-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3100-\u312f\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\u{20000}-\u{3ffff}]/u;
// CJK punctuation and full-width forms.
const FULL_WIDTH = /[\u3000-\u303f\ufe30-\ufe4f\uff00-\uffef]/u;
const LATIN_OR_DIGIT = /[0-9A-Za-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u024f\u1e00-\u1eff]/u;

// No space between two Chinese or Japanese characters or next to full-width punctuation. A Chinese or Japanese
// character next to a Latin letter or digit gets a space, as everything else does.
function separator(before, after) {
  const last = Array.from(before.slice(-2)).pop();
  const first = Array.from(after.slice(0, 2))[0];
  if (!CJK.test(last) && !CJK.test(first) && !FULL_WIDTH.test(last) && !FULL_WIDTH.test(first)) return " ";
  return (CJK.test(last) && LATIN_OR_DIGIT.test(first)) || (LATIN_OR_DIGIT.test(last) && CJK.test(first)) ? " " : "";
}

function transform(input) {
  const lines = [];
  for (const line of String(input ?? "").split(/\r\n|\r|\n/)) {
    let start = 0;
    let end = line.length;
    while (start < end && (line[start] === " " || line[start] === "\t")) start += 1;
    while (end > start && (line[end - 1] === " " || line[end - 1] === "\t")) end -= 1;
    if (end > start) lines.push(line.slice(start, end));
  }
  return lines.map((line, index) => (index === 0 ? line : separator(lines[index - 1], line) + line)).join("");
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/join-lines
- English：https://deckclip.app/plugins/join-lines.md

© 2026 潘禹泽
