Skip to content

Commit

Permalink
Refactor ColoredBlockquotes class internals
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 29, 2023
1 parent 4cc31c6 commit d818c59
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
73 changes: 32 additions & 41 deletions packages/framework/src/Markdown/Processing/ColoredBlockquotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@
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.
*
* @todo See if we can extract an arbitrary class name from the signature.
* @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 = '>';

protected static array $signatures = ['>danger', '>info', '>success', '>warning'];

/**
* @internal
*
* @return string[]
*/
public static function getSignatures(): array
{
return self::$signatures;
}

public static function signature(): string
{
Expand All @@ -30,49 +40,30 @@ 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
{
return sprintf(
'<blockquote class="%s">%s</blockquote>',
static::getClassNameFromSignature(static::signature()),
trim(Markdown::render(trim(substr($input, strlen(static::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))
);
}

protected static function getClassNameFromSignature(string $signature): string
protected static function stringStartsWithSignature(string $input): bool
{
return str_replace('>', '', $signature);
}
foreach (static::$signatures as $signature) {
if (str_starts_with($input, $signature)) {
return true;
}
}

/** @return ColoredBlockquotes[] */
public static function get(): array
{
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';
},
];
return false;

Check warning on line 67 in packages/framework/src/Markdown/Processing/ColoredBlockquotes.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L67 was not covered by tests
}
}
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,19 @@
*/
class ColoredBlockquoteShortcodesTest extends UnitTestCase
{
public function testGetMethod()
{
$this->assertCount(4, ColoredBlockquotes::get());
$this->assertContainsOnlyInstancesOf(ColoredBlockquotes::class,
ColoredBlockquotes::get()
);
}

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**')
);
}
}

0 comments on commit d818c59

Please sign in to comment.