Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switched code highlighting from js to php implementation #153

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./output_prod
cname: thephp.foundation
cname: thephp.foundation
2 changes: 2 additions & 0 deletions app/SculpinKernel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Bundles\AtomFeedGeneratorBundle\SculpinAtomFeedGeneratorBundle;
use App\Bundles\HighlightBundle\SculpinHighlightBundle;
use App\Bundles\MermaidBundle\SculpinMermaidBundle;
use App\Bundles\SharingImageGeneratorBundle\SculpinSharingImageGeneratorBundle;
use Sculpin\Bundle\SculpinBundle\HttpKernel\AbstractKernel;
Expand All @@ -13,6 +14,7 @@ protected function getAdditionalSculpinBundles(): array
SculpinAtomFeedGeneratorBundle::class,
SculpinSharingImageGeneratorBundle::class,
SculpinMermaidBundle::class,
SculpinHighlightBundle::class,
];
}
}
75 changes: 75 additions & 0 deletions app/src/Bundles/HighlightBundle/CodeHighlighting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Bundles\HighlightBundle;

use Dflydev\DotAccessConfiguration\Configuration;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Sculpin\Core\Event\ConvertEvent;
use Sculpin\Core\Sculpin;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tempest\Highlight\CommonMark\HighlightExtension;
use Tempest\Highlight\Highlighter;

readonly class CodeHighlighting implements EventSubscriberInterface
{
# Pattern to match both fenced code blocks and inline code blocks
private const string CODE_PATTERN = '/(`{3,}[^`]*`{3,}|`[^`\n]+`|```[^`]*```)/m';
# Pattern to match inline code blocks (`code`)
private const string INLINE_CODE_PATTERN = '/`([^`\n]+)`/m';

private MarkdownConverter $markdownConverter;

public function __construct(private Configuration $configuration, private Highlighter $highlighter)
{
$environment = new Environment();
$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new HighlightExtension($highlighter))
;

$this->markdownConverter = new MarkdownConverter($environment);
}

public static function getSubscribedEvents(): array
{
return [
Sculpin::EVENT_BEFORE_CONVERT => 'beforeConvert',
];
}

public function beforeConvert(ConvertEvent $event): void
{
$source = $event->source();
$content = $source->content();

if ($content !== null && $source->file()->getExtension() === 'md') {
$content = $this->removeLeadingIndentation($content);

$htmlContent = preg_replace_callback(self::CODE_PATTERN, function ($matches) {
[$codeBlock] = $matches;

if ($this->isInlineCodeBlock($codeBlock)) {
$codeBlock = sprintf('`{php} %s`', trim($codeBlock, '`'));
}

$convertedContent = $this->markdownConverter->convert($codeBlock)->getContent();

return preg_replace('/^<p>(.*)<\/p>$/s', '$1', $convertedContent);
}, $content);

$source->setContent($htmlContent);
}
}

private function isInlineCodeBlock(string $codeBlock): bool
{
return preg_match(self::INLINE_CODE_PATTERN, $codeBlock) && !preg_match('/{php}/', $codeBlock);
}

private function removeLeadingIndentation(string $content): string
{
return preg_replace('/^(?:\t| )/m', '', $content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Bundles\HighlightBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Extension\Extension;

class SculpinHighlightExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
24 changes: 24 additions & 0 deletions app/src/Bundles/HighlightBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="inline_theme" class="Tempest\Highlight\Themes\InlineTheme">
<argument>%kernel.project_dir%/vendor/tempest/highlight/src/Themes/Css/highlight-light-lite.css</argument>
</service>

<service id="highlighter" class="Tempest\Highlight\Highlighter">
<argument type="service" id="inline_theme"/>
</service>

<service id="sculpin_highlight.code_highlighting" class="App\Bundles\HighlightBundle\CodeHighlighting">
<argument type="service" id="sculpin.site_configuration"/>
<argument type="service" id="highlighter"/>

<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
9 changes: 9 additions & 0 deletions app/src/Bundles/HighlightBundle/SculpinHighlightBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Bundles\HighlightBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SculpinHighlightBundle extends Bundle
{
}
19 changes: 17 additions & 2 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,29 @@ footer > ul, footer > p {
padding: 0;
}

pre {
max-width: 100%;
overflow-x: auto;
width: 100%;
padding: 0.2em 0.4em;
border-radius: 0.375rem;
font-size: 85%;
background: rgba(135, 131, 120, 0.15);
margin-bottom: 1em;
}

pre code {
padding: 0;
background: transparent;
}

code {
font-family: Consolas, "PT Mono", "Liberation Mono", Courier, monospace;
line-height: normal;
background: rgba(135, 131, 120, 0.15);
color: #EB5757;
border-radius: 3px;
font-size: 85%;
padding: 0.2em 0.4em;
background: rgba(135, 131, 120, 0.15);
}

.alert {
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
"php": "^8.3",
"ext-gd": "*",
"ext-dom": "*",
"sculpin/sculpin": "3.3.0-alpha3",
"symfony/process": "^6.4|^7.0"
"sculpin/sculpin": "dev-main",
"symfony/process": "^6.4|^7.0",
"tempest/highlight": "^2.7",
"league/commonmark": "^2.4.2"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/pekhov14/sculpin"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan on maintaining this fork? I don't think it's a good idea for the website to depend on "dev-main" on a third party composer repo like this. If you intend to maintain this fork, register it under a different vendor name on packagist.org? Or better yet get this merged into sculpin itself? Either way I don't think this reference to a third party repo overwriting sculpin is a good idea to merge into the php foundation website.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naderman Yes, I made a pull request and it was accepted, I will update the code soon.

}
],
"scripts": {
"sculpin-watch": [
"Composer\\Config::disableProcessTimeout",
Expand Down
Loading
Loading