Skip to content

Commit

Permalink
Switch to CommonMark (#51)
Browse files Browse the repository at this point in the history
* wip

* wip
  • Loading branch information
freekmurze committed Nov 29, 2018
1 parent b6ccb32 commit 6db1490
Show file tree
Hide file tree
Showing 13 changed files with 1,400 additions and 403 deletions.
96 changes: 0 additions & 96 deletions app/Console/Commands/ImportWp.php

This file was deleted.

6 changes: 0 additions & 6 deletions app/Console/Kernel.php
Expand Up @@ -8,10 +8,6 @@

class Kernel extends ConsoleKernel
{
protected $commands = [
ImportWp::class,
];

protected function schedule(Schedule $schedule)
{
$schedule->command('responsecache:flush')->daily()->at('00:00');
Expand All @@ -23,7 +19,5 @@ protected function schedule(Schedule $schedule)
protected function commands()
{
$this->load(__DIR__.'/Commands');

//require base_path('routes/console.php');
}
}
3 changes: 2 additions & 1 deletion app/Models/Post.php
Expand Up @@ -5,6 +5,7 @@
use App\Jobs\PostOnMedium;
use App\Jobs\SendTweet;
use App\Models\Presenters\PostPresenter;
use App\Services\CommonMark\CommonMark;
use App\Services\Parsedown;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function scopePublished(Builder $query)

public function getFormattedTextAttribute()
{
return (new Parsedown())->text($this->text);
return CommonMark::convertToHtml($this->text);
}

public function updateAttributes(array $attributes)
Expand Down
26 changes: 26 additions & 0 deletions app/Services/CommonMark/CommonMark.php
@@ -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);
}
}
39 changes: 39 additions & 0 deletions app/Services/CommonMark/HeadingRenderer.php
@@ -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';
}
}
36 changes: 0 additions & 36 deletions app/Services/Parsedown.php

This file was deleted.

1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -32,6 +32,7 @@
"laravel/scout": "^3.0",
"laravel/tinker": "~1.0",
"ohdearapp/nova-ohdear-tool": "dev-master",
"spatie/commonmark-highlighter": "^0.1.0",
"spatie/laravel-backup": "^5.1",
"spatie/laravel-csp": "^1.0",
"spatie/laravel-feed": "^2.0",
Expand Down

0 comments on commit 6db1490

Please sign in to comment.