Preprocessor Extension Example

Purpose

Set the status of the document to DRAFT (using Document attributes) if it contains a comment that starts with draft.

sample-draft-doc.adoc

= This documentation is not ready yet

== First section

// draft: we need to talk about Y.

In this section, we are going to talk about X.

DraftPreprocessor

draft-preprocessor.js
module.exports = function (registry) {
  registry.preprocessor(function () {
    var self = this
    self.process(function (doc, reader) {
      var lines = reader.lines
      for (var i = 0; i < lines.length; i++) {
        if (lines[i].match(/^\/\/\s?draft.*/)) {
          doc.setAttribute('status', 'DRAFT')
        }
      }
      return reader
    })
  })
}

Usage

const asciidoctor = require('asciidoctor')()
const registry = asciidoctor.Extensions.create()
require('./draft-preprocessor.js')(registry)

const doc = asciidoctor.loadFile('sample-draft-doc.adoc', { 'extension_registry': registry })
console.log(doc.getAttribute('status')) // 'DRAFT'
const result = doc.convert()