An extension for league-commonmark that lets you use Wikilinks in your markdown files. These will be converted to html as you have come to expect from other apps that support Wikilinks such as Wikipedia and Obsidian.
You can install the package via composer:
composer require cassarco/league-commonmark-wikilinks
use League\CommonMark\MarkdownConverter;
use Cassarco\LeagueCommonmarkWikilinks\WikilinksExtension;
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new WikiLinksExtension());
$markdown = "[[Hello World]]";
(new MarkdownConverter($environment))->convert($markdown);
// <p><a href="hello-world" title="Hello World">Hello World</a></p>
Surround text in double braces and this extension will automatically convert the Wikilink into an html link with a suitable title and text.
$markdown = '[[Hello World]]';
// <p><a href="hello-world" title="Hello World">Hello World</a></p>
Looks-like text can be used to modify the title and text of the resulting a
tag.
$markdown = '[[Hello World|Welcome]]';
// <p><a href="hello-world" title="Welcome">Welcome</a></p>
You can optionally include hash links by adding a #
followed by the hash link text to the end of the Wikilink.
$markdown = '[[Hello World#Top]]';
// <p><a href="hello-world#top" title="Hello World > Top">Hello World > Top</a></p>
Of course, you can use looks-like text and hash links in combination.
$markdown = '[[Hello World#Top|Welcome]]';
// <p><a href="hello-world#top" title="Welcome">Welcome</a></p>
A prefix
configuration option can be used to prefix a string to the beginning of the href url.
$markdown = '[[Hello World#Top|Welcome]]';
(new MarkdownConverter($environment))->convert($markdown, [
'prefix' => 'articles/',
]
// <p><a href="articles/hello-world#top" title="Welcome">Welcome</a></p>
I've tried to think of all the edge cases, but there will doubtless be more. Please submit a bug report if you encounter any issues and I will try to resolve them.
$markdown = '[[]]'; //<p>[[]]</p>
$markdown = '[Hello World]'; // <p>[Hello World]</p>
$markdown = '[[Hello World'; // <p>[[Hello World</p>
$markdown = 'Hello World]]'; // <p>Hello World]]</p>
$markdown = '[[Hello World]]]]'; // <p>[[Hello World]]]]</p>
$markdown = '[[ Hello World]]'; // <p><a href="hello-world" title=" Hello World"> Hello World</a></p>
$markdown = '[[Hello World|||Welcome]]'; // <p>[[Hello World|||Welcome]]</p>
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you find a bug that impacts the security of this package please send an email to [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.