Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
1,400 additions
and
403 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Services\CommonMark; | ||
|
||
use League\CommonMark\Block\Element\FencedCode; | ||
use League\CommonMark\Block\Element\Heading; | ||
use League\CommonMark\Block\Element\IndentedCode; | ||
use League\CommonMark\CommonMarkConverter; | ||
use League\CommonMark\Environment; | ||
use Spatie\CommonMarkHighlighter\FencedCodeRenderer; | ||
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; | ||
|
||
class CommonMark | ||
{ | ||
public static function convertToHtml(string $markdown): string | ||
{ | ||
$environment = Environment::createCommonMarkEnvironment() | ||
->addBlockRenderer(FencedCode::class, new FencedCodeRenderer()) | ||
->addBlockRenderer(IndentedCode::class, new IndentedCodeRenderer()) | ||
->addBlockRenderer(Heading::class, new HeadingRenderer()); | ||
|
||
$commonMarkConverter = new CommonMarkConverter([], $environment); | ||
|
||
return $commonMarkConverter->convertToHtml($markdown); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Services\CommonMark; | ||
|
||
use League\CommonMark\Block\Element\AbstractBlock; | ||
use League\CommonMark\Block\Renderer\HeadingRenderer as BaseHeadingRenderer; | ||
use League\CommonMark\ElementRendererInterface; | ||
use League\CommonMark\HtmlElement; | ||
|
||
class HeadingRenderer extends BaseHeadingRenderer | ||
{ | ||
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) | ||
{ | ||
$element = parent::render($block, $htmlRenderer, $inTightList); | ||
|
||
$id = str_slug($element->getContents()); | ||
|
||
$element->setAttribute('id', $id); | ||
$element->setContents( | ||
$element->getContents() . | ||
new HtmlElement('a', ['href' => "#{$id}", 'class' => $this->getFragmentLinkClass($element->getTagName())], ' #') | ||
); | ||
|
||
return $element; | ||
} | ||
|
||
protected function getFragmentLinkClass($elementName) | ||
{ | ||
if ($elementName === 'h1') { | ||
return 'text-grey'; | ||
} | ||
|
||
if ($elementName === 'h2') { | ||
return 'text-grey'; | ||
} | ||
|
||
return 'text-grey-light'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.