asciidoctor / asciidoctor-intellij-plugin

AsciiDoc plugin for products on the IntelliJ platform (IDEA, RubyMine, etc)
https://intellij-asciidoc-plugin.ahus1.de/
Apache License 2.0
342 stars 145 forks source link

Resolve Antora's resource identifier in the Kroki extension #516

Open ggrossetie opened 4 years ago

ggrossetie commented 4 years ago

The Asciidoctor Kroki (JavaScript) extension can resolve Antora's resource identifier using the content catalog object:

const target = contentCatalog.resolveResource(resourceId, file.src)
return target ? target.contents.toString() : fs.readFileSync(resourceId, format)

For instance, you can use the partial$ prefix in the target of the plantuml macro:

= Antora x Kroki

plantuml::partial$ab.puml[target=ab-embedded-em1]

It's working because when the extension is registered, Antora passes a "context" that contains the content catalog:

module.exports.register = function register (registry, context = {}) {
  if (typeof context.contentCatalog !== 'undefined' && typeof context.contentCatalog.addFile === 'function' && typeof context.file !== 'undefined') {
    // -> will use Antora content catalog to resolve resources
  }
  // ...
  return registry
}

In the Intellij plugin, we cannot use the Antora's content catalog but we could pass a "context" that contains a similar method to resolve resources?

module AsciidoctorExtensions
  module Kroki
    def register(registry, context)
      if context && context.resolve_resource
        # -> will use resolve_resource method to resolve resources
      end
      # ...
    end
  end
end

I'm not entirely sure about AsciidoctorJ/JRuby can call this "register" method... What do you think?

Currently, the above AsciiDoc document produces an error:

yyy

ahus1 commented 4 years ago

I am not familiar with the context and how this is solved by Antora. But I can describe how I solved this for Antora style images for the Antora support of the plugin. This might not be elegant, but works for now.

The class AntoraReferenceAdapter takes an Antora style reference, resolves it and replaces it with the regular reference.

Then html5-antora.rb uses prepend to monkey-patch the HTML5 output. There is a similar file for the PDF output, provided resolved images for classic asciidoctor-pdf.

Do you want to try something similar for Kroki?

ggrossetie commented 4 years ago

I am not familiar with the context and how this is solved by Antora. But I can describe how I solved this for Antora style images for the Antora support of the plugin. This might not be elegant, but works for now. The class AntoraReferenceAdapter takes an Antora style reference, resolves it and replaces it with the regular reference.

Thanks for the explanation, I will take a look.

Do you want to try something similar for Kroki?

I was thinking about extracting the logic of resolving an Antora style reference to a "workspace" reference (from AntoraReferenceAdapter) and then somehow pass it to the Kroki extension. I think it makes more sense to keep the logic in the Intellij plugin rather than duplicating it in the Kroki extension. But that's something we will need to explore because I don't know if what I'm describing can actually work. Anyway, it's good to know that we have an alternative if this is not possible.

ahus1 commented 4 years ago

Looking at the code I have written there, I have to admit it didn't get all the love it deserved in terms of documentation and tests. If you know more about the API you need, I'll be happy to lend a hand in extracting/refactoring it as needed.

mcbeelen commented 1 year ago

Hi,

Running into the same problem as reported by @ggrossetie .

I read the comments and did see a mention on an alternative. Is there an alternative available or is still issue still pending the refactoring of the AntoraReferenceAdapter?

unable to render AsciiDoc document

org.asciidoctor.jruby.internal.AsciidoctorCoreException:
  org.jruby.exceptions.SystemCallError: (ENOENT) No such file or directory 
    - PROJECT_DIR/docs/modules/ROOT/pages/:partial$chart.vlite