From 4cc31c6dabac994156e7fdd664850dbc717639b7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 Oct 2023 16:43:55 +0200 Subject: [PATCH 1/8] Add todo --- .../framework/src/Markdown/Processing/ColoredBlockquotes.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php index 835f435de3b..5d455e043af 100644 --- a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php +++ b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php @@ -16,6 +16,8 @@ /** * @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. */ abstract class ColoredBlockquotes implements MarkdownShortcodeContract { From d818c59428605262dd0aadef1063e5fb7677db02 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:33:30 +0100 Subject: [PATCH 2/8] Refactor ColoredBlockquotes class internals --- .../Processing/ColoredBlockquotes.php | 73 ++++++++----------- .../Processing/ShortcodeProcessor.php | 12 ++- .../ColoredBlockquoteShortcodesTest.php | 16 +--- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php index 5d455e043af..898c588b2f6 100644 --- a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php +++ b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php @@ -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 { @@ -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( - '
%s
', - 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('
%s
', + $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; } } diff --git a/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php b/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php index c9cf4d3f6fd..2f28c91efcb 100644 --- a/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php +++ b/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php @@ -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 diff --git a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php index 1c51a6247d9..8f25cbdd3a0 100644 --- a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php +++ b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php @@ -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( - '

foo

', - ColoredBlockquotes::resolve('>color foo') + '

foo

', + ColoredBlockquotes::resolve('>info foo') ); } public function testCanUseMarkdownWithinBlockquote() { $this->assertSame( - '

foo bar

', - ColoredBlockquotes::resolve('>color foo **bar**') + '

foo bar

', + ColoredBlockquotes::resolve('>info foo **bar**') ); } } From bab8aa987aefcaff4757450f6118fa4054d43f68 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:35:14 +0100 Subject: [PATCH 3/8] Add additional unit test --- .../tests/Feature/ColoredBlockquoteShortcodesTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php index 8f25cbdd3a0..0a414dfce25 100644 --- a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php +++ b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php @@ -29,4 +29,12 @@ public function testCanUseMarkdownWithinBlockquote() ColoredBlockquotes::resolve('>info foo **bar**') ); } + + public function testWithUnrelatedClass() + { + $this->assertSame( + '>foo foo', + ColoredBlockquotes::resolve('>foo foo') + ); + } } From 73b4c3e41f682de13120bcded37c826a1d133c4a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:42:52 +0100 Subject: [PATCH 4/8] Add more unit tests --- .../Feature/ColoredBlockquoteShortcodesTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php index 0a414dfce25..063429d9d10 100644 --- a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php +++ b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php @@ -14,6 +14,19 @@ */ class ColoredBlockquoteShortcodesTest extends UnitTestCase { + public function testSignature() + { + $this->assertSame('>', ColoredBlockquotes::signature()); + } + + public function testSignatures() + { + $this->assertSame( + ['>danger', '>info', '>success', '>warning'], + ColoredBlockquotes::getSignatures() + ); + } + public function testResolveMethod() { $this->assertSame( From 5f10dddcc572eb580ef7ebc1adc1995900f08850 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:43:12 +0100 Subject: [PATCH 5/8] Move down uncontracted method --- .../Processing/ColoredBlockquotes.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php index 898c588b2f6..8e319a5b539 100644 --- a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php +++ b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php @@ -23,16 +23,6 @@ class ColoredBlockquotes implements MarkdownShortcodeContract protected static array $signatures = ['>danger', '>info', '>success', '>warning']; - /** - * @internal - * - * @return string[] - */ - public static function getSignatures(): array - { - return self::$signatures; - } - public static function signature(): string { return static::$signature; @@ -45,6 +35,16 @@ public static function resolve(string $input): string : $input; } + /** + * @internal + * + * @return string[] + */ + public static function getSignatures(): array + { + return self::$signatures; + } + protected static function expand(string $input): string { $parts = explode(' ', $input, 2); From d703af7cd23058442e29566cad112441f37c9a0a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:48:17 +0100 Subject: [PATCH 6/8] Add unit test --- .../Markdown/ShortcodeProcessorTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/framework/tests/Feature/Services/Markdown/ShortcodeProcessorTest.php b/packages/framework/tests/Feature/Services/Markdown/ShortcodeProcessorTest.php index e44d55ee331..559c1c849a2 100644 --- a/packages/framework/tests/Feature/Services/Markdown/ShortcodeProcessorTest.php +++ b/packages/framework/tests/Feature/Services/Markdown/ShortcodeProcessorTest.php @@ -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()); + } } From 4af3c81840721fbb4ef4f7dcce66a2ca8becf02f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:49:30 +0100 Subject: [PATCH 7/8] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 5194be7073a..17938f3cb4c 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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` (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. From e90de61c674e778480d77af06ccaf5169a3c1a1b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 11:02:18 +0100 Subject: [PATCH 8/8] Add and normalise generic array annotations in file --- .../framework/src/Markdown/Processing/ColoredBlockquotes.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php index 8e319a5b539..f37d807bbad 100644 --- a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php +++ b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php @@ -21,6 +21,7 @@ class ColoredBlockquotes implements MarkdownShortcodeContract /** @var string The core signature. We combine this with an additional check for color later. */ protected static string $signature = '>'; + /** @var array */ protected static array $signatures = ['>danger', '>info', '>success', '>warning']; public static function signature(): string @@ -38,7 +39,7 @@ public static function resolve(string $input): string /** * @internal * - * @return string[] + * @return array */ public static function getSignatures(): array {