parsedown->getTextLevelElements(); array_walk($textLevelElements, function (&$element) { $element = preg_quote($element, '/'); }); $this->textLevelElementRegex = '\b(?:' . implode('|', $textLevelElements) . ')\b'; } /** * @dataProvider data * @param $section * @param $markdown * @param $expectedHtml */ public function testExample($section, $markdown, $expectedHtml) { $expectedHtml = $this->cleanupHtml($expectedHtml); $actualHtml = $this->parsedown->text($markdown); $actualHtml = $this->cleanupHtml($actualHtml); $this->assertEquals($expectedHtml, $actualHtml); } protected function cleanupHtml($markup) { // invisible whitespaces at the beginning and end of block elements // however, whitespaces at the beginning of
elements do matter $markup = preg_replace( array( '/(<(?!(?:' . $this->textLevelElementRegex . '|\bpre\b))\w+\b[^>]*>(?:<' . $this->textLevelElementRegex . '[^>]*>)*)\s+/s', '/\s+((?:<\/' . $this->textLevelElementRegex . '>)*<\/(?!' . $this->textLevelElementRegex . ')\w+\b>)/s' ), '$1', $markup ); return $markup; } }