jbroadway/slimdown

A simple regex-based Markdown parser.

1.0.6-stable 2024-08-06 15:26 UTC

This package is auto-updated.

Last update: 2024-12-12 02:52:46 UTC


README

GitHub Workflow Status (branch) GitHub License Packagist Version Packagist PHP Version Support

A simple regex-based Markdown parser in PHP. Supports the following elements (and can be extended via Slimdown::add_rule()):

  • Headers
  • Links
  • Bold
  • Emphasis
  • Deletions
  • Quotes
  • Code blocks
  • Inline code
  • Blockquotes
  • Ordered/unordered lists
  • Checklists
  • Images

Originally hosted as a gist here.

Usage

Here is the general use case:

<?php

require_once ('Slimdown.php');

echo Slimdown::render (
	"# Page title\n\nAnd **now** for something _completely_ different."
);

Or via composer:

composer require jbroadway/slimdown

Then:

<?php

require __DIR__ . '/vendor/autoload.php';

echo Slimdown::render (
	"# Page title\n\nAnd **now** for something _completely_ different."
);

Adding rules

A simple rule to convert :) to an image:

<?php

Slimdown::add_rule ('/(\W)\:\)(\W)/', '\1<img src="smiley.png" />\2');

echo Slimdown::render ('Know what I\'m sayin? :)');

In this example, we add GitHub-style internal linking (e.g., [[Another Page]]).

<?php

function mywiki_internal_link ($title) {
	return sprintf (
		'<a href="%s">%s</a>',
		preg_replace ('/[^a-zA-Z0-9_-]+/', '-', $title),
		$title
	);
}

Slimdown::add_rule ('/\[\[(.*?)\]\]/e', 'mywiki_internal_link (\'\\1\')');

echo Slimdown::render ('Check [[This Page]] out!');

A longer example

<?php

echo Slimdown::render ("# Title

And *now* [a link](http://www.google.com) to **follow** and [another](http://yahoo.com/).

* One
* Two
* Three

## Subhead

One **two** three **four** five.

One __two__ three _four_ five __six__ seven _eight_.

1. One
2. Two
3. Three

More text with `inline($code)` sample.

> A block quote
> across two lines.

- [ ] One
- [x] Two
- [ ] Three

More text...");