Convert HTML to text

Convert HTML code to plain text, stripping out tags but keeping content. Filter out specific elements using CSS selectors 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.
export const 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",
    detail: "Remove spaces from the start and end of every line.",
  },
  {
    type: "toggle",
    label: "Double space",
    detail: "Include two lines between each line of text.",
  },
]

export const make = ([html, filter, trim, double]) => {
  const parser = new DOMParser()
  const document = parser.parseFromString(html, "text/html").documentElement

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

  let result = document.textContent

  if (trim) {
    result = result
      .replace(/^ +| +$/gm, "")
      .replace(/\n{3,}/g, "\n\n")
      .trim()
  }

  if (double) {
    result = result.replace(/\n+/g, "\n\n")
  }

  return result
}

export const options = { mode: "iframe" }