Skip to content

Commit

Permalink
Merge pull request #1413 from hydephp/refactor-colored-blockquote-int…
Browse files Browse the repository at this point in the history
…ernals

Refactor colored blockquote internals to extract class name from signature
  • Loading branch information
caendesilva authored Oct 29, 2023
2 parents 285f819 + e90de61 commit 0108b87
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This serves two purposes:
- 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)
- 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
- 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
- Internal code refactors and improvements in https://github.com/hydephp/develop/pull/1410 and https://github.com/hydephp/develop/pull/1411
- 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

### Deprecated
- for soon-to-be removed features.
Expand Down
72 changes: 33 additions & 39 deletions packages/framework/src/Markdown/Processing/ColoredBlockquotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
use Hyde\Markdown\Contracts\MarkdownShortcodeContract;
use Hyde\Markdown\Models\Markdown;

use function str_replace;
use function ltrim;
use function explode;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;
use function trim;

/**
* @internal This class may be refactored to work with a single class instead of five, thus extending this class is discouraged.
* @internal This class may be refactored further, thus extending this class is discouraged.
*/
abstract class ColoredBlockquotes implements MarkdownShortcodeContract
class ColoredBlockquotes implements MarkdownShortcodeContract
{
protected static string $signature = '>color';
/** @var string The core signature. We combine this with an additional check for color later. */
protected static string $signature = '>';

/** @var array<string> */
protected static array $signatures = ['>danger', '>info', '>success', '>warning'];

public static function signature(): string
{
Expand All @@ -28,49 +31,40 @@ public static function signature(): string

public static function resolve(string $input): string
{
return str_starts_with($input, static::signature())
return self::stringStartsWithSignature($input)
? static::expand($input)
: $input;
}

protected static function expand(string $input): string
/**
* @internal
*
* @return array<string>
*/
public static function getSignatures(): array
{
return sprintf(
'<blockquote class="%s">%s</blockquote>',
static::getClassNameFromSignature(static::signature()),
trim(Markdown::render(trim(substr($input, strlen(static::signature())), ' ')))
);
return self::$signatures;
}

protected static function getClassNameFromSignature(string $signature): string
protected static function expand(string $input): string
{
return str_replace('>', '', $signature);
$parts = explode(' ', $input, 2);
$class = ltrim($parts[0], '>');
$contents = trim($parts[1] ?? '', ' ');

return sprintf('<blockquote class="%s">%s</blockquote>',
$class, trim(Markdown::render($contents))
);
}

/** @return ColoredBlockquotes[] */
public static function get(): array
protected static function stringStartsWithSignature(string $input): bool
{
return [
/** @internal */
new class extends ColoredBlockquotes
{
protected static string $signature = '>danger';
},
/** @internal */
new class extends ColoredBlockquotes
{
protected static string $signature = '>info';
},
/** @internal */
new class extends ColoredBlockquotes
{
protected static string $signature = '>success';
},
/** @internal */
new class extends ColoredBlockquotes
{
protected static string $signature = '>warning';
},
];
foreach (static::$signatures as $signature) {
if (str_starts_with($input, $signature)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ public function addShortcode(MarkdownShortcodeContract $shortcode): void

protected function discoverShortcodes(): void
{
$this->addShortcodesFromArray(
ColoredBlockquotes::get()
);
// Add the built-in shortcodes.

foreach (ColoredBlockquotes::getSignatures() as $signature) {
$this->shortcodes[$signature] = new ColoredBlockquotes();
}

$this->addShortcodesFromArray([
//
]);
}

protected function getOutput(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,40 @@
*/
class ColoredBlockquoteShortcodesTest extends UnitTestCase
{
public function testGetMethod()
public function testSignature()
{
$this->assertCount(4, ColoredBlockquotes::get());
$this->assertContainsOnlyInstancesOf(ColoredBlockquotes::class,
ColoredBlockquotes::get()
$this->assertSame('>', ColoredBlockquotes::signature());
}

public function testSignatures()
{
$this->assertSame(
['>danger', '>info', '>success', '>warning'],
ColoredBlockquotes::getSignatures()
);
}

public function testResolveMethod()
{
$this->assertSame(
'<blockquote class="color"><p>foo</p></blockquote>',
ColoredBlockquotes::resolve('>color foo')
'<blockquote class="info"><p>foo</p></blockquote>',
ColoredBlockquotes::resolve('>info foo')
);
}

public function testCanUseMarkdownWithinBlockquote()
{
$this->assertSame(
'<blockquote class="color"><p>foo <strong>bar</strong></p></blockquote>',
ColoredBlockquotes::resolve('>color foo **bar**')
'<blockquote class="info"><p>foo <strong>bar</strong></p></blockquote>',
ColoredBlockquotes::resolve('>info foo **bar**')
);
}

public function testWithUnrelatedClass()
{
$this->assertSame(
'>foo foo',
ColoredBlockquotes::resolve('>foo foo')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@ public static function resolve(string $input): string
$this->assertArrayHasKey('foo', $processor->getShortcodes());
$this->assertEquals('bar', $processor->run());
}

public function test_shortcodes_can_be_added_to_processor_using_array()
{
$processor = new ShortcodeProcessor('foo');

$processor->addShortcodesFromArray([new class implements MarkdownShortcodeContract
{
public static function signature(): string
{
return 'foo';
}

public static function resolve(string $input): string
{
return 'bar';
}
}]);

$this->assertArrayHasKey('foo', $processor->getShortcodes());
$this->assertEquals('bar', $processor->run());
}
}

0 comments on commit 0108b87

Please sign in to comment.