Skip to content

Commit 8231102

Browse files
authored
Merge pull request #17 from 8fold/partials
add: CommonMark partials
2 parents aca0e5d + b8d78bd commit 8231102

File tree

6 files changed

+148
-3
lines changed

6 files changed

+148
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"league/commonmark": "^2.0",
1515
"symfony/yaml": "^2.3 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
1616
"8fold/commonmark-abbreviations": "^1.2 || ^2.1",
17-
"8fold/commonmark-accessible-heading-permalinks": "^1.0"
17+
"8fold/commonmark-accessible-heading-permalinks": "^1.0",
18+
"8fold/commonmark-partials": "^1.0"
1819
},
1920
"require-dev": {
2021
"phpstan/phpstan": "^1.2.0",

composer.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Markdown.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Eightfold\CommonMarkAbbreviations\AbbreviationExtension as Abbreviations;
1111
use Eightfold\CommonMarkAccessibleHeadingPermalink\HeadingPermalinkExtension
1212
as HeadingPermalink;
13+
use Eightfold\CommonMarkPartials\PartialsExtension as Partials;
1314

1415
class Markdown extends FluentCommonMark
1516
{
@@ -74,7 +75,7 @@ public function abbreviations(): Markdown
7475
}
7576

7677
/**
77-
* @param array<string, mixed> $config [description]
78+
* @param array<string, mixed> $config
7879
*/
7980
public function accessibleHeadingPermalinks(array $config = []): Markdown
8081
{
@@ -86,4 +87,18 @@ public function accessibleHeadingPermalinks(array $config = []): Markdown
8687
$config
8788
)->addExtension(new HeadingPermalink());
8889
}
90+
91+
/**
92+
* @param array<string, mixed> $config
93+
*/
94+
public function partials(array $config = []): Markdown
95+
{
96+
if (count($config) === 0) {
97+
return $this;
98+
}
99+
return $this->addExtensionWithConfig(
100+
'partials',
101+
$config
102+
)->addExtension(new Partials());
103+
}
89104
}

tests/MarkdownBaselineTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,43 @@ public function can_use_accessible_heading_permalinks(): void // phpcs:ignore
149149

150150
$this->assertSame($expected, $result);
151151
}
152+
153+
/**
154+
* @test
155+
*/
156+
public function can_use_partials(): void // phpcs: ignore
157+
{
158+
$expected = <<<html
159+
<p>This was created by a partial</p>
160+
161+
html;
162+
163+
$result = Markdown::create()->partials([
164+
'partials' => [
165+
'inject' => 'Eightfold\Markdown\Tests\Mocks\PartialMarkdown'
166+
]
167+
])->convert(<<<md
168+
{!! inject !!}
169+
md
170+
);
171+
172+
$this->assertSame($expected, $result);
173+
174+
$expected = <<<html
175+
<h1>This was created by a partial</h1>
176+
177+
html;
178+
179+
$result = Markdown::create()->withConfig(['html_input' => 'allow'])
180+
->partials([
181+
'partials' => [
182+
'inject' => 'Eightfold\Markdown\Tests\Mocks\PartialHtml'
183+
]
184+
])->convert(<<<md
185+
{!! inject !!}
186+
md
187+
);
188+
189+
$this->assertSame($expected, $result);
190+
}
152191
}

tests/Mocks/PartialHtml.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Eightfold\Markdown\Tests\Mocks;
5+
6+
use Eightfold\CommonMarkPartials\PartialInterface;
7+
8+
use Eightfold\CommonMarkPartials\PartialInput;
9+
10+
class PartialHtml implements PartialInterface
11+
{
12+
public function __invoke(PartialInput $input, array $extras = []): string
13+
{
14+
return '<h1>This was created by a partial</h1>';
15+
}
16+
}

tests/Mocks/PartialMarkdown.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Eightfold\Markdown\Tests\Mocks;
5+
6+
use Eightfold\CommonMarkPartials\PartialInterface;
7+
8+
use Eightfold\CommonMarkPartials\PartialInput;
9+
10+
class PartialMarkdown implements PartialInterface
11+
{
12+
public function __invoke(PartialInput $input, array $extras = []): string
13+
{
14+
return 'This was created by a partial';
15+
}
16+
}

0 commit comments

Comments
 (0)