diff --git a/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php b/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php --- a/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php +++ b/src/infrastructure/markup/blockrule/PhutilRemarkupCodeBlockRule.php @@ -44,11 +44,18 @@ } public function markupText($text, $children) { - if (preg_match('/^\s*```/', $text)) { + $very_first_line = null; + if (preg_match('/^\s*```(.*)/', $text, $matches)) { // If this is a ```-style block, trim off the backticks and any leading // blank line. $text = preg_replace('/^\s*```(\s*\n)?/', '', $text); $text = preg_replace('/```\s*$/', '', $text); + + // Just after the opening backticks there can be something valuable. + // Note that the general options are not necessarily here. + if (isset($matches[1])) { + $very_first_line = $matches[1]; + } } $lines = explode("\n", $text); @@ -79,6 +86,17 @@ } } + // If explicitly missing, eventually parse the language expressed + // just after the first backticks. + // Skip one-line blocks, to do not eat potential one-line content. + // If found, we remove that line since it is not content. + if (empty($options['lang']) && $very_first_line && count($lines) > 1) { + if (preg_match('/^[a-z0-9\-\+\#\/]+$/', $very_first_line)) { + $options['lang'] = $very_first_line; + array_shift($lines); + } + } + // Normalize the text back to a 0-level indent. $min_indent = 80; foreach ($lines as $line) {