Inline 宏处理器扩展示例

目的

将宏表情符号替换为对应的文本表情符号。

sample-emoticon-doc.adoc

emoticon:wink[]
emoticon:grin[]
emoticon:x[]

EmoticonInlineMacroProcessor

emoticon-inline-macro-processor.js
module.exports = function (registry) {
  registry.inlineMacro('emoticon', function () {
    var self = this
    self.process(function (parent, target) {
      var text
      if (target === 'grin') {
        text = ':D'
      } else if (target === 'wink') {
        text = ';)'
      } else {
        text = ':)'
      }
      return self.createInline(parent, 'quoted', text, { 'type': 'strong' })
    })
  })
}

使用

const asciidoctor = require('asciidoctor')()
const registry = asciidoctor.Extensions.create()
require('./emoticon-inline-macro-processor.js')(registry)

const html = asciidoctor.convertFile('ssample-emoticon-doc.adoc', { 'to_file': false, 'extension_registry': registry })
console.log(html)