I’m trying to make backlinks. Ideally I would just query every link in the document and filter based on the target, but links are not locatable yet. So, the approach I’ve taken is to create a label on every link. To ensure that multiple labels are not created with the same name, I increment a counter which is used in their naming.
This is my attempt.
#let prefix = "test:"
#let generate(it) = {
if type(it.dest) != label {
return it
}
let target = str(it.dest)
let backlink_count = counter(prefix + target)
context {
let here = label(
prefix + target + ":" + str(backlink_count.get().first())
)
return [
#it
#here
#backlink_count.step()
]
}
}
#let get(lab) = {
let target = str(lab)
let backlink_count = counter(prefix + target).final().first()
return range(backlink_count).map(
i => label(prefix + target + ":" + str(i))
)
}
#show link: generate
Test <label1>
#link(<label1>)[Go to label1]
#context link(get(<label1>).first())[Backlink] // This errors
This errors with label `<test:label1:0>` does not exist in the document. Why? Can I fix this?
You’ve queried for <test:link0> here instead of link0, which causes the result to be empty.
Aside from that, attaching a label to it in a show rule sadly doesn’t work. The element is already “finalized” so to speak before the show rule runs. If you need to do it in a show rule, you can add the label to some metadata that you generate in the show rule instead.
You say that the link element is already “finalized”, so adding a label doesn’t work. Does that mean that labels aren’t content elements? Are they just applied as a property to whatever comes before it?