|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Torchlight\Commonmark; |
| 4 | + |
| 5 | + |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use League\CommonMark\Environment\EnvironmentBuilderInterface; |
| 8 | +use League\CommonMark\Event\DocumentParsedEvent; |
| 9 | +use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; |
| 10 | +use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode; |
| 11 | +use League\CommonMark\Extension\ExtensionInterface; |
| 12 | +use League\CommonMark\Node\Node; |
| 13 | +use League\CommonMark\Renderer\ChildNodeRendererInterface; |
| 14 | +use League\CommonMark\Renderer\NodeRendererInterface; |
| 15 | +use League\CommonMark\Util\Xml; |
| 16 | +use Torchlight\Block; |
| 17 | +use Torchlight\Torchlight; |
| 18 | + |
| 19 | +class TorchlightExtensionV2 implements ExtensionInterface, NodeRendererInterface |
| 20 | +{ |
| 21 | + public static $torchlightBlocks = []; |
| 22 | + |
| 23 | + public function register(EnvironmentBuilderInterface $environment): void |
| 24 | + { |
| 25 | + // We start by walking the document immediately after it's parsed |
| 26 | + // to gather up all the code blocks and send off our requests. |
| 27 | + $environment->addEventListener(DocumentParsedEvent::class, [$this, 'onDocumentParsed']); |
| 28 | + |
| 29 | + // After the document is parsed, it's rendered. We register our |
| 30 | + // renderers with a higher priority than the default ones, |
| 31 | + // and we'll fetch the blocks straight from the cache. |
| 32 | + $environment->addRenderer(FencedCode::class, $this, 10); |
| 33 | + $environment->addRenderer(IndentedCode::class, $this, 10); |
| 34 | + } |
| 35 | + |
| 36 | + public function onDocumentParsed(DocumentParsedEvent $event) |
| 37 | + { |
| 38 | + $walker = $event->getDocument()->walker(); |
| 39 | + |
| 40 | + while ($event = $walker->next()) { |
| 41 | + /** @var FencedCode|IndentedCode $node */ |
| 42 | + $node = $event->getNode(); |
| 43 | + |
| 44 | + // Only look for code nodes, and only process them upon entering. |
| 45 | + if (!$this->isCodeNode($node) || !$event->isEntering()) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + $block = $this->makeTorchlightBlock($node); |
| 50 | + |
| 51 | + // Set by hash instead of ID, because we'll be remaking all the |
| 52 | + // blocks in the `render` function so the ID will be different, |
| 53 | + // but the hash will always remain the same. |
| 54 | + static::$torchlightBlocks[$block->hash()] = $block; |
| 55 | + } |
| 56 | + |
| 57 | + // All we need to do is fire the request, which will store |
| 58 | + // the results in the cache. In the render function we |
| 59 | + // use that cached value. |
| 60 | + Torchlight::highlight(static::$torchlightBlocks); |
| 61 | + } |
| 62 | + |
| 63 | + public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
| 64 | + { |
| 65 | + $hash = $this->makeTorchlightBlock($node)->hash(); |
| 66 | + |
| 67 | + if (array_key_exists($hash, static::$torchlightBlocks)) { |
| 68 | + $renderer = $this->customBlockRenderer ?? $this->defaultBlockRenderer(); |
| 69 | + |
| 70 | + return call_user_func($renderer, static::$torchlightBlocks[$hash]); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public function useCustomBlockRenderer($callback) |
| 75 | + { |
| 76 | + $this->customBlockRenderer = $callback; |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + public function defaultBlockRenderer() |
| 82 | + { |
| 83 | + return function (Block $block) { |
| 84 | + return "<pre><code class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code></pre>"; |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + protected function makeTorchlightBlock($node) |
| 89 | + { |
| 90 | + return Block::make() |
| 91 | + ->language($this->getLanguage($node)) |
| 92 | + ->theme($this->getTheme($node)) |
| 93 | + ->code($this->getContent($node)); |
| 94 | + } |
| 95 | + |
| 96 | + protected function isCodeNode($node) |
| 97 | + { |
| 98 | + return $node instanceof FencedCode || $node instanceof IndentedCode; |
| 99 | + } |
| 100 | + |
| 101 | + protected function getContent($node) |
| 102 | + { |
| 103 | + $content = $node->getLiteral(); |
| 104 | + |
| 105 | + if (!Str::startsWith($content, '<<<')) { |
| 106 | + return $content; |
| 107 | + } |
| 108 | + |
| 109 | + $file = trim(Str::after($content, '<<<')); |
| 110 | + |
| 111 | + // It must be only one line, because otherwise it might be a heredoc. |
| 112 | + if (count(explode("\n", $file)) > 1) { |
| 113 | + return $content; |
| 114 | + } |
| 115 | + |
| 116 | + return Torchlight::processFileContents($file) ?: $content; |
| 117 | + } |
| 118 | + |
| 119 | + protected function getInfo($node) |
| 120 | + { |
| 121 | + if (!$this->isCodeNode($node) || $node instanceof IndentedCode) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + $infoWords = $node->getInfoWords(); |
| 126 | + |
| 127 | + return empty($infoWords) ? [] : $infoWords; |
| 128 | + } |
| 129 | + |
| 130 | + protected function getLanguage($node) |
| 131 | + { |
| 132 | + $language = $this->getInfo($node)[0]; |
| 133 | + |
| 134 | + return $language ? Xml::escape($language, true) : null; |
| 135 | + } |
| 136 | + |
| 137 | + protected function getTheme($node) |
| 138 | + { |
| 139 | + foreach ($this->getInfo($node) as $item) { |
| 140 | + if (Str::startsWith($item, 'theme:')) { |
| 141 | + return Str::after($item, 'theme:'); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments