Preprocessor Extension Example
- Purpose
-
Set the status of the document to
DRAFT(using Document attributes) if it contains a comment that starts withdraft.
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
export default function (registry) {
registry.preprocessor(function () {
const self = this
self.process(function (doc, reader) {
const lines = reader.lines
for (const line of lines) {
if (line.match(/^\/\/\s?draft.*/)) {
doc.setAttribute('status', 'DRAFT')
}
}
return reader
})
})
}
Usage
import { Extensions, loadFile } from '@asciidoctor/core'
import registerDraftPreprocessor from './draft-preprocessor.js'
const registry = Extensions.create()
registerDraftPreprocessor(registry)
const doc = await loadFile('sample-draft-doc.adoc', { extension_registry: registry })
console.log(doc.getAttribute('status')) // 'DRAFT'
const html = await doc.convert()