filterHtmlOnly function

String filterHtmlOnly(
  1. String html, {
  2. List<String>? unwantedSelectors,
})

Implementation

String filterHtmlOnly(String html, {List<String>? unwantedSelectors}) {
  try {
    final document = html_parser.parse(html);

    // Remove unwanted elements
    final selectors = unwantedSelectors ??
        ['script', 'style', 'iframe', 'noscript', 'link', 'meta'];
    for (final selector in selectors) {
      document.querySelectorAll(selector).forEach((element) {
        element.remove();
      });
    }

    return document.outerHtml;
  } catch (e) {
    return html;
  }
}