# 去除 HTML 标签

> 删除 HTML 标签和注释，只保留文字，<br> 和段落等块级元素处换行

版本：1.0.0 · 作者 Deck

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

也可以用终端安装：

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

## 脚本

```js
// Removes HTML tags and comments and keeps the text, with line breaks for <br> and block elements such as <p> and <li>.
const BLOCKS = new Set(("address article aside blockquote caption center dd details dialog div dl dt fieldset figcaption " +
  "figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr legend li main menu nav ol p pre section summary table tbody " +
  "tfoot thead tr ul").split(" "));
// Elements whose content a browser does not show, with the pattern that finds their end tag.
const HIDDEN = new Map(["head", "noscript", "script", "style", "template"].map((name) => [
  name, new RegExp("</" + name + "[\\t\\n\\f\\r />]", "gi"),
]));
const BASIC_ENTITIES = { "&lt;": "<", "&gt;": ">", "&amp;": "&", "&quot;": '"', "&apos;": "'", "&#39;": "'" };

function isSpace(c) {
  return c === " " || c === "\t" || c === "\n" || c === "\r" || c === "\f";
}

function isLetter(c) {
  return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z");
}

// Returns the index after the > that ends a tag, skipping quoted attribute values, or -1 when the tag never ends.
function tagEnd(text, from) {
  let i = from;
  while (i < text.length && text[i] !== ">") {
    if (text[i] !== "=") {
      i += 1;
      continue;
    }
    i += 1;
    while (i < text.length && isSpace(text[i])) i += 1;
    if (text[i] === '"' || text[i] === "'") {
      const close = text.indexOf(text[i], i + 1);
      if (close === -1) return -1;
      i = close + 1;
    }
  }
  return i < text.length ? i + 1 : -1;
}

function transform(input) {
  const text = String(input ?? "");
  if (text.indexOf("<") === -1) return text;
  const out = [];
  let breaks = 0;
  let space = false;
  let started = false;
  let pre = 0;
  let cells = 0;
  let removed = 0;
  const unclosed = new Set();

  // Line breaks and spaces are owed until the next text, so none appear at the start or the end.
  const write = (chunk) => {
    if (started && breaks > 0) out.push("\n".repeat(breaks));
    else if (started && space) out.push(" ");
    out.push(chunk);
    started = true;
    breaks = 0;
    space = false;
  };
  const addText = (chunk) => {
    if (pre > 0) {
      if (chunk !== "") write(chunk.replace(/\r\n?/g, "\n"));
      return;
    }
    const words = chunk.split(/[ \t\n\r\f]+/);
    for (let k = 0; k < words.length; k += 1) {
      if (k > 0) space = true;
      if (words[k] !== "") write(words[k]);
    }
  };

  let textStart = 0;
  let i = 0;
  for (;;) {
    const open = text.indexOf("<", i);
    if (open === -1) break;
    let end;
    let name = "";
    let closing = false;
    if (text.startsWith("<!--", open)) {
      end = text.indexOf("-->", open + 4);
      if (end !== -1) end += 3;
    } else if (text[open + 1] === "!" || text[open + 1] === "?") {
      end = text.indexOf(">", open + 2);
      if (end !== -1) end += 1;
    } else {
      closing = text[open + 1] === "/";
      const nameStart = open + (closing ? 2 : 1);
      if (!isLetter(text[nameStart] ?? "")) {
        i = open + 1;
        continue;
      }
      let nameEnd = nameStart;
      while (nameEnd < text.length && !isSpace(text[nameEnd]) && text[nameEnd] !== "/" && text[nameEnd] !== ">") nameEnd += 1;
      name = text.slice(nameStart, nameEnd).toLowerCase();
      end = tagEnd(text, nameEnd);
    }
    // A tag or comment that never ends is text, and so is everything after it.
    if (end === -1) break;
    addText(text.slice(textStart, open));
    removed += 1;
    i = textStart = end;
    if (name === "br") {
      breaks += 1;
    } else if (name === "td" || name === "th") {
      if (!closing && cells > 0) {
        space = false;
        write("\t");
      }
      if (!closing) cells += 1;
    } else if (BLOCKS.has(name)) {
      breaks = Math.max(breaks, name === "p" ? 2 : 1);
      if (name === "tr" || name === "table") cells = 0;
      if (name === "pre" && closing) pre = Math.max(0, pre - 1);
      if (name === "pre" && !closing) {
        pre += 1;
        if (text[end] === "\n") i = textStart = end + 1;
        else if (text.startsWith("\r\n", end)) i = textStart = end + 2;
      }
    } else if (HIDDEN.has(name) && !closing && !unclosed.has(name)) {
      const pattern = HIDDEN.get(name);
      pattern.lastIndex = end;
      const match = pattern.exec(text);
      if (match === null) unclosed.add(name);
      else i = textStart = match.index;
    }
  }
  addText(text.slice(textStart));
  if (removed === 0) return text;
  return out.join("").replace(/&(?:lt|gt|amp|quot|apos|#39);/g, (entity) => BASIC_ENTITIES[entity]);
}
```

---

- HTML：https://deckclip.app/zh-cn/plugins/strip-html-tags
- English：https://deckclip.app/plugins/strip-html-tags.md

© 2026 潘禹泽
