Start replacing highlight js with tempest-highlight

This commit is contained in:
Tim
2025-05-27 00:58:48 +02:00
parent 5fcc32de6d
commit a741ee102d
10 changed files with 244 additions and 45 deletions

View File

@ -20,7 +20,7 @@ abstract class AbstractParser implements ParserInterface
try {
return $this->safeParseView($content);
} catch (\Exception $exception) {
return sprintf('<pre><code class="hljs">%s</code></pre>', htmlspecialchars($exception->getMessage()));
return sprintf('<pre><code>%s</code></pre>', htmlspecialchars($exception->getMessage()));
}
}

View File

@ -17,9 +17,9 @@ class GenericParser extends AbstractParser
$builder = new PipelineBuilder();
$pipeline = $builder
->add(new HtmlEscapeStage())
->add(new ReplaceBlocksStage('<pre>', '</pre>', '```'))
->add(new ReplaceBlocksStage('<code>', '</code>', '``'))
->add(new ReplaceStage(PHP_EOL, '<br>'))
->add(new ReplaceBlocksStage('<pre><code class="hljs">', '</code></pre>', '```'))
->add(new ReplaceBlocksStage('<code class="hljs">', '</code>', '``'))
->add($this->referenceStage)
->add($this->includeStage)
->build()

View File

@ -4,13 +4,14 @@ namespace App\Service\SnipParser\Generic;
use InvalidArgumentException;
use League\Pipeline\StageInterface;
use Tempest\Highlight\Highlighter;
class ReplaceBlocksStage implements StageInterface
readonly class ReplaceBlocksStage implements StageInterface
{
public function __construct(
public readonly string $openTag = '<pre><code>',
public readonly string $closeTag = '</code></pre>',
public readonly string $delimiter = '```'
public string $openTag = '<pre><code>',
public string $closeTag = '</code></pre>',
public string $delimiter = '```'
) {}
public function __invoke(mixed $payload): string
@ -26,8 +27,9 @@ class ReplaceBlocksStage implements StageInterface
{
$pattern = sprintf('/%s(.+?)%s/s', preg_quote($this->delimiter), preg_quote($this->delimiter));
return preg_replace_callback($pattern, function ($matches) {
return $this->openTag . trim($matches[1]) . $this->closeTag;
$highlighter = new Highlighter()->withGutter();
return preg_replace_callback($pattern, function ($matches) use ($highlighter) {
return $this->openTag . $highlighter->parse(trim($matches[1]), 'php') . $this->closeTag;
}, $text);
}
}

View File

@ -3,11 +3,14 @@
namespace App\Service\SnipParser\Html;
use App\Service\SnipParser\AbstractParser;
use Tempest\Highlight\Highlighter;
class HtmlParser extends AbstractParser
{
public function safeParseView(string $content): string
{
return sprintf('<pre><code class="hljs">%s</code></pre>', htmlspecialchars($content));
$highlighter = new Highlighter()->withGutter();
return '<pre data-lang="html" class="notranslate">' . $highlighter->parse($content, 'html') . '</pre>';
}
}

View File

@ -11,6 +11,8 @@ use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Node\Query;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\RouterInterface;
use Tempest\Highlight\CommonMark\HighlightExtension;
use Tempest\Highlight\Highlighter;
class MarkdownParser extends AbstractParser
{
@ -22,7 +24,13 @@ class MarkdownParser extends AbstractParser
public function safeParseView(string $content): string
{
$converter = new GithubFlavoredMarkdownConverter();
$converter->getEnvironment()->addEventListener(DocumentParsedEvent::class, $this->documentParsed(...));
$converter
->getEnvironment()
->addExtension(new HighlightExtension(
new Highlighter()->withGutter()
))
->addEventListener(DocumentParsedEvent::class, $this->documentParsed(...))
;
return $converter->convert($content);
}
@ -32,7 +40,8 @@ class MarkdownParser extends AbstractParser
$linkNodes = new Query()
->where(Query::type(Link::class))
->findAll($document);
->findAll($document)
;
foreach ($linkNodes as $linkNode) {
$url = $linkNode->getUrl();