-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
142533a
switched code highlighting from js to php implementation
Pekhov14 7707230
Fixed highlighting of multi-line code block
Pekhov14 28a7aff
Changed implementation of code highlighting
Pekhov14 eacd75b
Added removal of one tab or four spaces at the beginning of each line
Pekhov14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/Bundles/HighlightBundle/DependencyInjection/SculpinHighlightExtension.php
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,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
24
app/src/Bundles/HighlightBundle/Resources/config/services.xml
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,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> |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Bundles\HighlightBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class SculpinHighlightBundle extends Bundle | ||
{ | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.