Running JavaScript in an AsciidoctorJ extension

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Running JavaScript in an AsciidoctorJ extension


We are creating a custom visualization based on d3.js for a web application. I would like to be able to re-use that code to generate diagrams with that code and put the resulting SVG in a HTML or PDF document. My idea is to use the Nashorn JavaScript engine from Java8 to make that possible.

So far, I managed to create a simple extension that executes some simple JavaScript:

public class JavaScriptExecutionBlock extends BlockProcessor {

    private static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("nashorn");

    public JavaScriptExecutionBlock(String name, Map<String, Object> config) {
        super(name, createConfig());

    public Object process(AbstractBlock parent, Reader reader, Map<String, Object> attributes) {
        String jsResult;
        try {
            jsResult = SCRIPT_ENGINE.eval(;
        } catch (ScriptException e) {
            jsResult = e.getMessage();
        return createBlock(parent, "paragraph", jsResult, attributes, new HashMap<>());

    private static Map<String, Object> createConfig() {
        Map<String, Object> result = new HashMap<>();
        result.put("contexts", createContextsConfig());
        return result;

    private static List<String> createContextsConfig() {
        List<String> contexts = new ArrayList<>();
        return contexts;

Using this BlockProccessor, it will transform this:


by running the addition in Javascript and outputting the result.

I am wondering how to do the next steps, so I could use some guidance.

The visualization is similar to drawing an UML diagram, so I will explain it like that. Suppose I put this in the input asciidoc document:

BlockProcessor -> Processor

Suppose further that I have a JavaScript file with a method that accepts 'BlockProcessor -> Processor' and returns an SVG string. Given the code from I should be able to load those JavaScript files and execute the function.

2 main questions:
* How do I code the fact that I am returning SVG in the process method of my BlockProcessor? I am currently using something like: createBlock(parent, "paragraph", jsResult, attributes, new HashMap<>()); Do I need to use something else than "paragraph" ?
* Are there any ideas how I could make this a re-usable extension where you could somehow indicate in the document what JavaScript files you want to load and what function you want to call? How could this be indicated in an asciidoc source file?