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
\t\t\t\t/(?(R) # in case of recursion match parentheses
\t\t\t\t\t \\(((?>[^\\s()]+)|(?R))*\\)
\t\t\t\t| # else match a link with title
\t\t\t\t\t^\\(\\s*(((?>[^\\s()]+)|(?R))*)(\\s+"(.*?)")?\\s*\\)
\t\t\t\t)/x
REGEXP;
if (preg_match($pattern, $markdown, $refMatches)) {
// inline link
return [$text, isset($refMatches[2]) ? $this->replaceEscape($refMatches[2]) : '', empty($refMatches[5]) ? null : $refMatches[5], $offset + strlen($refMatches[0]), 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;
}