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

Inline Macro Processor Extension Example

Purpose

Render npm:PACKAGE[] as a hyperlink to the package page on npmjs.com. An optional version attribute appends the version to the link text.

sample-npm-doc.adoc

= Dependencies

Install npm:lodash[version=4.17.21] for utility functions
and npm:axios[] for HTTP requests.

NpmInlineMacroProcessor

npm-inline-macro-processor.js
export default function (registry) {
  registry.inlineMacro('npm', function () {
    this.process(function (parent, target, attrs) {
      const version = attrs.version ? `@${attrs.version}` : ''
      const url = `https://www.npmjs.com/package/${target}`
      const text = `${target}${version}`
      return this.createInline(parent, 'anchor', text, { type: 'link', target: url })
    })
  })
}

Usage

import { Extensions, convertFile } from '@asciidoctor/core'
import registerNpmInlineMacro from './npm-inline-macro-processor.js'

const registry = Extensions.create()
registerNpmInlineMacro(registry)

const html = await convertFile('sample-npm-doc.adoc', { to_file: false, extension_registry: registry })
console.log(html)
// <p>Install <a href="https://www.npmjs.com/package/lodash">lodash@4.17.21</a> for utility functions
// and <a href="https://www.npmjs.com/package/axios">axios</a> for HTTP requests.</p>