Convert HTML to text

Convert HTML content to plain-text format. Filter elements by selector and trim whitespace.

Face with waiting expression Nothing to see yet!

Loading takeymakey...
TakeyMakey code
Want this tool to do something else? Edit the code below and make it do whatever you want.
({
  options: { mode: "iframe" },
  take: [
    { type: "code", label: "HTML" },
    {
      type: "code",
      label: "Filter",
      detail:
        "Elements matching this CSS selector will be removed from the result.",
    },
    { type: "toggle", label: "Trim whitespace" },
  ],
  make: ([html, filter, trim]) => {
    const parser = new DOMParser()
    const result = parser.parseFromString(html, "text/html").documentElement

    if (filter) {
      Array.from(result.querySelectorAll(filter)).forEach((el) => el.remove())
    }

    if (trim) {
      return result.textContent
        .replace(/^ +/gm, "")
        .replace(/\n{3,}/g, "\n\n")
        .trim()
    } else {
      return result.textContent
    }
  },
})