prolificinteractive / node-html-to-json

Parses HTML strings into objects using flexible, composable filters.
MIT License
120 stars 13 forks source link

Return nested elements as array #6

Open leandroz opened 7 years ago

leandroz commented 7 years ago

Hi!, what I am trying to implement is parsing a ul that can have deeper levels like:

<ul>
  <li><a>Title Page</a></li>
  <li><a>Copyright Page</a></li>
  <li><a>Dedication Page</a></li>
  <li>
    <a>Preface: About this edition</a>
    <ul>
      <li><a>#1. Let’s face it: It’s old</a></li>
      <li><a>#2. The world has changed</a></li>
      <li><a>Don’t get me wrong...</a></li>
    </ul>
  </li>
   <li><a>Index</a></li>
</ul>

The idea is to get something like:

{
  text: '',
  href: '',
  children: [
    {
      text: '',
      href: '',
      children: []
    }
  ]
}

Any idea how to apply recursion?