asciidoctor.js icon indicating copy to clipboard operation
asciidoctor.js copied to clipboard

Add QuickJS support

Open yne opened this issue 8 months ago • 1 comments

This Add QuickJS support to Asciidoctor.js

You can run the only quickjs test with:

# qjs must be in your $PATH
npm run test:quickjs --prefix packages/core

QuickJS can also compile asciidoctor.js into a native binary (like a WASM+runtime) so we could release asciidoctor.js as standalone binary for windows/mac/linux.

Here is a basic CLI frontend (asciidoctor.mjs):

import * as std from 'std';
import Asciidoctor from "asciidoctor-quickjs.js";

const _ = scriptArgs.shift();
const USAGE = `USAGE: ${_} [OPTIONS] [FILE|-]
EXAMPLE: ${_} --safe=0 --doctype=\\"article\\" <<< include::partial.adoc[]`
const die = (msg) => console.log(msg) + std.exit(1);
const [file = ""] = scriptArgs.filter(arg => !arg.startsWith('-'));
const options = Object.fromEntries(scriptArgs.filter(arg => arg.startsWith('-'))
    .map(arg => arg.split('=')).map(([k, ...v]) => [k.replace(/^-+/, ''), std.parseExtJSON(v.join('=') || '1')]));
if (options.help) die(USAGE);
std.err.puts(`converting ${file ? "file:" + file : 'stdin'} options:${JSON.stringify(options)}\n`);
const body = file ? std.loadFile(file) : std.in.readAsString();
if (!body) die(USAGE);
try {
    console.log(Asciidoctor().convert(body, options));
} catch (e) {
    console.log(e)
}

which we can be compile/strip/pack with

qjsc -o asciidoctor_bin -flto asciidoctor.mjs && strip asciidoctor_bin && upx -9 asciidoctor_bin

To get a 746,5 KiB binary:

yne avatar Jun 16 '24 00:06 yne