3 namespace mdref\Formatter
;
8 use League\CommonMark\Extension
;
9 use League\CommonMark\GithubFlavoredMarkdownConverter
;
10 use League\CommonMark\Normalizer
;
12 use function file_get_contents
;
13 use function preg_replace
;
15 class League
extends Formatter
{
18 function __construct() {
19 $this->md
= new GithubFlavoredMarkdownConverter([
20 "slug_normalizer" => [
21 "instance" => new class implements Normalizer\TextNormalizerInterface
{
22 function normalize(string $text, $context = null) : string {
23 return preg_replace("/[^[:alnum:]:._-]/", ".", $text);
27 "heading_permalink" => [
28 "html_class" => "permalink",
30 "fragment_prefix" => "",
34 "min_heading_level" => 2,
37 $this->md
->getEnvironment()->addExtension(
38 new Extension\DescriptionList\DescriptionListExtension
40 $this->md
->getEnvironment()->addExtension(
41 new Extension\HeadingPermalink\HeadingPermalinkExtension
43 $this->md
->getEnvironment()->addExtension(
44 new Extension\Attributes\AttributesExtension
48 function formatString(string $string) : string {
49 return $this->md
->convertToHtml($string);
52 function formatFile(string $file) : string {
53 $string = file_get_contents($file);
54 if ($string === false) {
55 throw Exception
::fromLastError();
57 return $this->md
->convertToHtml($string);