bleizstudio / asciidoc-bundle
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
Type:symfony-bundle
pkg:composer/bleizstudio/asciidoc-bundle
Requires
- php: >=8.4
- symfony/config: ^7.0|^8.0
- symfony/dependency-injection: ^7.0|^8.0
- symfony/http-kernel: ^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^11.0|^12.0|^13.0
- symfony/console: ^7.0|^8.0
- twig/twig: ^3.14
Suggests
- symfony/console: To use the bin/console asciidoc:build command
- twig/twig: To use the |asciidoc and |asciidoc_document Twig filters
Provides
None
Conflicts
None
Replaces
None
README
bleizstudio/asciidoc-bundle
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 withclass="mermaid" - KaTeX inline & display math (
$...$,$$...$$,\(...\),\[...\]) protected during parsing - Preprocessor directives:
include::path[],ifdef::,ifndef::,ifeval::,endif::
- Sections (
- Static Twig compiler:
php bin/console asciidoc:buildcompiles.adoctrees into.html.twigtemplates and generatesmanifest.json. - Twig filters:
|asciidocand|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)
- Create your
.adocfiles insideasciidoc/at the root of your Symfony project (subdirectories are preserved):asciidoc/ ├── index.adoc └── guides/ └── getting-started.adoc - Compile them to Twig templates:
php bin/console asciidoc:build --clean - Use the generated
.html.twigfiles intemplates/asciidoc/(along withtemplates/asciidoc/manifest.jsoncontaining 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) andtemplates/asciidoc/(output_dir) can be customized inconfig/packages/ascii_doc.yamlor overridden via-s/-oonasciidoc: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
| Option | Type | Default | Description |
|---|---|---|---|
scrollContainer | string \| Element \| Window | window | Scrollable container element or CSS selector. |
tocContainer | string \| Element | '.asciidoc-toc' | Table of contents container element or selector. |
linkSelector | string | 'a[href^="#"]' | Selector matching TOC anchor links inside tocContainer. |
activeClass | string | 'active' | CSS class toggled on the currently active TOC link. |
offset | number | 120 | Top threshold (px) used to mark a heading as active. |
autoScrollToc | boolean | true | Automatically 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.