Query in current document

I use the bundle feature to build a website. I want to include an outline on every page, that links to the different headers on the current document. Is there a way to do that?

This document:

#document("page1.html")[
  #outline()
  = Page 1, header 1
  = Page 1, header 2
]
#document("page2.html")[
  = Page 2, header 1
  = Page 2, header 2
]

Compiles to:

<nav>
  <h2>Contents</h2>
  <ol style="list-style-type: none">
    <li><a href="#loc-1">Page 1, header 1</a></li>
    <li><a href="#loc-2">Page 1, header 2</a></li>
    <li><a href="page2.html#loc-1">Page 2, header 1</a></li>
    <li><a href="page2.html#loc-2">Page 2, header 2</a></li>
  </ol>
</nav>
<h2 id="loc-1">Page 1, header 1</h2>
<h2 id="loc-2">Page 1, header 2</h2>

Where I would have wanted:

<nav>
  <h2>Contents</h2>
  <ol style="list-style-type: none">
    <li><a href="#loc-1">Page 1, header 1</a></li>
    <li><a href="#loc-2">Page 1, header 2</a></li>
  </ol>
</nav>
<h2 id="loc-1">Page 1, header 1</h2>
<h2 id="loc-2">Page 1, header 2</h2>

I would need something like

#context outline(target: selector(heading).within(selector(document).parent(here())))

But selectors don’t have anything like a parent() method.

A workaround is to wrap the whole document in a context, and use here() in the outline’s target selector. It is somewhat fragile though, because there cannot be another context as the outline’s parents.

#document("page1.html", context [
  #outline(target: selector(heading).within(here()))
= Page 1, header 1
= Page 1, header 2
]) // works
#document("page2.html", context [
  #context outline(target: selector(heading).within(here()))
= Page 2, header 1
= Page 2, header 2
]) // doesn't work (outline is empty)