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

Block Macro Extension Example

Purpose

Embed a GitHub Gist by ID using the gist:: block macro. An optional file attribute restricts the embed to a single file within the Gist.

sample-gist-doc.adoc

= Code Examples

The following Gist shows how to configure the client:

gist::mojombo/4ae3f9bc20c6b67e06a7[]

Or a single file from a multi-file Gist:

gist::mojombo/4ae3f9bc20c6b67e06a7[file=hello_world.rb]

GistBlockMacro

gist-block-macro.js
export default function (registry) {
  registry.blockMacro(function () {
    this.named('gist')
    this.process(function (parent, target, attrs) {
      const file = attrs.file ? `?file=${attrs.file}` : ''
      const html = `<script src="https://gist.github.com/${target}.js${file}"></script>`
      return this.createBlock(parent, 'pass', html)
    })
  })
}

Usage

import { Extensions, convert } from '@asciidoctor/core'
import registerGistBlockMacro from './gist-block-macro.js'

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

const html = await convert('gist::mojombo/4ae3f9bc20c6b67e06a7[]', { extension_registry: registry })
console.log(html)
// <script src="https://gist.github.com/mojombo/4ae3f9bc20c6b67e06a7.js"></script>