asciidoctor / asciidoctor.js

:scroll: A JavaScript port of Asciidoctor, a modern implementation of AsciiDoc
https://asciidoctor.org
MIT License
739 stars 136 forks source link

Asynchronous extension registration #470

Closed thom4parisot closed 6 years ago

thom4parisot commented 6 years ago

Hello,

I am trying to make an extension which brings code from a third party npm script.

At the moment it looks like this:

asciidoctor.register(function() {
  this.docinfoProcessor(function(){
    this.process(() => {
      return `<script src="https://wzrd.in/standalone/menuspy"></script>
<script>window.addEventListener('load', () => new menuspy(document.querySelector('#toc'), {enableLocationHash: false}))</script>`;
    });
  });
});

I was wondering if there were some thinking to make them be asynchronous?

const browserify = require('browserify');
const script = require('./extension');

asciidoctor.register(function() {
  this.docinfoProcessor(function(){
    this.process((done) => {
      browserify(script).require('menuspy').bundle((err, code) => done(err, `<script>${code}</script>`));
    });
  });
});

Or maybe there is a more recommended way of bundling CSS and JS withing an extension.

mojavelinux commented 6 years ago

I would flip this around. I would put Asciidoctor in the callback of browserify.

Given that the porcelain extension methods are provided by Asciidoctor.js, I suppose it would be possible to make these async...or at least have an async variant of them.

thom4parisot commented 6 years ago

Hm it might be too much work to create async methods, especially if no other person ask for them.

I like the idea of flipping the problem around—will do that!

Thank you :-)