Skip to content

Commit

Permalink
podmiana parsedown na commonmark
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-boduch committed Feb 5, 2022
1 parent 6d36398 commit 1499bc7
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 424 deletions.
6 changes: 1 addition & 5 deletions app/Services/Parser/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public function detach()
$this->parsers = [];
}

/**
* @param $text
* @return mixed
*/
public function parse($text)
public function parse(string $text): string
{
foreach ($this->parsers as $parser) {
$text = $parser->parse($text);
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Parser/Factories/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Cache
/**
* @var bool
*/
protected $enable = true;
protected $enable = false;

/**
* @var
Expand Down
16 changes: 6 additions & 10 deletions app/Services/Parser/Factories/CommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ public function parse(string $text) : string

if (!$isInCache) {
$text = $this->cache($text, function () use ($parser) {
$parser->attach(
(new SimpleMarkdown($this->app[UserRepositoryInterface::class]))
->setEnableHashParser($this->enableHashParser)
->setBreaksEnabled($this->enableLineBreaks)
);

$parser->attach(
(new Purifier())->set('HTML.Allowed', $this->getHtmlTags())
);
$parser->attach(new Link($this->app[PageRepositoryInterface::class], $this->request->getHost()));
$parser->attach(new SimpleMarkdown($this->app[UserRepositoryInterface::class]));

// $parser->attach(
// (new Purifier())->set('HTML.Allowed', $this->getHtmlTags())
// );
// $parser->attach(new Link($this->app[PageRepositoryInterface::class], $this->request->getHost()));
$parser->attach(new Censore($this->app[WordRepositoryInterface::class]));

if (!empty($this->userId)) {
Expand Down
6 changes: 3 additions & 3 deletions app/Services/Parser/Factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function parse(string $text) : string

if (!$isInCache) {
$text = $this->cache($text, function () use ($parser) {
$parser->attach((new Markdown($this->app[UserRepositoryInterface::class]))->setBreaksEnabled(true));
$parser->attach((new Markdown($this->app[UserRepositoryInterface::class])));
$parser->attach(new Latex());
$parser->attach(new Purifier());
$parser->attach(new Link($this->app[PageRepositoryInterface::class], $this->request->getHost(), $this->app['html']));
// $parser->attach(new Purifier());
// $parser->attach(new Link($this->app[PageRepositoryInterface::class], $this->request->getHost(), $this->app['html']));
$parser->attach(new Censore($this->app[WordRepositoryInterface::class]));
$parser->attach(new Prism());

Expand Down
38 changes: 38 additions & 0 deletions app/Services/Parser/MentionGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Coyote\Services\Parser;

use League\CommonMark\Extension\CommonMark\Node\Inline\Strong;
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
use League\CommonMark\Extension\Mention\Mention;
use League\CommonMark\Node\Inline\AbstractInline;
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository;
use League\CommonMark\Node\Inline\Text;

class MentionGenerator implements MentionGeneratorInterface
{
public function __construct(private UserRepository $user)
{
}

public function generateMention(Mention $mention): ?AbstractInline
{
$identifier = $this->stripIdentifier($mention->getIdentifier());
$user = $this->user->findByName($identifier);

if ($user) {
$mention->setUrl(route('profile', [$user->id]));
$mention->setLabel('@' . $user->name);
$mention->data->set('attributes', ['class' => 'mention', 'data-user-id' => (string) $user->id]);

return $mention;
}

return new Text('@' . $identifier);
}

private function stripIdentifier(string $identifier): string
{
return trim($identifier, '{}');
}
}
2 changes: 1 addition & 1 deletion app/Services/Parser/Parsers/Censore.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(WordRepository $word)
* @param string $text
* @return string
*/
public function parse($text)
public function parse(string $text): string
{
static $result;

Expand Down
28 changes: 12 additions & 16 deletions app/Services/Parser/Parsers/Emphasis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class Emphasis extends Parser implements ParserInterface
/**
* @var int
*/
protected $userId;
protected int $userId;

/**
* @var string
*/
protected $ability;
protected string $ability;

/**
* @var UserRepository
*/
protected $user;
protected UserRepository $user;

/**
* @param UserRepository $user
Expand All @@ -36,7 +36,7 @@ public function __construct(UserRepository $user)
* @param string $ability
* @return $this
*/
public function setAbility($ability)
public function setAbility(string $ability): static
{
$this->ability = $ability;

Expand All @@ -47,7 +47,7 @@ public function setAbility($ability)
* @param int $userId
* @return $this
*/
public function setUserId($userId)
public function setUserId(int $userId): static
{
$this->userId = $userId;

Expand All @@ -58,9 +58,9 @@ public function setUserId($userId)
* @param string $text
* @return string
*/
public function parse($text)
public function parse(string $text): string
{
if (substr($text, 0, 1) !== '!') {
if (!str_starts_with($text, '!')) {
return $text;
}

Expand All @@ -70,18 +70,14 @@ public function parse($text)
return $text;
}

if ('!!' === substr($text, 0, 2)) {
return $this->emphasisWithColor($text);
} else {
return $this->emphasisWithBold($text);
}
return (str_starts_with($text, '!!')) ? $this->emphasisWithColor($text) : $this->emphasisWithBold($text);
}

/**
* @param string $text
* @return string
*/
private function emphasisWithBold($text)
private function emphasisWithBold(string $text): string
{
return $this->emphasis($text);
}
Expand All @@ -90,7 +86,7 @@ private function emphasisWithBold($text)
* @param string $text
* @return string
*/
private function emphasisWithColor($text)
private function emphasisWithColor(string $text): string
{
return $this->emphasis($text, ['style' => 'color: ' . self::COLOR]);
}
Expand All @@ -100,15 +96,15 @@ private function emphasisWithColor($text)
* @param array $attributes
* @return string
*/
private function emphasis($text, array $attributes = [])
private function emphasis(string $text, array $attributes = []): string
{
return (string) $this->getHtml()->tag('strong', ltrim($text, '!'), $attributes);
}

/**
* @return HtmlBuilder
*/
protected function getHtml()
protected function getHtml(): HtmlBuilder
{
return app(HtmlBuilder::class);
}
Expand Down
6 changes: 1 addition & 5 deletions app/Services/Parser/Parsers/Latex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

class Latex extends Parser implements ParserInterface
{
/**
* @param string $text
* @return string
*/
public function parse($text)
public function parse(string $text): string
{
$text = $this->hashBlock($text, ['code', 'a']);
$text = $this->hashInline($text, 'img');
Expand Down
Loading

0 comments on commit 1499bc7

Please sign in to comment.