gtudan / asciidoctor-kroki

Asciidoctor.js extension to convert diagrams to images using Kroki!

Home Page:https://kroki.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🖍 Asciidoctor Kroki Extension

Build status npm version Gitter

An extension for Asciidoctor.js to convert diagrams to images using Kroki!

Install

Node.js

Install the dependencies:

$ npm i asciidoctor asciidoctor-kroki

Create a file named kroki.js with following content and run it:

const asciidoctor = require('@asciidoctor/core')()
const kroki = require('asciidoctor-kroki')

const input = 'plantuml::hello.puml[svg,role=sequence]'

kroki.register(asciidoctor.Extensions) // <1>
console.log(asciidoctor.convert(input))

const registry = asciidoctor.Extensions.create()
kroki.register(registry) // <2>
console.log(asciidoctor.convert(input, { extension_registry: registry }))

<1> Register the extension in the global registry
<2> Register the extension in a dedicated registry

Browser

Install the dependencies:

$ npm i asciidoctor asciidoctor-kroki

Create a file named kroki.html with the following content and open it in your browser:

<html lang="en">
  <head>
    <title>Asciidoctor x Kroki</title>
    <meta charset="utf-8">
    <script src="node_modules/@asciidoctor/core/dist/browser/asciidoctor.js"></script>
    <script src="node_modules/asciidoctor-kroki/dist/browser/asciidoctor-kroki.js"></script>
  </head>
  <body>
    <div id="content"></div>
    <script>
      const input = `Let's take an example with a _GraphViz_ "Hello World":

[graphviz]
....
digraph G {
  Hello->World
}
....`

      const asciidoctor = Asciidoctor()

      const registry = asciidoctor.Extensions.create()
      AsciidoctorKroki.register(registry) // <1>
      const result = asciidoctor.convert(input, { extension_registry: registry })
      document.getElementById('content').innerHTML = result
    </script>
  </body>
</html>

<1> Register the extension in a dedicated registry

❗ IMPORTANT: If you want to reference a diagram file in a browser environment you will need to define the base directory using the base_dir option. In addition, you will also need to provide an implementation to read a binary file synchronously for a given path. You can find an implementation based on XMLHttpRequest in the source code: https://github.com/Mogztter/asciidoctor-kroki/blob/9585b969014a1894d0c9fb76df10e1e8c66ce2b2/test/browser/test.js#L2-L34. Once httpGet is defined, here's how we should configure the extension:

const registry = asciidoctor.Extensions.create()
AsciidoctorKroki.register(registry, {
  vfs: {
    read: (path, encoding = 'utf8') => httpGet(path, encoding),
    exists: (_) => false,
    add: (_) => { /* no-op */ }
  }
})
const input = 'plantuml::hello.puml[svg,role=sequence]'
asciidoctor.convert(input, { base_dir: window.location.origin, safe: 'safe', extension_registry: registry })

Antora Integration

If you are using Antora, you can integrate Kroki in your documentation site.

  1. Install the extension in your playbook project:

    $ npm i asciidoctor-kroki
    
  2. Register the extension in your playbook file:

    asciidoc:
      extensions:
        - asciidoctor-kroki

    https://docs.antora.org/antora/2.3/playbook/configure-asciidoc/#extensions

  3. Enjoy!

💡 TIP: You can use the kroki-fetch-diagram option to download the images from Kroki at build time. In other words, while viewing pages you won't rely on Kroki anymore. However, in Antora, this is not currently compatible with inline SVG images.

asciidoc:
  attributes:
    kroki-fetch-diagram: true

Usage

In your AsciiDoc document, you can either write your diagram inline or, alternatively, you can make a reference to the diagram file using macro form or with the include directive.

Here's an example where we declare a GraphViz diagram directly in our AsciiDoc document using the block syntax:

[graphviz]
....
digraph foo {
  node [style=rounded]
  node1 [shape=box]
  node2 [fillcolor=yellow, style="rounded,filled", shape=diamond]
  node3 [shape=record, label="{ a | b | c }"]

  node1 -> node2 -> node3
}
....

GraphViz diagram

In the example below, we are using the vegalite macro to reference a file named chart.vlite:

vegalite::chart.vlite[svg,role=chart,opts=interactive]

Vega-Lite chart diagram

Finally, we can use the include directive to reference a diagram file:

[plantuml,alice-bob,svg,role=sequence]
....
include::alice-bob.puml[]
....

PlantUML diagram

Syntax

You can declare positional and named attributes when using the block or the macro form.

Positional attributes

When using the block form:

  1. The first positional attribute specifies the diagram type (see below for a complete list).
  2. The second optional positional attribute assigns a file name (i.e. target) to the generated diagram. Currently, the value of this attribute is ignored, and an auto-generated hash will be used as file name for caching purposes (see #48).
  3. The third optional positional attribute specifies the image format.

Example:

[mermaid,abcd-flowchart,svg]
....
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
....

In the above example, the diagram type is mermaid, the file name (i.e. target) is abcd-flowchart, and the image format is svg.

When using the macro form:

  1. The first optional positional attribute specifies the image format.

Example:

vegalite::chart.vlite[svg]

In the above example, the diagram type is vegalite, the target is chart.vlite, and the image format is svg.

Named attributes

You can also use both positional and named attributes. Here's an example:

.PlantUML example
[plantuml#diagAliceBob,alice-and-bob,svg,role=sequence]
....
alice -> bob
....

As you can see, we specified an id using the syntax #diagAliceBob just after the diagram type, and we are also using a named attribute to assign a role using role=sequence.

Here's another example using the macro form:

vegalite::chart.vlite[svg,role=chart,opts=interactive]

We are using a positional attribute to declare the image format and two named attributes to define the role and options.

Attributes

It's important to note that not all attributes are used in all converters. Here's a list of all attributes used in the built-in HTML 5 converter:

  • target
  • width
  • height
  • format (default svg)
  • fallback
  • link
  • float
  • align
  • role

Options

In addition, the following options are available when using the SVG format:

  • inline
  • interactive
  • none (used for cancelling defaults)

Options can be defined using options attribute (or opts for short):

[blockdiag,opts=inline]
....
blockdiag {
  Kroki -> generates -> "Block diagrams";

  Kroki [color = "greenyellow"];
  "Block diagrams" [color = "pink"];
}
....

Supported diagram types

Kroki currently supports the following diagram libraries:

Each diagram libraries support one or more output formats. Consult the Kroki documentation to find out which formats are supported.

Configuration

Attribute name Description Default value
kroki-server-url The URL of the Kroki server (see "Using Your Own Kroki") https://kroki.io
kroki-fetch-diagram Define if we should download (and save on the disk) the images from the Kroki server.
This feature is not available when running in the browser.
false
kroki-http-method Define how we should get the image from the Kroki server. Possible values:
  • get: always use GET requests
  • post: always use POST requests
  • adaptive: use a POST request if the URI length is longer than 4096 characters, otherwise use a GET request
adaptive

Default configuration

By default, images are generated as SVG when possible. To alter this, set the kroki-default-format attribute:

:kroki-default-format: png

You can unset this with :kroki-default-format!: or :kroki-default-format: svg.

ℹ️ NOTE: An AsciiDoc attribute can be defined through the CLI or API, in the document’s header or in the document’s body. In addition, if you are using Antora, you can define AsciiDoc attributes in your playbook and/or in your component descriptor.

References:

For instance, in an Antora playbook or component descriptor:

asciidoc:
  attributes:
    kroki-default-format@: png

(the @ allows the attribute value to be reset in documents)

By default, Asciidoctor Kroki generates a link, to a Kroki server or a local file. To change the default for SVG diagrams, set the kroki-default-options attribute.

:kroki-default-options: inline

You can unset this with :kroki-default-options: none or :kroki-default-options!: or specify opts=none in a block or macro.

Using Your Own Kroki

By default, this extension sends information and receives diagrams back from https://kroki.io.

You may choose to use your own server due to:

  • Network restrictions - if Kroki is not available behind your corporate firewall
  • Network latency - you are far from the European public instance
  • Privacy - you don't want to send your diagrams to a remote server on the internet

This is done using the kroki-server-url attribute. Typically, this is at the top of the document (under the title):

:kroki-server-url: http://my-server-url:port

For instance, if you have followed the instructions to set up a self-managed server using Docker you can use the following:

:kroki-server-url: http://localhost:8080

Note that either the http:// or https:// prefix is required (the default Docker image only uses http).

You can also set this attribute using the Javascript API, for instance:

asciidoctor.convertFile('file.adoc', { attributes: { 'kroki-server-url': 'http://my-server-url:port' } })

Contributing

Setup

To build this project, you will need the latest active LTS of Node.js and npm (we recommend nvm to manage multiple active Node.js versions).

Building

  1. Install the dependencies:

    $ npm i
    
  2. Generate a distribution:

    $ npm run dist
    

When working on a new feature or when fixing a bug, make sure to run the linter and the tests suite:

$ npm run lint
$ npm run test

About

Asciidoctor.js extension to convert diagrams to images using Kroki!

https://kroki.io/

License:MIT License


Languages

Language:JavaScript 91.8%Language:Ruby 7.4%Language:HTML 0.5%Language:Shell 0.3%