asciidoctor / asciidoctor-kroki

Asciidoctor.js extension to convert diagrams to images using Kroki!
https://kroki.io/
MIT License
151 stars 51 forks source link

Can't resolve relative path for included puml diagrams #303

Closed wouter-veeken closed 2 years ago

wouter-veeken commented 2 years ago

I'm struggling with an issue similar to #288, but using Asciidoctor JS in a Gatsby site.

I'm trying to include a standard "stylesheet" file in every PUML diagram with the kroki-plantuml-include attribute, but I can't figure out what kroki-plantuml-include-paths considers to be my root folder. This is my file/folder structure:

.
├──docs/
   ├──diagrams/
      └──stylesheet.puml
      └──diagram.puml
   └──productName/
      └──document.adoc
├──site/
      └──gatsby-config.js

In gatsby-config.js, I've defined this:

        attributes: {
          ...
          "kroki-plantuml-include-paths": "./docs/diagrams/",
          "kroki-plantuml-include": "puml-stylesheet.puml"
          ...
        },

But I keep getting:

warn Skipping preprocessing of PlantUML include, because reading the referenced local file 'puml-stylesheet.puml' caused an error:
Error: ENOENT: no such file or directory, open 'puml-stylesheet.puml'

I've verified that it works with an absolute path (i.e. /Users/myName/git_tree/etc) but none of the relative paths I've tried work.

Any guidance? Thanks in advance!

ggrossetie commented 2 years ago

Could you please share a repository that reproduces this issue?

Since you are using a relative path (i.e., !include ./docs/diagrams/puml-stylesheet.puml), Node.js will use the current working directory to resolve this relative path.

My guess is that the working directory when running the gatsby command to generate your site is not the root of your project. Hence, ./docs/diagrams/puml-stylesheet.puml is not correctly resolved.

You can debug/add console.log statements in this function to troubleshoot:

https://github.com/Mogztter/asciidoctor-kroki/blob/86340a2f0665999baa0101230890322c33c5881c/src/preprocess.js#L175-L186

You can use process.cwd() to get the current working directory of your process (Gatsby).

wouter-veeken commented 2 years ago

Could you please share a repository that reproduces this issue?

I'm afraid this is in a private repo, sorry :/

I'm going to try the logging you suggested, thanks!

wouter-veeken commented 2 years ago

@Mogztter your assumption was correct: I run Gatsby from /site so that's obviously considered the root folder. Makes perfect sense in hindsight, of course. Changing the path to ../docs/diagrams/ has the intended outcome. 👍 Many thanks for the help!

ggrossetie commented 2 years ago

You're welcome 😊