Skip to content

Commit 7fe5b65

Browse files
committed
Add Wikilinks example.
1 parent e7811e5 commit 7fe5b65

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

docs/cookbook.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,22 +644,70 @@ Output:
644644

645645
### Wiki Links
646646

647-
Support `[[Page Name]]` wiki-style links:
647+
Support `[[Page Name]]` and `[[Page Name|display text]]` wiki-style links:
648648

649649
```php
650-
$parser->addInlinePattern('/\[\[([^\]]+)\]\]/', function ($match, $groups, $p) {
651-
$page = $groups[1];
652-
$link = new Link('/wiki/' . rawurlencode($page));
653-
$link->appendChild(new Text($page));
650+
use Djot\DjotConverter;
651+
use Djot\Node\Inline\Link;
652+
use Djot\Node\Inline\Text;
653+
654+
$converter = new DjotConverter();
655+
$parser = $converter->getParser()->getInlineParser();
656+
657+
// Pattern matches [[target]] or [[target|display text]]
658+
$parser->addInlinePattern('/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/', function ($match, $groups, $p) {
659+
$target = trim($groups[1]);
660+
$display = isset($groups[2]) ? trim($groups[2]) : $target;
661+
662+
// Convert target to URL (customize as needed)
663+
$url = '/wiki/' . rawurlencode(str_replace(' ', '-', $target));
664+
665+
$link = new Link($url);
666+
$link->appendChild(new Text($display));
667+
$link->setAttribute('class', 'wikilink');
654668
return $link;
655669
});
656670

657-
echo $converter->convert('See [[Home Page]] and [[Getting Started]].');
671+
echo $converter->convert('See [[Home Page]] and [[API Reference|the API docs]].');
658672
```
659673

660674
Output:
661675
```html
662-
<p>See <a href="/wiki/Home%20Page">Home Page</a> and <a href="/wiki/Getting%20Started">Getting Started</a>.</p>
676+
<p>See <a href="/wiki/Home-Page" class="wikilink">Home Page</a> and <a href="/wiki/API-Reference" class="wikilink">the API docs</a>.</p>
677+
```
678+
679+
#### Configurable Base URL
680+
681+
```php
682+
// Make the base URL configurable
683+
$wikiBaseUrl = '/docs/'; // or 'https://wiki.example.com/'
684+
685+
$parser->addInlinePattern('/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/', function ($match, $groups, $p) use ($wikiBaseUrl) {
686+
$target = trim($groups[1]);
687+
$display = isset($groups[2]) ? trim($groups[2]) : $target;
688+
$slug = strtolower(str_replace(' ', '-', $target));
689+
690+
$link = new Link($wikiBaseUrl . rawurlencode($slug));
691+
$link->appendChild(new Text($display));
692+
return $link;
693+
});
694+
```
695+
696+
#### With File Extension
697+
698+
```php
699+
// Add .html extension for static sites
700+
$parser->addInlinePattern('/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/', function ($match, $groups, $p) {
701+
$target = trim($groups[1]);
702+
$display = isset($groups[2]) ? trim($groups[2]) : $target;
703+
$slug = strtolower(str_replace(' ', '-', $target));
704+
705+
$link = new Link('/pages/' . $slug . '.html');
706+
$link->appendChild(new Text($display));
707+
return $link;
708+
});
709+
710+
// [[Installation Guide]] → <a href="/pages/installation-guide.html">Installation Guide</a>
663711
```
664712

665713
### Hashtags

0 commit comments

Comments
 (0)