Skip to content

An extension for league-commonmark that lets you use Wikilinks in your markdown files.

License

Notifications You must be signed in to change notification settings

cassarco/league-commonmark-wikilinks

Repository files navigation

league-commonmark-wikilinks

Latest Version on Packagist Tests Total Downloads

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.

Installation

You can install the package via composer:

composer require cassarco/league-commonmark-wikilinks

Usage

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>

Features

Convert Wikilinks to HTML Links

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>

Support for Looks-like Text

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>

Support for Hash Links

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 &gt; Top">Hello World &gt; Top</a></p>

Support for Hash Links combined with Looks-like Text

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>

Support for Link Prefixes

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>

Handles Edge Cases Gracefully

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>

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

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.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

An extension for league-commonmark that lets you use Wikilinks in your markdown files.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages