-
Notifications
You must be signed in to change notification settings - Fork 546
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
c548128
commit aaa368b
Showing
10 changed files
with
121 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace App\Markdown; | ||
|
||
use Illuminate\Support\Str; | ||
use League\CommonMark\Extension\CommonMark\Renderer\Block\BlockQuoteRenderer as BaseBlockQuoteRenderer; | ||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use League\CommonMark\Util\HtmlElement; | ||
|
||
class BlockQuoteRenderer implements NodeRendererInterface | ||
{ | ||
/** | ||
* @return \Stringable|string|null | ||
* | ||
* @throws InvalidArgumentException if the wrong type of Node is provided | ||
*/ | ||
public function render(Node $node, ChildNodeRendererInterface $childRenderer) | ||
{ | ||
$element = (new BaseBlockQuoteRenderer($node))->render($node, $childRenderer); | ||
|
||
if (! $element instanceof HtmlElement || $element->getTagName() !== 'blockquote') { | ||
return $element; | ||
} | ||
|
||
$html = trim($element->getContents(true)); | ||
|
||
if (! str_starts_with($html, '<p>')) { | ||
return $element; | ||
} | ||
|
||
$htmlPartial = Str::after($html, '<p>'); | ||
|
||
if (preg_match('/^\[!(.*?)\]/', $htmlPartial, $matches)) { | ||
// GitHub styled notes, e.g., | ||
// > [!NOTE] Content | ||
// or | ||
// > [!NOTE] | ||
// > Content | ||
} elseif (preg_match('/^<strong>(.*?):?<\/strong>:?/', $htmlPartial, $matches)) { | ||
// Legacy GitHub styled notes, e.g., | ||
// > **Note:** Content | ||
// or | ||
// > **Note**: Content | ||
// or | ||
// > **Note** Content | ||
// or | ||
// > **Note:** | ||
// > Content | ||
// or | ||
// > **Note**: | ||
// > Content | ||
// or | ||
// > **Note** | ||
// > Content | ||
} elseif (preg_match('/^\{(.*?)\}/', $htmlPartial, $matches)) { | ||
// Legacy Laravel styled notes, e.g., | ||
// > {tip} Content | ||
// or | ||
// > {tip} | ||
// > Content | ||
} else { | ||
return $element; | ||
} | ||
|
||
[$replacing, $type] = $matches; | ||
|
||
$bits = match ($type) { | ||
'WARNING', 'Warning', 'note' => [Vite::content('resources/images/exclamation.svg'), 'bg-red-600'], | ||
'NOTE', 'Note', 'tip' => [Vite::content('resources/images/lightbulb.svg'), 'bg-purple-600'], | ||
'video' => [Vite::content('resources/images/laracast.svg'), 'bg-blue-600'], | ||
default => null, | ||
}; | ||
|
||
if ($bits === null) { | ||
return $element; | ||
} | ||
|
||
$body = str_replace($replacing, '', $htmlPartial); | ||
|
||
[$asset, $class] = $bits; | ||
|
||
return <<<HTML | ||
<div class="mb-10 max-w-2xl mx-auto px-4 py-8 shadow-lg lg:flex lg:items-center callout"> | ||
<div class="w-20 h-20 mb-6 flex items-center justify-center shrink-0 lg:mb-0 {$class}"> | ||
<div class="opacity-75"> | ||
{$asset} | ||
</div> | ||
</div> | ||
<p class="mb-0 lg:ml-6 callout">{$body} | ||
</div> | ||
HTML; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
public/img/callouts/exclamation.min.svg → resources/images/exclamation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
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