protected function parseLinkOrImage($markdown)
{
if (strpos($markdown, ']') !== false && preg_match('/\\[((?>[^\\]\\[]+|(?R))*)\\]/', $markdown, $textMatches)) {
// TODO improve bracket regex
$text = $textMatches[1];
$offset = strlen($textMatches[0]);
$markdown = substr($markdown, $offset);
$pattern = <<<REGEXP
/(?(R) # in case of recursion match parentheses
\\(((?>[^\\s()]+)|(?R))*\\)
| # else match a link with title
^\\((((?>[^\\s()]+)|(?R))*)(\\s+"(.*?)")?\\)?(\\s+=([0-9]+)?x([0-9]+)?)?\\)
)/x
REGEXP;
if (preg_match($pattern, $markdown, $refMatches)) {
// inline link
return [$text, isset($refMatches[2]) ? $refMatches[2] : '', empty($refMatches[5]) ? null : $refMatches[5], $offset + strlen($refMatches[0]), null, isset($refMatches[7]) ? $refMatches[7] : null, isset($refMatches[8]) ? $refMatches[8] : null];
} elseif (preg_match('/^([ \\n]?\\[(.*?)\\])?/s', $markdown, $refMatches)) {
// reference style link
if (empty($refMatches[2])) {
$key = strtolower($text);
} else {
$key = strtolower($refMatches[2]);
}
return [$text, null, null, $offset + strlen($refMatches[0]), $key];
}
}
return false;
}