diff options
author | zotlabs <mike@macgirvin.com> | 2018-10-30 18:11:49 -0700 |
---|---|---|
committer | zotlabs <mike@macgirvin.com> | 2018-10-30 18:11:49 -0700 |
commit | 70c55da1df69d90dcbeb5a78c994b23a8456bfc9 (patch) | |
tree | 86d39069b02ecabe4ed091514e5076a98abda43a /vendor/league/html-to-markdown/src/Converter/CodeConverter.php | |
parent | 623aa7ea48149ca7c2bc556931f25befdf49e58a (diff) | |
parent | f9ab7647dd660adb37464614616cb8484c500de4 (diff) | |
download | volse-hubzilla-70c55da1df69d90dcbeb5a78c994b23a8456bfc9.tar.gz volse-hubzilla-70c55da1df69d90dcbeb5a78c994b23a8456bfc9.tar.bz2 volse-hubzilla-70c55da1df69d90dcbeb5a78c994b23a8456bfc9.zip |
Merge branch 'dev' of https://framagit.org/hubzilla/core into xdev_merge
Diffstat (limited to 'vendor/league/html-to-markdown/src/Converter/CodeConverter.php')
-rw-r--r-- | vendor/league/html-to-markdown/src/Converter/CodeConverter.php | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php index e536362ee..39e6a7bc4 100644 --- a/vendor/league/html-to-markdown/src/Converter/CodeConverter.php +++ b/vendor/league/html-to-markdown/src/Converter/CodeConverter.php @@ -13,7 +13,7 @@ class CodeConverter implements ConverterInterface */ public function convert(ElementInterface $element) { - $language = null; + $language = ''; // Checking for language class on the code block $classes = $element->getAttribute('class'); @@ -24,8 +24,7 @@ class CodeConverter implements ConverterInterface foreach ($classes as $class) { if (strpos($class, 'language-') !== false) { // Found one, save it as the selected language and stop looping over the classes. - // The space after the language avoids gluing the actual code with the language tag - $language = str_replace('language-', '', $class) . ' '; + $language = str_replace('language-', '', $class); break; } } @@ -39,14 +38,13 @@ class CodeConverter implements ConverterInterface $code = preg_replace('/<code\b[^>]*>/', '', $code); $code = str_replace('</code>', '', $code); - // Checking if the code has multiple lines - $lines = preg_split('/\r\n|\r|\n/', $code); - if (count($lines) > 1) { - // Multiple lines detected, adding three backticks and newlines - $markdown .= '```' . $language . "\n" . $code . "\n" . '```' . "\n\n"; + // Checking if it's a code block or span + if ($this->shouldBeBlock($element, $code)) { + // Code block detected, newlines will be added in parent + $markdown .= '```' . $language . "\n" . $code . "\n" . '```'; } else { - // One line of code, wrapping it on one backtick. - $markdown .= '`' . $language . $code . '`'; + // One line of code, wrapping it on one backtick, removing new lines + $markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`'; } return $markdown; @@ -59,4 +57,23 @@ class CodeConverter implements ConverterInterface { return array('code'); } + + /** + * @param ElementInterface $element + * @param string $code + * + * @return bool + */ + private function shouldBeBlock(ElementInterface $element, $code) + { + if ($element->getParent()->getTagName() == 'pre') { + return true; + } + + if (preg_match('/[^\s]` `/', $code)) { + return true; + } + + return false; + } } |