asciidoctor/asciidoctor.js

doc.findBy doesn't seem to be able to look inside tables

gdams opened this issue · 5 comments

gdams commented

I'm trying to use the findBy function to relocate my images before publishing, the code I have works great but when there is an image inside a table it doesn't seem to work, any help would be most appreciated!

const asciidoctor = require('asciidoctor')()

const adoc = `
image::sample1.png[]

|===
| |
a| image::sample2.png[]
a| image::sample3.png[]
|===
`;

const doc = asciidoctor.load(adoc)
const images = doc.findBy({ context: 'image' })

images.forEach(image => {
  const imagePath = image.getAttribute('target')
  console.log(imagePath) // will only log sample.1.png
})

The above example has 3 images but the doc.findBy function will only return the one that's not in a table.

gdams commented

You need to enable this using the :traverse_documents option. See https://github.com/asciidoctor/asciidoctor/blob/25f6b7c9081ee8e6ef0aaad6e338ba29caadf057/test/api_test.rb#L694-L717

Is that a setting I add to the asciidoc header or the findBy function?

The findBy function. In Asciidoctor.js, the key will be a string (since JavaScript doesn't support Ruby symbols).

gdams commented

yup, it seems to work using:

const images = doc.findBy({ context: 'image', traverse_documents: true })

I have another example that not being found though if you can also help me with that?

[%autowidth,cols="d,l",options="header",stripes=none]
|===
^.^|Button ^.^|Code

^.^|
image:button_img_002.png[Get Eclipse Temurin,width=120,height=120]
<.^a|
[source,html]
----
<a href="https://adoptium.net/temurin?utm_medium=button">
  <img src="https://adoptium.net/images/button_img_002.png"
     alt="Get Eclipse Temurin" width="120" height="120" />
</a>
----
|===

findBy only looks for blocks, not inlines.

In the future, please direct questions to the project chat at https://chat.asciidoctor.org. That's where we can discuss use cases like this.