From 382a7f5acdd2ff0f995f760ce7a8173ad01761fa Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 17 Sep 2018 23:17:41 -0400 Subject: [PATCH] Create new HTML::toMarkdown wrapper --- src/Content/Text/BBCode.php | 4 +--- src/Content/Text/HTML.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index d0f512ce13..c3453bcf72 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -25,7 +25,6 @@ use Friendica\Util\Map; use Friendica\Util\Network; use Friendica\Util\ParseUrl; use Friendica\Util\Proxy as ProxyUtils; -use League\HTMLToMarkdown\HtmlConverter; class BBCode extends BaseObject { @@ -1942,8 +1941,7 @@ class BBCode extends BaseObject $stamp1 = microtime(true); // Now convert HTML to Markdown - $converter = new HtmlConverter(); - $text = $converter->convert($text); + $text = HTML::toMarkdown($text); // unmask the special chars back to HTML $text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $text); diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index 9a9f24f97f..c256717a15 100644 --- a/src/Content/Text/HTML.php +++ b/src/Content/Text/HTML.php @@ -11,6 +11,7 @@ use DOMXPath; use Friendica\Core\Addon; use Friendica\Util\Network; use Friendica\Util\XML; +use League\HTMLToMarkdown\HtmlConverter; class HTML { @@ -672,4 +673,19 @@ class HTML return trim($message); } + + /** + * Converts provided HTML code to Markdown. The hardwrap parameter maximizes + * compatibility with Diaspora in spite of the Markdown standards. + * + * @param string $html + * @return string + */ + public static function toMarkdown($html) + { + $converter = new HtmlConverter(['hard_break' => true]); + $markdown = $converter->convert($html); + + return $markdown; + } }