htmlrubynokogiriunwrap

What's the best way to unwrap tags with nokogiri?


Say, I have a HTML file like so...

<html>
<head>
<title>hello</title>
</head>
<body>
<h1>title</h1>
<p>So, help me <a href="/">remove</a> this.</p>
</body>
</html>

What node method would I use to make it like so?

<html>
<head>
<title>hello</title>
</head>
<body>
<h1>title</h1>
<p>So, help me remove this.</p>
</body>
</html>

There's probably something like...

doc.css('a').each |i|
  i.unwrap
end

but I can't seem to find it in the documentation.


Solution

  • You can use Nokogiri::XML::Node#replace to replace the link tags with the text in them.

    doc.css('a').each do |link|
      link.replace(link.inner_html)
    end