asciidoctor.js icon indicating copy to clipboard operation
asciidoctor.js copied to clipboard

Optional include inside an include file raises an `Unresolved directive`

Open someth2say opened this issue 3 years ago • 3 comments

Say we have a root file with an include directive:

Root

include::test.adoc[]

Then, the test file:

Test

include::test2.adoc[opts=optional]

The expected result was just the text from both files (Root and Test), but I get a Unresolved directive error: image

Given the second include is optional, the error should not appear.

someth2say avatar Feb 07 '22 11:02 someth2say

This feature is not implemented in a browser because it relies on File.file? which is not available in a browser environment: https://github.com/asciidoctor/asciidoctor/blob/4d849f85a7e2684c0553c8d9082c5b82ead2cfbc/lib/asciidoctor/reader.rb#L1236-L1245

In other words, we cannot check that the file exists without doing an XMLHttpRequest. I guess we could reverse the logic and perform an XMLHttpRequest only if optional-option is present since we are already monkey-patching the resolve_include_path: https://github.com/asciidoctor/asciidoctor.js/blob/bb39bb29d10ac703090fe237d05203b0a1dcf173/packages/core/lib/asciidoctor/js/asciidoctor_ext/browser/reader.rb#L3-L39

ggrossetie avatar Feb 07 '22 13:02 ggrossetie

Another idea is to catch the exception when the request for the file fails and suppress the behavior if the optional option is set. I haven't looked to see if there is an opportunity in the code to handle it that way. That's just the idea in theory.

mojavelinux avatar Feb 07 '22 19:02 mojavelinux

Another idea is to catch the exception when the request for the file fails and suppress the behavior if the optional option is set. I haven't looked to see if there is an opportunity in the code to handle it that way.

Technically it's possible but not practical since we would need to monkey patch at different locations:

  • https://github.com/asciidoctor/asciidoctor/blob/1cfe82949c2634e4efa145c23be3014711575708/lib/asciidoctor/reader.rb#L1106-L1107
  • https://github.com/asciidoctor/asciidoctor/blob/1cfe82949c2634e4efa145c23be3014711575708/lib/asciidoctor/reader.rb#L1170-L1171
  • https://github.com/asciidoctor/asciidoctor/blob/1cfe82949c2634e4efa145c23be3014711575708/lib/asciidoctor/reader.rb#L1194-L1195

Asciidoctor eagerly check that the file exists (using File.file? in resolve_include_path) to avoid additional processing. We could probably send a HEAD request if optional-option is present... in my opinion that's the lesser evil 😃

ggrossetie avatar Feb 08 '22 19:02 ggrossetie