forked from themsaid/ibis
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bedb2f2
commit 8dacfae
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
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,14 @@ | ||
<?php | ||
|
||
namespace Ibis\Markdown\Extensions; | ||
|
||
use League\CommonMark\Node\Block\AbstractBlock; | ||
|
||
class Aside extends AbstractBlock | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Ibis\Markdown\Extensions; | ||
|
||
use League\CommonMark\Node\Block\AbstractBlock; | ||
use League\CommonMark\Parser\Block\BlockContinue; | ||
use League\CommonMark\Parser\Block\BlockContinueParserInterface; | ||
use League\CommonMark\Parser\Cursor; | ||
|
||
class AsideBlockParser implements BlockContinueParserInterface | ||
{ | ||
private readonly Aside $aside; | ||
|
||
public function __construct() | ||
{ | ||
$this->aside = new Aside(); | ||
} | ||
|
||
public function getBlock(): AbstractBlock | ||
{ | ||
return $this->aside; | ||
} | ||
|
||
public function isContainer(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function canHaveLazyContinuationLines(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function canContain(AbstractBlock $childBlock): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue | ||
{ | ||
if ($cursor->getLine() === ":::") { | ||
return BlockContinue::finished(); | ||
} | ||
|
||
return BlockContinue::at($cursor); | ||
} | ||
|
||
public function addLine(string $line): void {} | ||
|
||
public function closeBlock(): void {} | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace Ibis\Markdown\Extensions; | ||
|
||
use League\CommonMark\Environment\EnvironmentBuilderInterface; | ||
use League\CommonMark\Extension\ExtensionInterface; | ||
|
||
class AsideExtension implements ExtensionInterface | ||
{ | ||
public function register(EnvironmentBuilderInterface $environment): void | ||
{ | ||
$environment->addBlockStartParser(new AsideParser()) | ||
->addRenderer(Aside::class, new AsideRenderer()); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Ibis\Markdown\Extensions; | ||
|
||
use League\CommonMark\Parser\Block\BlockStart; | ||
use League\CommonMark\Parser\Block\BlockStartParserInterface; | ||
use League\CommonMark\Parser\Cursor; | ||
use League\CommonMark\Parser\MarkdownParserStateInterface; | ||
|
||
class AsideParser implements BlockStartParserInterface | ||
{ | ||
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart | ||
{ | ||
if (!str_starts_with($cursor->getRemainder(), ':::note')) { | ||
return BlockStart::none(); | ||
} | ||
|
||
$cursor->advanceToNextNonSpaceOrTab(); | ||
$cursor->advanceToEnd(); | ||
|
||
return BlockStart::of(new AsideBlockParser())->at($cursor); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Ibis\Markdown\Extensions; | ||
|
||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use League\CommonMark\Util\HtmlElement; | ||
|
||
class AsideRenderer implements NodeRendererInterface | ||
{ | ||
public function render(Node $node, ChildNodeRendererInterface $childRenderer) | ||
{ | ||
Aside::assertInstanceOf($node); | ||
|
||
$attrs = $node->data->getData('attributes'); | ||
$contents = $childRenderer->renderNodes($node->children()); | ||
|
||
return new HtmlElement( | ||
'blockquote', | ||
['class' => 'notice'], | ||
new HtmlElement('div', $attrs->export(), $contents) | ||
); | ||
} | ||
} |