Fix get menu with non sequential headings

This patch fixes an issue arising when e.g. an ``<h3>`` follows an ``<h1>`` immediately – that is, without an ``<h2>`` in-between.