acf_parse_markdown()ACF 5.7.2

acf_parse_markdown

A very basic regex-based Markdown parser function based off slimdown.

No Hooks.

Returns

String.

Usage

acf_parse_markdown( $text );
$text(string)
The string to parse.
Default: ''

Changelog

Since 5.7.2 Introduced.

acf_parse_markdown() code ACF 6.8.8

function acf_parse_markdown( $text = '' ) {

	// trim
	$text = trim( $text );

	// rules
	$rules = array(
		'/=== (.+?) ===/'            => '<h2>$1</h2>',                   // headings
		'/== (.+?) ==/'              => '<h3>$1</h3>',                   // headings
		'/= (.+?) =/'                => '<h4>$1</h4>',                   // headings
		'/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>',           // links
		'/(\*\*)(.*?)\1/'            => '<strong>$2</strong>',           // bold
		'/(\*)(.*?)\1/'              => '<em>$2</em>',                   // intalic
		'/`(.*?)`/'                  => '<code>$1</code>',               // inline code
		'/\n\*(.*)/'                 => "\n<ul>\n\t<li>$1</li>\n</ul>",  // ul lists
		'/\n[0-9]+\.(.*)/'           => "\n<ol>\n\t<li>$1</li>\n</ol>",  // ol lists
		'/<\/ul>\s?<ul>/'            => '',                              // fix extra ul
		'/<\/ol>\s?<ol>/'            => '',                              // fix extra ol
	);
	foreach ( $rules as $k => $v ) {
		$text = preg_replace( $k, $v, $text );
	}

	// autop
	$text = wpautop( $text );

	// return
	return $text;
}