Remove attributes from HTML

Hello dear n8n community,

Is there a node, that would allow me to remove attributes from HTML?
Example:

<p class="c4">And some more text</p><p class="c1"></p><h1 class="c8" id="h.4wsj9miliye9">More text</h1><h2 class="c7" id="h.nl7fh4vyf8nf">And even more text</h2>

Would become:
<p>And some more text</p><p></p><h1>More text</h1><h2>And even more text</h2>

Alternatively, is there a way to use DOM selectors to remove the attributes in a function?

Thank you,
B.

Hi @Boris_Idesman, there is no existing node for this, but assuming your HTML is relatively simple you could use two Markdown nodes for the job. Check out this example:

The first node creates markdown without classes or any other attributes:

The second node converts the markdown back into HTML:

Hope this helps!

1 Like

Hello @MutedJam ,

Thank you for the solution - it works!
I also found another way, described in the forum here, as you are making use of cheerio:

const cheerio = require('cheerio');
for (const item of $input.all()) {
  var $ = cheerio.load(item.json.body);
  $('*').each(function() {      // iterate over all elements
      this.attribs = {};     // remove all attributes
  });
  //console.log($.html());
  item.json.body = $.html();
}
return $input.all();
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.