Asciidoctor.js: AsciiDoc in JavaScript powered by Asciidoctor
Dan Allen https://github.com/mojavelinux[@mojavelinux]; Guillaume Grossetie https://github.com/mogztter[@mogztter]; Anthonny Quérouil https://github.com/anthonny[@anthonny]
:idprefix:
:idseparator: -
:uri-nodejs: http://nodejs.org
:uri-opal: http://opalrb.org
:uri-repo: https://github.com/asciidoctor/asciidoctor.js
:uri-freesoftware: https://www.gnu.org/philosophy/free-sw.html
:uri-rel-file-base: link:
:uri-contribute: {uri-rel-file-base}CONTRIBUTING.adoc
:uri-user-manual: {uri-rel-file-base}manual.adoc
:license: {uri-repo}/blob/master/LICENSE
:experimental:
:endash:
ifdef::env-github[]
image:http://img.shields.io/travis/asciidoctor/asciidoctor.js.svg[Travis build status, link=https://travis-ci.org/asciidoctor/asciidoctor.js]
image:http://img.shields.io/npm/v/asciidoctor.js.svg[npm version, link=https://www.npmjs.org/package/asciidoctor.js]
image:https://ci.appveyor.com/api/projects/status/i69sqvvyr95sf6i7/branch/master?svg=true[Appveyor build status, link=https://ci.appveyor.com/project/asciidoctor/asciidoctor-js]
endif::[]
Asciidoctor.js brings AsciiDoc to the JavaScript world!
This project uses {uri-opal}[Opal] to transpile http://asciidoctor.org[Asciidoctor], a modern implementation of AsciiDoc, from Ruby to JavaScript to produce asciidoctor.js.
The asciidoctor.js script can be run on any JavaScript platform, including Node.js, Nashorn and, of course, a web browser.
Introduction
You can use Asciidoctor.js either for back-end development using {uri-nodejs}[Node.js] or front-end development using a browser.
Front-end development
.Installing Asciidoctor.js with Bower
$ bower install asciidoctor.js --save
Once the package installed, you can add the following script
tag to your HTML page:
<script src="bower_components/asciidoctor.js/dist/asciidoctor-all.min.js"></script>
<script src="bower_components/asciidoctor.js/dist/asciidoctor-docbook.min.js"></script>
If you don't want to use extensions, you can load files separately :
<script src="bower_components/opal/opal/current/opal.min.js"></script>
<script src="bower_components/asciidoctor.js/dist/asciidoctor-core.min.js"></script>
<script src="bower_components/asciidoctor.js/dist/asciidoctor-docbook.min.js"></script>
Here is a simple example that converts AsciiDoc to HTML5 using the doctype: 'inline'
option and showtitle
attribute:
.sample.js
var content = "http://asciidoctor.org[*Asciidoctor*] " +
"running on http://opalrb.org[_Opal_] " +
"brings AsciiDoc to the browser!";
var options = Opal.hash({doctype: 'inline', attributes: ['showtitle']});
var html = Opal.Asciidoctor.$convert(content, options);
console.log(html);
<1> Convert AsciiDoc content to HTML5 using Asciidoctor.js
<2> Print the HTML5 output to the console
Back-end development
.Installing Asciidoctor.js with npm
$ npm install asciidoctor.js --save
Once the package is installed, the first thing to do is to load the asciidoctor.js
module using require
, then you're ready to start using the API:
.sample.js
var asciidoctor = require('asciidoctor.js')();
var opal = asciidoctor.Opal;
var processor = null;
var useExtensions = true;
if (useExtensions) {
processor = asciidoctor.Asciidoctor(true);
}
else {
processor = asciidoctor.Asciidoctor();
}
var content = "http://asciidoctor.org[*Asciidoctor*] " +
"running on http://opalrb.org[_Opal_] " +
"brings AsciiDoc to Node.js!";
var options = opal.hash({doctype: 'inline', attributes: ['showtitle']});
var html = processor.$convert(content, options);
console.log(html);
<1> Load the Asciidoctor.js library
<2> Retrieve and alias the top-level Opal namespace
<3> Instantiate Asciidoctor with extensions enabled
<4> Instantiate Asciidoctor without extensions
<5> Convert AsciiDoc content to HTML5 using Asciidoctor.js
<6> Print the HTML5 output to the console
Save the file as sample.js
and run it using the node
command:
$ node sample.js
You should see the following output in your terminal:
[.output]
....
Asciidoctor running on Opal brings AsciiDoc to Node.js!
....
Advanced topics
If you want to know more about Asciidoctor.js, please read the {uri-user-manual}[User Manual].
Contributing
In the spirit of {uri-freesoftware}[free software], everyone is encouraged to help improve this project.
If you discover errors or omissions in the source code, documentation, or website content, please don't hesitate to submit an issue or open a pull request with a fix.
New contributors are always welcome!
The {uri-contribute}[Contributing] guide provides information on how to contribute.
Copyright
Copyright (C) 2016 Dan Allen, Guillaume Grossetie, Anthonny Quérouil and the Asciidoctor Project.
Free use of this software is granted under the terms of the MIT License.
See the {license}[LICENSE] file for details.