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)) { + $header_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])) { + $header_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']) && $header_line && count($lines) > 1) { + if (preg_match('/^[a-z0-9\-\+\#\/]+$/', $header_line)) { + $options['lang'] = $header_line; + array_shift($lines); + } + } + // Normalize the text back to a 0-level indent. $min_indent = 80; foreach ($lines as $line) { diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-markdown.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-markdown.txt new file mode 100644 --- /dev/null +++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/tick-block-multi-flavored-markdown.txt @@ -0,0 +1,19 @@ +```c++ +code + +more code + +more code``` + +~~~~~~~~~~ +
code
+
+more code
+
+more code
+~~~~~~~~~~ + code + + more code + + more code