From 710bc5b8ca3ee67460015a90b5179f38b5461429 Mon Sep 17 00:00:00 2001 From: upstream Date: Fri, 2 Oct 2026 16:19:40 +0200 Subject: [PATCH] Upstream thephpleague/html-to-markdown state of the touched files before e793ef24d6 --- src/Converter/PreformattedConverter.php | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/Converter/PreformattedConverter.php diff --git a/src/Converter/PreformattedConverter.php b/src/Converter/PreformattedConverter.php new file mode 100644 index 0000000..af6dd10 --- /dev/null +++ b/src/Converter/PreformattedConverter.php @@ -0,0 +1,58 @@ +getChildrenAsString()); + $preContent = \preg_replace('/]*>/', '', $preContent); + \assert($preContent !== null); + $preContent = \str_replace('', '', $preContent); + + /* + * Checking for the code tag. + * Usually pre tags are used along with code tags. This conditional will check for already converted code tags, + * which use backticks, and if those backticks are at the beginning and at the end of the string it means + * there's no more information to convert. + */ + + $firstBacktick = \strpos(\trim($preContent), '`'); + $lastBacktick = \strrpos(\trim($preContent), '`'); + if ($firstBacktick === 0 && $lastBacktick === \strlen(\trim($preContent)) - 1) { + return $preContent . "\n\n"; + } + + // If the execution reaches this point it means it's just a pre tag, with no code tag nested + + // Empty lines are a special case + if ($preContent === '') { + return "```\n```\n\n"; + } + + // Normalizing new lines + $preContent = \preg_replace('/\r\n|\r|\n/', "\n", $preContent); + \assert(\is_string($preContent)); + + // Ensure there's a newline at the end + if (\strrpos($preContent, "\n") !== \strlen($preContent) - \strlen("\n")) { + $preContent .= "\n"; + } + + // Use three backticks + return "```\n" . $preContent . "```\n\n"; + } + + /** + * @return string[] + */ + public function getSupportedTags(): array + { + return ['pre']; + } +}