Extract information from a document

Assumptions:

  • You’ve successfully installed Asciidoctor.js.

To make it easier to understand, in the following examples, we will use this sample file:

sample.adoc
= The Documentation Chronicles: Based on True Events
Kismet Chameleon <kismet@asciidoctor.org>
v1.0, October 2, 2018: First incarnation

== The Ravages of Writing

Document header

Get the document title

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.getDocumentTitle()) // The Documentation Chronicles: Based on True Events

By default, the document title is separated into a main title and subtitle using the industry standard, a colon followed by a space. You can partition the title from the API when calling the getDocumentTitle function:

var doc = asciidoctor.loadFile('sample.adoc')
var doctitle = doc.getDocumentTitle({ partition: true })
console.log(doctitle.getMain())     // The Documentation Chronicles
console.log(doctitle.getSubtitle()) // Based on True Events
console.log(doctitle.getCombined()) // The Documentation Chronicles: Based on True Events
console.log(doctitle.hasSubtitle()) // true

Get the author

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.getAuthor()) // Kismet Chameleon

Asciidoctor uses the author’s name and email to assign values to a number of built-in attributes that can be used throughout the document’s body but also retrieve with the getAttribute function:

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.getAttribute('author'))         // Kismet Chameleon
console.log(doc.getAttribute('firstname'))      // Kismet
console.log(doc.getAttribute('lastname'))       // Chameleon
console.log(doc.getAttribute('middlename'))     // undefined
console.log(doc.getAttribute('authorinitials')) // KC
console.log(doc.getAttribute('email'))          // kismet@asciidoctor.org

These attributes include:

author

The author’s full name, which includes all of the characters or words prior to a semicolon (;), angle bracket (<) or the end of the line.

firstname

The first word in the author attribute.

lastname

The last word in the author attribute.

middlename

If a firstname and lastname are present, any remaining words or characters found between these attributes are assigned to the middlename attribute.

authorinitials

The first character of the firstname, middlename, and lastname attributes.

email

An email address, delimited by angle brackets (<>).

Get the document revision information

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.getRevisionDate())   // October 2, 2018
console.log(doc.getRevisionNumber()) // 1.0
console.log(doc.getRevisionRemark()) // First incarnation

Alternatively, you can also get all the revision information at once:

var doc = asciidoctor.loadFile('sample.adoc')
var revisionInfo = doc.getRevisionInfo()
console.log(revisionInfo.getDate())   // October 2, 2018
console.log(revisionInfo.getNumber()) // 1.0
console.log(revisionInfo.getRemark()) // First incarnation

You can also check if the document contains revision info with hasRevisionInfo function:

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.hasRevisionInfo()) // true

As with the author, revision information are also available as built-in attributes:

var doc = asciidoctor.loadFile('sample.adoc')
console.log(doc.getAttribute('revdate'))   // October 2, 2018
console.log(doc.getAttribute('revnumber')) // 1.0
console.log(doc.getAttribute('revremark')) // First incarnation
revdate

Date of document version

revnumber

Version number of the document

revremark

Version comments

What’s next?

You can read the API docs to learn more about the API.