├── config └── commonmark-blade-block.php ├── src ├── CommonmarkBladeBlockServiceProvider.php ├── Block │ ├── Blade.php │ ├── Renderer │ │ └── BladeRenderer.php │ └── Parser │ │ ├── BladeStartParser.php │ │ └── BladeParser.php └── BladeExtension.php ├── CHANGELOG.md ├── LICENSE.md ├── composer.json └── README.md /config/commonmark-blade-block.php: -------------------------------------------------------------------------------- 1 | name('commonmark-blade-block'); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Block/Blade.php: -------------------------------------------------------------------------------- 1 | content; 16 | } 17 | 18 | public function setContent(string $content): void 19 | { 20 | $this->content = $content; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Block/Renderer/BladeRenderer.php: -------------------------------------------------------------------------------- 1 | getContent()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BladeExtension.php: -------------------------------------------------------------------------------- 1 | addBlockStartParser(new BladeStartParser, 100) 17 | ->addRenderer(Blade::class, new BladeRenderer); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `commonmark-blade-block` will be documented in this file. 4 | 5 | ## v1.1.0 - 2025-05-09 6 | 7 | ### What's Changed 8 | 9 | * Allow Laravel 12 by @bambamboole in https://github.com/ryangjchandler/commonmark-blade-block/pull/3 10 | * Removes Laravel 10 and 11 support. 11 | 12 | 13 | ### New Contributors 14 | 15 | * @dependabot made their first contribution in https://github.com/ryangjchandler/commonmark-blade-block/pull/1 16 | * @bambamboole made their first contribution in https://github.com/ryangjchandler/commonmark-blade-block/pull/3 17 | 18 | **Full Changelog**: https://github.com/ryangjchandler/commonmark-blade-block/compare/v1.0.0...v1.1.0 19 | 20 | ## v1.0.0 - 2024-10-17 21 | 22 | * Initial release. 23 | -------------------------------------------------------------------------------- /src/Block/Parser/BladeStartParser.php: -------------------------------------------------------------------------------- 1 | isIndented()) { 17 | return BlockStart::none(); 18 | } 19 | 20 | if (! $cursor->match(self::REGEX)) { 21 | return BlockStart::none(); 22 | } 23 | 24 | return BlockStart::of(new BladeParser)->at($cursor); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Ryan Chandler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Block/Parser/BladeParser.php: -------------------------------------------------------------------------------- 1 | block = new Blade; 21 | $this->strings = new ArrayCollection; 22 | } 23 | 24 | public function getBlock(): Blade 25 | { 26 | return $this->block; 27 | } 28 | 29 | public function addLine(string $line): void 30 | { 31 | $this->strings[] = $line; 32 | } 33 | 34 | public function closeBlock(): void 35 | { 36 | $this->block->setContent( 37 | ltrim(implode("\n", $this->strings->toArray())) 38 | ); 39 | } 40 | 41 | public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue 42 | { 43 | if ($cursor->match('/^@endblade/')) { 44 | return BlockContinue::finished(); 45 | } 46 | 47 | return BlockContinue::at($cursor); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/commonmark-blade-block", 3 | "description": "Embed Laravel Blade code inside of your Markdown templates.", 4 | "keywords": [ 5 | "Ryan Chandler", 6 | "laravel", 7 | "commonmark-blade-block" 8 | ], 9 | "homepage": "https://github.com/ryangjchandler/commonmark-blade-block", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Ryan Chandler", 14 | "email": "support@ryangjchandler.co.uk", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "illuminate/contracts": "^12.0", 21 | "league/commonmark": "^2.7", 22 | "spatie/laravel-package-tools": "^1.92.4" 23 | }, 24 | "require-dev": { 25 | "laravel/pint": "^1.22.1", 26 | "nunomaduro/collision": "^8.0", 27 | "larastan/larastan": "^3.0", 28 | "orchestra/testbench": "^10.0.0", 29 | "pestphp/pest": "^3.0", 30 | "pestphp/pest-plugin-arch": "^3.0", 31 | "pestphp/pest-plugin-laravel": "^3.0", 32 | "phpstan/extension-installer": "^1.4.3", 33 | "phpstan/phpstan-deprecation-rules": "^2.0", 34 | "phpstan/phpstan-phpunit": "^2.0", 35 | "spatie/laravel-ray": "^1.40.2" 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "RyanChandler\\CommonmarkBladeBlock\\": "src/", 40 | "RyanChandler\\CommonmarkBladeBlock\\Database\\Factories\\": "database/factories/" 41 | } 42 | }, 43 | "autoload-dev": { 44 | "psr-4": { 45 | "RyanChandler\\CommonmarkBladeBlock\\Tests\\": "tests/", 46 | "Workbench\\App\\": "workbench/app/" 47 | } 48 | }, 49 | "scripts": { 50 | "post-autoload-dump": "@composer run prepare", 51 | "clear": "@php vendor/bin/testbench package:purge-commonmark-blade-block --ansi", 52 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 53 | "build": [ 54 | "@composer run prepare", 55 | "@php vendor/bin/testbench workbench:build --ansi" 56 | ], 57 | "start": [ 58 | "Composer\\Config::disableProcessTimeout", 59 | "@composer run build", 60 | "@php vendor/bin/testbench serve" 61 | ], 62 | "analyse": "vendor/bin/phpstan analyse", 63 | "test": "vendor/bin/pest", 64 | "test-coverage": "vendor/bin/pest --coverage", 65 | "format": "vendor/bin/pint" 66 | }, 67 | "config": { 68 | "sort-packages": true, 69 | "allow-plugins": { 70 | "pestphp/pest-plugin": true, 71 | "phpstan/extension-installer": true 72 | } 73 | }, 74 | "extra": { 75 | "laravel": { 76 | "providers": [ 77 | "RyanChandler\\CommonmarkBladeBlock\\CommonmarkBladeBlockServiceProvider" 78 | ], 79 | "aliases": { 80 | "CommonmarkBladeBlock": "RyanChandler\\CommonmarkBladeBlock\\Facades\\CommonmarkBladeBlock" 81 | } 82 | } 83 | }, 84 | "minimum-stability": "dev", 85 | "prefer-stable": true 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Embed Laravel Blade code inside of your Markdown templates. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/commonmark-blade-block.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/commonmark-blade-block) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ryangjchandler/commonmark-blade-block/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/ryangjchandler/commonmark-blade-block/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ryangjchandler/commonmark-blade-block/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/ryangjchandler/commonmark-blade-block/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/commonmark-blade-block.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/commonmark-blade-block) 7 | 8 | This package provides an extension for [`league/commonmark`](https://github.com/thephpleague/commonmark) that lets you embed [Laravel Blade code](https://laravel.com/docs/blade) inside of your Markdown content. 9 | 10 | ## Installation 11 | 12 | You can install the package via Composer: 13 | 14 | ```bash 15 | composer require ryangjchandler/commonmark-blade-block 16 | ``` 17 | 18 | ## Usage 19 | 20 | Start by registering the extension. 21 | 22 | ```php 23 | use League\CommonMark\MarkdownConverter; 24 | use League\CommonMark\Environment\Environment; 25 | use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; 26 | use RyanChandler\CommonmarkBladeBlock\BladeExtension; 27 | 28 | $environment = new Environment(); 29 | 30 | $environment 31 | ->addExtension(new CommonMarkCoreExtension) 32 | ->addExtension(new BladeExtension); 33 | 34 | $converter = new MarkdownConverter($environment); 35 | ``` 36 | 37 | Then start embedding Blade inside of your Markdown content with the `@blade` and `@endblade` tags. 38 | 39 | ```md 40 | # Hello, world! 41 | 42 | @blade 43 | 44 | Click me! 45 | 46 | @endblade 47 | ``` 48 | 49 | ### Using Laravel's `Str` or `str()` helpers 50 | 51 | If you're using `Str::markdown()` or `str()->markdown()`, then you can register the extension through the `extensions` argument. 52 | 53 | ```php 54 | Str::markdown( 55 | <<<'MD' 56 | @blade 57 | 58 | @endblade 59 | MD, 60 | extensions: [ 61 | new BladeExtension(), 62 | ] 63 | ) 64 | ``` 65 | 66 | ## Testing 67 | 68 | ```bash 69 | composer test 70 | ``` 71 | 72 | ## Changelog 73 | 74 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 75 | 76 | ## Contributing 77 | 78 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 79 | 80 | ## Security Vulnerabilities 81 | 82 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 83 | 84 | ## Credits 85 | 86 | - [Ryan Chandler](https://github.com/ryangjchandler) 87 | - [All Contributors](../../contributors) 88 | 89 | ## License 90 | 91 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 92 | --------------------------------------------------------------------------------