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

v1.2.0 2025-06-14 08:04 UTC

This package is auto-updated.

Last update: 2025-12-14 09:55:51 UTC


README

Packagist Version License: MIT

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>`
  - `![alt](url)` β†’ `<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