Forum How do I...?

Index from hidden terms

cayhorstmann
My book has index terms in spans like this:
<p>
<span class='indexterm' ><span class='primary'>Collections</span><span class='secondary'>interfaces for</span></span> 
<span class='indexterm'><span class='primary'><code>Vector</code></span>
...</p>

In the book, I hide them with
.indexterm {
    display: none;
}

I don't expect a fully automated solution from Prince. Just logging them with page numbers is good enough. I had a look at several JavaScript examples, but I am unsure how I would get those page numbers. Or rather, the page numbers of their parents since they are not being displayed.Thanks for any hints!
ldbeth
Hi, there is a sample about creating index for books on https://css4.pub/2022/indexes/ and the javascript used in that example is available at https://princexml.com/howcome/2021/guides/charms.js
cayhorstmann
I am trying the simplest thing, but I cannot get page numbers. getPrinceBoxes always returns an array of length 0.

Prince.registerPostLayoutFunc(() => {
  var is = document.querySelectorAll(".indexterm")
  for (var i = 0; i < is.length - 1; i++) {
    console.log(is[i].textContent)
    console.log(is[i].parentNode.textContent)
    var boxes = is[i].parentNode.getPrinceBoxes()
    console.log(boxes.length)
    for (var j = 0; j < boxes.length; j++) 
      console.log(boxes[j])
  }
})


I see lots of matches for the selector. Since it is not displayed, I look at the boxes of the parent. But no dice, erm, boxes. All the boxes.length return 0.

Edited by cayhorstmann

cayhorstmann
Never mind--I just realized I need to set

Prince.trackBoxes = true;