Skip to content
Open
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
71 changes: 55 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,73 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.3, 7.4, 8.0 ]
laravel: [ 7.*, 8.* ]
php: [ 8.0, 8.1, 8.2, 8.3 ]
laravel: [ 8.*, 9.*, 10.*, 11.*, 12.*, 13.* ]
commonmark: [ ^1.5, ^2.0 ]
dependency-version: [ prefer-lowest, prefer-stable ]
include:
- laravel: 7.*
testbench: 5.*

- laravel: 8.*
testbench: 6.*
testbench: ^6.0
- laravel: 9.*
testbench: ^7.0
- laravel: 10.*
testbench: ^8.0
- laravel: 11.*
testbench: ^9.0
- laravel: 12.*
testbench: ^10.0
- laravel: 13.*
testbench: ^11.0

exclude:
# Commonmark 2.0 requires PHP 7.4
- commonmark: ^2.0
php: 7.3
# Laravel 8 with commonmark 1 only needs one PHP version
- laravel: 8.*
commonmark: ^1.5
php: 8.1
- laravel: 8.*
commonmark: ^1.5
php: 8.2
- laravel: 8.*
commonmark: ^1.5
php: 8.3

# Laravel 9+ requires PHP 8.0+, already satisfied by our matrix
# Laravel 10 requires PHP 8.1+
- laravel: 10.*
php: 8.0

# Laravel 11+ requires PHP 8.2+
- laravel: 11.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 12.*
php: 8.0
- laravel: 12.*
php: 8.1
- laravel: 13.*
php: 8.0
- laravel: 13.*
php: 8.1

# Laravel 7 requires commonmark 1
- commonmark: ^2.0
laravel: 7.*
# commonmark 1 is only relevant for older setups; skip for L10+
- laravel: 10.*
commonmark: ^1.5
- laravel: 11.*
commonmark: ^1.5
- laravel: 12.*
commonmark: ^1.5
- laravel: 13.*
commonmark: ^1.5

name: P${{ matrix.php }} - L${{ matrix.laravel }} - C${{ matrix.commonmark }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
Expand All @@ -49,7 +88,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
coverage: none

- name: Install dependencies
Expand All @@ -59,4 +98,4 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/phpunit
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

## 0.5.6 - 2022-07-25

### Fixed

- Respect HTML attributes from AttributesExtension on the `<pre>` block

## 0.5.5 - 2022-02-23

### Changed
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
}
],
"require": {
"php": "^7.2|^8.0",
"torchlight/torchlight-laravel": "^0.5.10",
"php": "^7.3|^8.0",
"torchlight/torchlight-laravel": "^0.6.0",
"league/commonmark": "^1.5|^2.0"
},
"require-dev": {
"orchestra/testbench": "^5.0|^6.0",
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"mockery/mockery": "^1.3.3",
"phpunit/phpunit": "^8.4"
"phpunit/phpunit": "^9.0|^10.0|^11.0"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 1 addition & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Torchlight Commonmark Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
</php>
</phpunit>
</phpunit>
12 changes: 9 additions & 3 deletions src/BaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Str;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Util\HtmlElement;
use League\CommonMark\Util\Xml;
use Torchlight\Block;
use Torchlight\Torchlight;
Expand Down Expand Up @@ -65,7 +67,7 @@ public function useCustomBlockRenderer($callback)
*/
public function defaultBlockRenderer()
{
return function (Block $block) {
return function (Block $block, AbstractBlock $node) {
$inner = '';

// Clones come from multiple themes.
Expand All @@ -76,7 +78,11 @@ public function defaultBlockRenderer()
$inner .= "<code {$block->attrsAsString()}class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code>";
}

return "<pre>$inner</pre>";
return new HtmlElement(
'pre',
$node->data->getData('attributes')->export(),
$inner
);
};
}

Expand Down Expand Up @@ -143,7 +149,7 @@ protected function renderNode($node)
if (array_key_exists($hash, static::$torchlightBlocks)) {
$renderer = $this->customBlockRenderer ?? $this->defaultBlockRenderer();

return call_user_func($renderer, static::$torchlightBlocks[$hash]);
return call_user_func($renderer, static::$torchlightBlocks[$hash], $node);
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/BaseRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,47 @@ public function it_can_set_a_custom_renderer()
$expected = <<<EOT
foo_bar

EOT;

$this->assertEquals($expected, $html);
}

/** @test */
public function sets_attributes_on_pre()
{
$markdown = <<<'EOT'
before

{.large}
```html
<div>html</div>
```
after
EOT;

$response = [
'blocks' => [[
'id' => 'block_id_1',
'classes' => 'torchlight',
'styles' => 'color: red;',
'attrs' => [
'data-lang' => 'lang'
],
'highlighted' => 'highlighted',
]]
];

Http::fake([
'api.torchlight.dev/*' => Http::response($response, 200),
]);

$html = $this->render($markdown);

$expected = <<<EOT
<p>before</p>
<pre class="large"><code data-lang="lang" class='torchlight' style='color: red;'>highlighted</code></pre>
<p>after</p>

EOT;

$this->assertEquals($expected, $html);
Expand Down
2 changes: 2 additions & 0 deletions tests/V1/CodeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\HtmlRenderer;
use Torchlight\Commonmark\Tests\BaseRendererTest;

Expand All @@ -22,6 +23,7 @@ protected function extension()
protected function render($markdown, $extension = null)
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new AttributesExtension);
$environment->addExtension($extension ?? $this->extension());

$parser = new DocParser($environment);
Expand Down
2 changes: 2 additions & 0 deletions tests/V2/CodeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Torchlight\Commonmark\Test\V2;

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Parser\MarkdownParser;
use League\CommonMark\Renderer\HtmlRenderer;
Expand All @@ -24,6 +25,7 @@ protected function render($markdown, $extension = null)
{
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension);
$environment->addExtension(new AttributesExtension);
$environment->addExtension($extension ?? $this->extension());

$parser = new MarkdownParser($environment);
Expand Down