daryledesilva / markdown-to-mrkdwn-php
Convert standard Markdown to Slack's mrkdwn format
Installs: 6 245
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/daryledesilva/markdown-to-mrkdwn-php
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5 || ^10.0
- squizlabs/php_codesniffer: ^3.7
README
A lightweight, dependency-free PHP library for converting Markdown into Slack-compatible mrkdwn. It preserves code blocks, handles tables, blockquotes, lists, and more for seamless Slack messaging.
π‘ What is this?
An easy-to-use, dependency-free PHP 7.4+ library that converts Markdownβheadings, text formatting, lists, tables, blockquotes, and code blocksβinto Slackβs mrkdwn format. Ideal for bots, integrations, or any app sending rich text to Slack.
β¨ Features
- Headings (H1βH6 β `*Heading*`) - Text formatting: - Bold (`**bold**` β `*bold*`) - Italic (`*italic*` or `_italic_` β `_italic_`) - Strikethrough (`~~strike~~` β `~strike~`) - Lists: - Unordered & ordered (with nesting) - Task lists (`- [ ]` / `- [x]` β `β’ β` / `β’ β`) - Tables (simple text tables with bold headers) - Links & images: - `[text](url)` β `<url|text>` - `` β `<url>` - Code: - Inline `` `code` `` - Fenced code blocks (```β¦```), preserving language hint - Blockquotes (`> quote`) - Horizontal rules (`---`, `***`, `___` β `ββββββββββ`)
π Supported Conversions
| Markdown | Slack mrkdwn |
|---|---|
# Heading |
*Heading* |
**Bold** |
*Bold* |
*Italic* / _Italic_ |
_Italic_ |
~~Strike~~ |
~Strike~ |
- [ ] Task |
β’ β Task |
- [x] Task |
β’ β Task |
- Item / 1. Item |
β’ Item / 1. Item |
`Inline code` |
`Inline code` |
| ```lang | ```lang |
| code | code |
| ``` | ``` |
> Quote |
> Quote |
--- / *** / ___ |
ββββββββββ |
π Plugin System
You can extend the converter with custom plugins (global, line or block scope):
use DaryleDeSilva\MarkdownToMrkdwn\Converter; $converter = new Converter(); // Global plugin (runs on full text) $converter->registerPlugin( 'addQuotes', fn(string $text) => "\"{$text}\"", priority: 10, scope: 'global' ); // Line plugin (before standard conversion) $converter->registerPlugin( 'linePrefix', fn(string $line) => "[LINE] {$line}", priority: 20, scope: 'line', timing: 'before' ); echo $converter->convert("**Hello**, world!");
Advanced plugin examples
// Function plugin: convert entire text to uppercase $converter->registerPlugin( name: 'toUpper', converter_func: fn(string $text) => strtoupper($text), priority: 10, scope: 'line', timing: 'after' ); // Regex plugin: mask email addresses $converter->registerRegexPlugin( name: 'maskEmails', pattern: '/[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+/', replacement: '[EMAIL]', priority: 20, timing: 'after' );
Error handling
If an error occurs during conversion, the original Markdown text is returned unmodified.
π Requirements
- PHP 7.4 or higher
π¦ Installation
composer require daryledesilva/markdown-to-mrkdwn-php
π Usage
use DaryleDeSilva\MarkdownToMrkdwn\Converter; $converter = new Converter(); echo $converter->convert("**Hello**, [Slack](https://slack.com)!");
π§ͺ Testing
composer install
composer test
π Testing in Slack
You can preview the converted mrkdwn in Slackβs Block Kit Builder:
https://app.slack.com/block-kit-builder/
π License
MIT
Inspired by the original Python package markdown_to_mrkdwn.
Created by @daryledesilva