预处理器扩展示例
- 目的
-
如果文档中包含以
draft
开头的注释,则将文档的状态设置为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
})
})
}
使用
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()