Search by

bleizstudio / asciidoc-bundle

Skryptar

Pure PHP AsciiDoc-to-Twig compiler, HTML5 parser and documentation viewer bundle for Symfony 7 & 8 (sections, TOC, advanced tables, admonitions, Mermaid, KaTeX, includes).

Package info

gitlab.com/io.bleizstudio/asciidoc-bundle

Issues

Type:symfony-bundle

pkg:composer/bleizstudio/asciidoc-bundle

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

1.0.0 2026-12-02 07:10 UTC

This package is auto-updated.

Last update: 2026-12-03 03:30:16 UTC


README

bleizstudio/asciidoc-bundle

bleizstudio/asciidoc-bundle

Pipeline PHPStan PHPUnit PHP License

Language / Langue : English | Français

Pure PHP 8.4+ / Symfony 7 & 8 AsciiDoc-to-Twig compiler, HTML5 parser, and UI helpers (asciidoc.css + customizable asciidoc-scrollspy.js).

Features

  • Pure PHP AsciiDoc HTML5 parser (AsciiDocParserInterface / AsciiDocParser) — no Ruby/Asciidoctor binary required.
  • Document metadata & TOC extraction (AsciiDocDocument: title, description, attributes, headings tree).
  • Rich syntax support:
    • Sections (= to ======) with automatic anchor IDs
    • Advanced tables (|===, [cols="1,1,2", options="header"], alignment, spans)
    • Admonitions (NOTE, TIP, IMPORTANT, WARNING, CAUTION + [NOTE] blocks)
    • Code / literal / quote blocks, callouts, inline formatting, links, images
    • Mermaid blocks ([mermaid] / `mermaid ) preserved with class="mermaid"
    • KaTeX inline & display math ($...$, $$...$$, \(...\), \[...\]) protected during parsing
    • Preprocessor directives: include::path[], ifdef::, ifndef::, ifeval::, endif::
  • Static Twig compiler: php bin/console asciidoc:build compiles .adoc trees into .html.twig templates and generates manifest.json.
  • Twig filters: |asciidoc and |asciidoc_document.
  • Customizable ScrollSpy (public/asciidoc-scrollspy.js) compatible with dedicated scroll containers, window, Turbo, and Symfony UX LiveComponent / Stimulus.

Installation

1. Composer

If using a local path repository in composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "../packages/asciidoc-bundle",
            "options": {
                "symlink": false
            }
        }
    ],
    "require": {
        "bleizstudio/asciidoc-bundle": "^1.0"
    }
}

Then run:

composer update bleizstudio/asciidoc-bundle
php bin/console assets:install public

2. Bundle registration (config/bundles.php)

return [
    // ...
    BleizStudio\AsciiDocBundle\AsciiDocBundle::class => ['all' => true],
];

3. Configuration (config/packages/ascii_doc.yaml)

ascii_doc:
    source_dir: '%kernel.project_dir%/asciidoc'
    output_dir: '%kernel.project_dir%/templates/asciidoc'
    layout: null

How It Works (Quick Start)

  1. Create your .adoc files inside asciidoc/ at the root of your Symfony project (subdirectories are preserved):
    asciidoc/
    ├── index.adoc
    └── guides/
        └── getting-started.adoc
    
  2. Compile them to Twig templates:
    php bin/console asciidoc:build --clean
    
  3. Use the generated .html.twig files in templates/asciidoc/ (along with templates/asciidoc/manifest.json containing the title, description, and TOC of each page):
    templates/asciidoc/
    ├── manifest.json
    ├── index.html.twig
    └── guides/
        └── getting-started.html.twig
    
    {# Render a compiled page in any Twig template #}
    <div class="asciidoc-content">
        {% include 'asciidoc/guides/getting-started.html.twig' %}
    </div>
    

Tip: Both asciidoc/ (source_dir) and templates/asciidoc/ (output_dir) can be customized in config/packages/ascii_doc.yaml or overridden via -s / -o on asciidoc:build.

Usage

Twig Filters

{# Render AsciiDoc string directly to HTML5 #}
<div class="asciidoc-content">
    {{ content|asciidoc }}
</div>

{# With baseDir (for relative include::) and custom attributes #}
{{ content|asciidoc(app.project_dir ~ '/asciidoc', { env: 'prod' }) }}

{# Parse into an AsciiDocDocument object (title, toc, attributes, html) #}
{% set doc = content|asciidoc_document %}
<h1>{{ doc.title }}</h1>
<div class="asciidoc-content">{{ doc.html|raw }}</div>

PHP Service

use BleizStudio\AsciiDocBundle\Parser\AsciiDocParserInterface;

final readonly class DocService
{
    public function __construct(
        private AsciiDocParserInterface $parser,
    ) {
    }

    public function render(string $source): string
    {
        return $this->parser->toHtml($source);
    }
}

Static Compilation CLI (asciidoc:build)

# Compile all .adoc files from source_dir into output_dir + manifest.json
php bin/console asciidoc:build

# Remove orphaned .html.twig files no longer present in source_dir
php bin/console asciidoc:build --clean

# Custom directories and optional Twig layout
php bin/console asciidoc:build -s docs/ -o templates/docs/ -l base.html.twig

Assets & Customizable ScrollSpy

Publish assets with php bin/console assets:install public, then include them in your Twig template:

<link rel="stylesheet" href="{{ asset('bundles/asciidoc/asciidoc.css') }}">
<script src="{{ asset('bundles/asciidoc/asciidoc-scrollspy.js') }}"></script>

Initialize the ScrollSpy:

document.addEventListener('DOMContentLoaded', () => {
    window.initAsciiDocScrollSpy({
        scrollContainer: '.my-scroll-pane', // defaults to window
        tocContainer: '.my-toc',            // defaults to '.asciidoc-toc'
        linkSelector: 'a[href^="#"]',       // defaults to 'a[href^="#"]'
        activeClass: 'active',              // defaults to 'active'
        offset: 120,                        // activation offset in px (default: 120)
        autoScrollToc: true,                // keep active TOC link in view (default: true)
    });
});

ScrollSpy Options

OptionTypeDefaultDescription
scrollContainerstring \| Element \| WindowwindowScrollable container element or CSS selector.
tocContainerstring \| Element'.asciidoc-toc'Table of contents container element or selector.
linkSelectorstring'a[href^="#"]'Selector matching TOC anchor links inside tocContainer.
activeClassstring'active'CSS class toggled on the currently active TOC link.
offsetnumber120Top threshold (px) used to mark a heading as active.
autoScrollTocbooleantrueAutomatically scrolls the TOC to keep the active link visible.

initAsciiDocScrollSpy() returns a cleanup function (destroyAsciiDocScrollSpy) or null if no TOC entries are found.

Symfony UX LiveComponent & Stimulus

AsciiDocParser, |asciidoc, |asciidoc_document, and compiled .html.twig templates are stateless and work out of the box inside any Symfony UX LiveComponent:

{# templates/components/AsciiDocPreview.html.twig #}
<div {{ attributes }} data-controller="asciidoc-scrollspy">
    <aside class="asciidoc-toc" data-asciidoc-scrollspy-target="toc">
        {# TOC links #}
    </aside>
    <div class="asciidoc-content" data-asciidoc-scrollspy-target="content">
        {{ this.source|asciidoc }}
    </div>
</div>

Because LiveComponent re-renders DOM fragments via morphdom without firing DOMContentLoaded, initialize and tear down the ScrollSpy inside a Stimulus controller using the returned cleanup function:

// assets/controllers/asciidoc_scrollspy_controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
    static targets = ['toc', 'content'];

    connect() {
        if (typeof window.initAsciiDocScrollSpy === 'function') {
            this.destroyScrollSpy = window.initAsciiDocScrollSpy({
                scrollContainer: this.hasContentTarget ? this.contentTarget : window,
                tocContainer: this.hasTocTarget ? this.tocTarget : this.element,
            });
        }
    }

    disconnect() {
        if (typeof this.destroyScrollSpy === 'function') {
            this.destroyScrollSpy();
            this.destroyScrollSpy = null;
        }
    }
}

Tests

vendor/bin/phpunit packages/asciidoc-bundle/tests

License

bleizstudio/asciidoc-bundle is licensed under GPL-3.0-or-later.