PHPackages                             samwilson/commonmark-shortcodes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. samwilson/commonmark-shortcodes

ActiveCommonmark-extension[Parsing &amp; Serialization](/categories/parsing)

samwilson/commonmark-shortcodes
===============================

Adds shortcodes to Markdown, for the CommonMark package.

0.6.1(8mo ago)32.3k↓18.8%[2 issues](https://github.com/samwilson/commonmark-shortcodes/issues)1gpl-3.0-or-laterPHPCI passing

Since Apr 16Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/samwilson/commonmark-shortcodes)[ Packagist](https://packagist.org/packages/samwilson/commonmark-shortcodes)[ RSS](/packages/samwilson-commonmark-shortcodes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (12)Used By (1)

Samwilson/CommonMarkShortcodes
==============================

[](#samwilsoncommonmarkshortcodes)

An extension to [League/CommonMark](https://commonmark.thephpleague.com)for adding 'shortcodes' to Markdown.

[![Packagist Version](https://camo.githubusercontent.com/2dacd4ab731bc86e3e774bec6713757eedffc59d0148e393e58cbf764eb59b02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f646573)](https://camo.githubusercontent.com/2dacd4ab731bc86e3e774bec6713757eedffc59d0148e393e58cbf764eb59b02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f646573)[![Packagist License](https://camo.githubusercontent.com/41e5d390fcf1a738a3150422055ab6faa77d5e3e11765179543bdfcae3be8f9d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f646573)](https://camo.githubusercontent.com/41e5d390fcf1a738a3150422055ab6faa77d5e3e11765179543bdfcae3be8f9d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f646573)[![GitHub Workflow Status](https://camo.githubusercontent.com/d9f44769f27643547aac91a6a34722d67012c8483769fb45f2e7f63353f06557/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f6465732f63692e796d6c3f6272616e63683d6d61696e)](https://camo.githubusercontent.com/d9f44769f27643547aac91a6a34722d67012c8483769fb45f2e7f63353f06557/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73616d77696c736f6e2f636f6d6d6f6e6d61726b2d73686f7274636f6465732f63692e796d6c3f6272616e63683d6d61696e)

Shortcodes are bits of text in a Markdown document that are delimited with one or three braces and are replaced with whatever content is needed. There are two types of shortcode, inline and block (the same name cannot be used for both an inline and block shortcode; i.e. you must decide when designing the shortcodes how each is to be used).

Inline shortcodes have one brace, `{name|attr=val}`, and appear within a paragraph or list item etc. For example, here `cite` is the name of the shortcode and it's got one attribute:

```
There are over 300 distinct breeds{cite|Kris2008} of goat.

```

Block shortcodes have three braces, `{{{name|attr=val}}}`, and an optional body; their attributes are the same as for inline shortcodes. Here `quotation` is the shortcode, it's got one attribute, and a two-line body:

```
{{{quotation|source=Q2934
The goat is a member of the animal family Bovidae and the tribe Caprini,
meaning it is closely related to the sheep.
}}}

```

Shortcodes can have zero or more attributes. Attributes are separated by pipes `|`and can be given a name by adding the equals sign (without a name, they're numbered from 1).

When compiled, the shortcodes are replaced by the HTML (or other content) that is specified in the shortcode handlers. See [\#Usage](#Usage) below for more information about how to set up the CommonMark environment.

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/):

```
$ composer require samwilson/commonmark-shortcodes

```

Usage
-----

[](#usage)

Set up your CommonMark Environment object with a `shortcodes` configuration key:

```
$environment = new Environment([
    'shortcodes' => [
        'shortcodes' => [
            'myshortcode' => /* callback one */
            'myshortcode2' => /* callback two */
        ],
    ],
]);
$environment->addExtension(new CommonMarkCoreExtension());
```

Then add the `ShortcodeExtension`, and start converting Markdown:

```
$environment->addExtension(new ShortcodeExtension());
$converter = new MarkdownConverter($environment);
echo $converter->convert('Markdown *goes here*.')->getContent();
```

The callbacks are where you define your shortcodes' output. Each takes a single parameter (a `Shortcode` object) and returns a string that will be inserted into the output in place of the shortcode.

For example, Markdown like this:

```
Lorem {smallcaps|ipsum}.

```

could be paired with the following shortcode configuration:

```
[
    'smallcaps' => function (Shortcode $shortcode) {
        return ''
            . $shortcode->getAttr(1)
            . '';
    },
]
```

to produce this output:

```
Lorem ipsum
```

See the [examples/ directory](/examples/) for functioning examples, and if you have any issues please do [report them](https://github.com/samwilson/commonmark-shortcodes/issues).

License
-------

[](#license)

Copyright © 2023 Sam Wilson

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance49

Moderate activity, may be stable

Popularity25

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~88 days

Recently: every ~151 days

Total

11

Last Release

247d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a0281a00958518a98244b8a6052f91e86d1599aebf9b7bb6c55bca30d98774e7?d=identicon)[samwilson](/maintainers/samwilson)

---

Top Contributors

[![samwilson](https://avatars.githubusercontent.com/u/213655?v=4)](https://github.com/samwilson "samwilson (21 commits)")

---

Tags

commonmark-extensionhacktoberfestmarkdownshortcodes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/samwilson-commonmark-shortcodes/health.svg)

```
[![Health](https://phpackages.com/badges/samwilson-commonmark-shortcodes/health.svg)](https://phpackages.com/packages/samwilson-commonmark-shortcodes)
```

###  Alternatives

[spatie/laravel-markdown

A highly configurable markdown renderer and Blade component for Laravel

4053.4M35](/packages/spatie-laravel-markdown)[mnapoli/front-yaml

2895.6M45](/packages/mnapoli-front-yaml)[daux/daux.io

Documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly

825191.0k1](/packages/daux-dauxio)[spatie/sheets

Store &amp; retrieve your static content in plain text files

30187.7k4](/packages/spatie-sheets)[prezet/prezet

Prezet: Markdown Blogging for Laravel

2969.8k2](/packages/prezet-prezet)[sinnbeck/markdom

Converts markdown to html with classes

69122.6k2](/packages/sinnbeck-markdom)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
