定制 beholdr/commonmark-shortcode 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beholdr/commonmark-shortcode

Composer 安装命令:

composer require beholdr/commonmark-shortcode

包简介

Shortcodes extension for CommonMark

README 文档

README

Latest Version on Packagist

Shortcodes extension for CommonMark.

Installation

You can install the package via composer:

composer require beholdr/commonmark-shortcode

Usage

use Beholdr\CommonmarkShortcode\ShortcodeExtension;
use Beholdr\CommonmarkShortcode\ShortcodeRegistry;
use League\CommonMark\Environment\Environment;
use League\CommonMark\MarkdownConverter;

// create shortcodes registry
$registry = new ShortcodeRegistry();
$registry->register('my-code', fn (array $attrs) => /* Your callback */);

// register extension
$environment = new Environment();
$environment->addExtension(new ShortcodeExtension($registry));

// use it in markdown
$converter = new MarkdownConverter($environment);
echo $converter->convert('Markdown with [my-code]!');

You can rebuild a shortcode attribute string using stringify helper:

use Beholdr\CommonmarkShortcode\ShortcodeAttributes;

$attrs = ['foo' => 'bar', 'enabled' => true];
ShortcodeAttributes::stringify($attrs); // `foo=bar enabled`

Laravel example

You can use this extension with graham-campbell/markdown or spatie/laravel-markdown packages.

For example, you want to replace [calculator amount=100] shortcode with Livewire component, passing given attributes.

  1. Register extension in the markdown package config:
'extensions' => [
    Beholdr\CommonmarkShortcode\ShortcodeExtension::class,
],
  1. Bind ShortcodeRegistry as singleton inside your AppServiceProvider and register your shortcodes:
use Beholdr\CommonmarkShortcode\ShortcodeAttributes;
use Beholdr\CommonmarkShortcode\ShortcodeRegistry;
use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ShortcodeRegistry::class, fn () => new ShortcodeRegistry);
    }

    public function boot(): void
    {
        app(ShortcodeRegistry::class)
            ->register('calculator', fn ($attrs) =>
                Blade::render(
                    sprintf(
                        '<livewire:calculator %s />',
                        ShortcodeAttributes::stringify($attrs)
                    )
                )
            );
    }
}

Testing

composer test

Opencode auth

Put auth.json into .devcontainer/mnt folder and rename it to opencode-auth.json.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 185
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固