This documentation covers a prerelease version of the software. Follow this link to view the documentation for the stable version (3.0) instead.

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

const doc = await 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:

const doc = await loadFile('sample.adoc')
const 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

const doc = await 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 retrieved with the getAttribute function:

const doc = await 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

const doc = await 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:

const doc = await loadFile('sample.adoc')
const 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:

const doc = await loadFile('sample.adoc')
console.log(doc.hasRevisionInfo()) // true

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

const doc = await 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.