Skip to content

Commit 0108b87

Browse files
authored
Merge pull request #1413 from hydephp/refactor-colored-blockquote-internals
Refactor colored blockquote internals to extract class name from signature
2 parents 285f819 + e90de61 commit 0108b87

File tree

5 files changed

+85
-51
lines changed

5 files changed

+85
-51
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This serves two purposes:
2222
- Updated the navigation menu generator to remove duplicates after running the sorting method in https://github.com/hydephp/develop/pull/1407 (fixes https://github.com/hydephp/develop/issues/1406)
2323
- Updated the exception message caused by missing source option for featured images to include the path of the file that caused the error in https://github.com/hydephp/develop/pull/1409
2424
- Narrows down parsed `BladeMatter` array types to `array<string, scalar>` (Experimental feature not covered by BC promise) in https://github.com/hydephp/develop/pull/1410
25-
- Internal code refactors and improvements in https://github.com/hydephp/develop/pull/1410 and https://github.com/hydephp/develop/pull/1411
25+
- Internal code refactors and improvements in https://github.com/hydephp/develop/pull/1410, https://github.com/hydephp/develop/pull/1411, and https://github.com/hydephp/develop/pull/1413
2626

2727
### Deprecated
2828
- for soon-to-be removed features.

packages/framework/src/Markdown/Processing/ColoredBlockquotes.php

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
use Hyde\Markdown\Contracts\MarkdownShortcodeContract;
88
use Hyde\Markdown\Models\Markdown;
99

10-
use function str_replace;
10+
use function ltrim;
11+
use function explode;
1112
use function sprintf;
1213
use function str_starts_with;
13-
use function strlen;
14-
use function substr;
1514
use function trim;
1615

1716
/**
18-
* @internal This class may be refactored to work with a single class instead of five, thus extending this class is discouraged.
17+
* @internal This class may be refactored further, thus extending this class is discouraged.
1918
*/
20-
abstract class ColoredBlockquotes implements MarkdownShortcodeContract
19+
class ColoredBlockquotes implements MarkdownShortcodeContract
2120
{
22-
protected static string $signature = '>color';
21+
/** @var string The core signature. We combine this with an additional check for color later. */
22+
protected static string $signature = '>';
23+
24+
/** @var array<string> */
25+
protected static array $signatures = ['>danger', '>info', '>success', '>warning'];
2326

2427
public static function signature(): string
2528
{
@@ -28,49 +31,40 @@ public static function signature(): string
2831

2932
public static function resolve(string $input): string
3033
{
31-
return str_starts_with($input, static::signature())
34+
return self::stringStartsWithSignature($input)
3235
? static::expand($input)
3336
: $input;
3437
}
3538

36-
protected static function expand(string $input): string
39+
/**
40+
* @internal
41+
*
42+
* @return array<string>
43+
*/
44+
public static function getSignatures(): array
3745
{
38-
return sprintf(
39-
'<blockquote class="%s">%s</blockquote>',
40-
static::getClassNameFromSignature(static::signature()),
41-
trim(Markdown::render(trim(substr($input, strlen(static::signature())), ' ')))
42-
);
46+
return self::$signatures;
4347
}
4448

45-
protected static function getClassNameFromSignature(string $signature): string
49+
protected static function expand(string $input): string
4650
{
47-
return str_replace('>', '', $signature);
51+
$parts = explode(' ', $input, 2);
52+
$class = ltrim($parts[0], '>');
53+
$contents = trim($parts[1] ?? '', ' ');
54+
55+
return sprintf('<blockquote class="%s">%s</blockquote>',
56+
$class, trim(Markdown::render($contents))
57+
);
4858
}
4959

50-
/** @return ColoredBlockquotes[] */
51-
public static function get(): array
60+
protected static function stringStartsWithSignature(string $input): bool
5261
{
53-
return [
54-
/** @internal */
55-
new class extends ColoredBlockquotes
56-
{
57-
protected static string $signature = '>danger';
58-
},
59-
/** @internal */
60-
new class extends ColoredBlockquotes
61-
{
62-
protected static string $signature = '>info';
63-
},
64-
/** @internal */
65-
new class extends ColoredBlockquotes
66-
{
67-
protected static string $signature = '>success';
68-
},
69-
/** @internal */
70-
new class extends ColoredBlockquotes
71-
{
72-
protected static string $signature = '>warning';
73-
},
74-
];
62+
foreach (static::$signatures as $signature) {
63+
if (str_starts_with($input, $signature)) {
64+
return true;
65+
}
66+
}
67+
68+
return false;
7569
}
7670
}

packages/framework/src/Markdown/Processing/ShortcodeProcessor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ public function addShortcode(MarkdownShortcodeContract $shortcode): void
9797

9898
protected function discoverShortcodes(): void
9999
{
100-
$this->addShortcodesFromArray(
101-
ColoredBlockquotes::get()
102-
);
100+
// Add the built-in shortcodes.
101+
102+
foreach (ColoredBlockquotes::getSignatures() as $signature) {
103+
$this->shortcodes[$signature] = new ColoredBlockquotes();
104+
}
105+
106+
$this->addShortcodesFromArray([
107+
//
108+
]);
103109
}
104110

105111
protected function getOutput(): string

packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,40 @@
1414
*/
1515
class ColoredBlockquoteShortcodesTest extends UnitTestCase
1616
{
17-
public function testGetMethod()
17+
public function testSignature()
1818
{
19-
$this->assertCount(4, ColoredBlockquotes::get());
20-
$this->assertContainsOnlyInstancesOf(ColoredBlockquotes::class,
21-
ColoredBlockquotes::get()
19+
$this->assertSame('>', ColoredBlockquotes::signature());
20+
}
21+
22+
public function testSignatures()
23+
{
24+
$this->assertSame(
25+
['>danger', '>info', '>success', '>warning'],
26+
ColoredBlockquotes::getSignatures()
2227
);
2328
}
2429

2530
public function testResolveMethod()
2631
{
2732
$this->assertSame(
28-
'<blockquote class="color"><p>foo</p></blockquote>',
29-
ColoredBlockquotes::resolve('>color foo')
33+
'<blockquote class="info"><p>foo</p></blockquote>',
34+
ColoredBlockquotes::resolve('>info foo')
3035
);
3136
}
3237

3338
public function testCanUseMarkdownWithinBlockquote()
3439
{
3540
$this->assertSame(
36-
'<blockquote class="color"><p>foo <strong>bar</strong></p></blockquote>',
37-
ColoredBlockquotes::resolve('>color foo **bar**')
41+
'<blockquote class="info"><p>foo <strong>bar</strong></p></blockquote>',
42+
ColoredBlockquotes::resolve('>info foo **bar**')
43+
);
44+
}
45+
46+
public function testWithUnrelatedClass()
47+
{
48+
$this->assertSame(
49+
'>foo foo',
50+
ColoredBlockquotes::resolve('>foo foo')
3851
);
3952
}
4053
}

packages/framework/tests/Feature/Services/Markdown/ShortcodeProcessorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,25 @@ public static function resolve(string $input): string
6262
$this->assertArrayHasKey('foo', $processor->getShortcodes());
6363
$this->assertEquals('bar', $processor->run());
6464
}
65+
66+
public function test_shortcodes_can_be_added_to_processor_using_array()
67+
{
68+
$processor = new ShortcodeProcessor('foo');
69+
70+
$processor->addShortcodesFromArray([new class implements MarkdownShortcodeContract
71+
{
72+
public static function signature(): string
73+
{
74+
return 'foo';
75+
}
76+
77+
public static function resolve(string $input): string
78+
{
79+
return 'bar';
80+
}
81+
}]);
82+
83+
$this->assertArrayHasKey('foo', $processor->getShortcodes());
84+
$this->assertEquals('bar', $processor->run());
85+
}
6586
}

0 commit comments

Comments
 (0)