Skip to content

Commit 7444d99

Browse files
committed
Update README and docs to reference extensions
- Add Extensions to features list in README - Add Extensions link to documentation list - Update main example to use ExternalLinksExtension and DefaultAttributesExtension - Add Extensions section to docs/README.md - Update Custom Syntax Patterns to show hashtags (mentions covered by extension) - Simplify Event System example
1 parent 5d39cc0 commit 7444d99

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,25 @@ $html = $converter->convert('Hello *world*!');
3333
- **Advanced**: Footnotes, math expressions, symbols, block attributes, raw HTML blocks, comments
3434
- **Smart typography**: Curly quotes, en/em dashes, ellipsis
3535
- **Multiple renderers**: HTML, plain text, Markdown output
36+
- **Extensions**: Built-in extensions for external links, TOC, heading permalinks, @mentions, autolinks, default attributes
3637
- **Extensible**: Custom inline/block patterns, render events
3738
- **File support**: Parse and convert files directly
3839

3940
## Example
4041

4142
```php
4243
use Djot\DjotConverter;
43-
use Djot\Event\RenderEvent;
44+
use Djot\Extension\ExternalLinksExtension;
45+
use Djot\Extension\DefaultAttributesExtension;
4446

4547
$converter = new DjotConverter();
4648

47-
// Customize link rendering
48-
$converter->on('render.link', function (RenderEvent $event): void {
49-
$link = $event->getNode();
50-
if (str_starts_with($link->getDestination(), 'http')) {
51-
$link->setAttribute('target', '_blank');
52-
}
53-
});
49+
// Add extensions for common features
50+
$converter
51+
->addExtension(new ExternalLinksExtension())
52+
->addExtension(new DefaultAttributesExtension([
53+
'table' => ['class' => 'table'],
54+
]));
5455

5556
$djot = <<<'DJOT'
5657
# Welcome
@@ -75,8 +76,8 @@ Output:
7576

7677
```html
7778
<h1>Welcome</h1>
78-
<p>This is <em>emphasized</em> and <strong>strong</strong> text with a <a href="https://example.com" target="_blank">link</a>.</p>
79-
<table>
79+
<p>This is <em>emphasized</em> and <strong>strong</strong> text with a <a href="https://example.com" target="_blank" rel="noopener noreferrer">link</a>.</p>
80+
<table class="table">
8081
<thead>
8182
<tr><th>Name</th><th>Role</th></tr>
8283
</thead>
@@ -101,6 +102,7 @@ https://sandbox.dereuromark.de/sandbox/djot
101102
- [Examples](docs/README.md) - Comprehensive usage examples
102103
- [Syntax Reference](docs/syntax.md) - Complete Djot syntax guide
103104
- [API Reference](docs/api.md) - Classes and methods
105+
- [Extensions](docs/extensions.md) - Built-in extensions for common features
104106
- [Profiles](docs/profiles.md) - Feature restriction for different contexts
105107
- [Converters](docs/converters.md) - Markdown/BBCode to Djot conversion
106108
- [Cookbook](docs/cookbook.md) - Common customizations and recipes

docs/README.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,39 @@ Output:
264264
<p>Ellipsis…</p>
265265
```
266266

267-
## Event System
267+
## Extensions
268268

269-
Customize rendering by listening to events:
269+
Use built-in extensions for common features:
270270

271271
```php
272272
use Djot\DjotConverter;
273-
use Djot\Event\RenderEvent;
273+
use Djot\Extension\ExternalLinksExtension;
274+
use Djot\Extension\TableOfContentsExtension;
275+
use Djot\Extension\MentionsExtension;
274276

275277
$converter = new DjotConverter();
278+
$toc = new TableOfContentsExtension(position: 'top');
276279

277-
// Add target="_blank" to external links
278-
$converter->on('render.link', function (RenderEvent $event): void {
279-
$link = $event->getNode();
280-
$href = $link->getDestination();
280+
$converter
281+
->addExtension(new ExternalLinksExtension())
282+
->addExtension(new MentionsExtension())
283+
->addExtension($toc);
281284

282-
if (str_starts_with($href, 'http')) {
283-
$link->setAttribute('target', '_blank');
284-
$link->setAttribute('rel', 'noopener noreferrer');
285-
}
286-
});
285+
echo $converter->convert('Visit [Example](https://example.com) or ask @admin!');
286+
// External links get target="_blank", @mentions become profile links
287+
```
288+
289+
See [Extensions](extensions.md) for all available extensions.
290+
291+
## Event System
292+
293+
For custom rendering logic, listen to events:
294+
295+
```php
296+
use Djot\DjotConverter;
297+
use Djot\Event\RenderEvent;
298+
299+
$converter = new DjotConverter();
287300

288301
// Custom emoji rendering
289302
$converter->on('render.symbol', function (RenderEvent $event): void {
@@ -297,8 +310,8 @@ $converter->on('render.symbol', function (RenderEvent $event): void {
297310
$event->setHtml($emoji);
298311
});
299312

300-
echo $converter->convert('[Example](https://example.com) I :heart: this!');
301-
// Output: <p><a href="https://example.com" target="_blank" rel="noopener noreferrer">Example</a> I ❤️ this!</p>
313+
echo $converter->convert('I :heart: this!');
314+
// Output: <p>I ❤️ this!</p>
302315
```
303316

304317
## XHTML Mode
@@ -353,25 +366,28 @@ $html = $converter->render($document);
353366

354367
## Custom Syntax Patterns
355368

369+
> **Tip:** For @mentions, use the built-in [MentionsExtension](extensions.md#mentionsextension).
370+
356371
Extend Djot with custom inline and block patterns:
357372

358373
```php
359374
use Djot\Node\Inline\Link;
360375
use Djot\Node\Inline\Text;
361376

362-
// Add @mention support
377+
// Add #hashtag support
363378
$parser = $converter->getParser()->getInlineParser();
364-
$parser->addInlinePattern('/@([a-zA-Z0-9_]+)/', function ($match, $groups, $p) {
365-
$link = new Link('/users/' . $groups[1]);
366-
$link->appendChild(new Text('@' . $groups[1]));
379+
$parser->addInlinePattern('/#([a-zA-Z0-9_]+)/', function ($match, $groups) {
380+
$tag = $groups[1];
381+
$link = new Link('/tags/' . $tag);
382+
$link->appendChild(new Text('#' . $tag));
367383
return $link;
368384
});
369385

370-
echo $converter->convert('Hello @john!');
371-
// Output: <p>Hello <a href="/users/john">@john</a>!</p>
386+
echo $converter->convert('Check out #php and #djot!');
387+
// Output: <p>Check out <a href="/tags/php">#php</a> and <a href="/tags/djot">#djot</a>!</p>
372388
```
373389

374-
See the [Cookbook](cookbook.md) for more examples including wiki links, hashtags, admonitions, and custom block patterns.
390+
See the [Cookbook](cookbook.md) for more examples including wiki links, admonitions, and custom block patterns.
375391

376392
## Security Considerations
377393

0 commit comments

Comments
 (0)