Create new HTML::toMarkdown wrapper

This commit is contained in:
Hypolite Petovan 2018-09-17 23:17:41 -04:00
parent 87c425e388
commit 382a7f5acd
2 changed files with 17 additions and 3 deletions

View File

@ -25,7 +25,6 @@ use Friendica\Util\Map;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\ParseUrl; use Friendica\Util\ParseUrl;
use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Proxy as ProxyUtils;
use League\HTMLToMarkdown\HtmlConverter;
class BBCode extends BaseObject class BBCode extends BaseObject
{ {
@ -1942,8 +1941,7 @@ class BBCode extends BaseObject
$stamp1 = microtime(true); $stamp1 = microtime(true);
// Now convert HTML to Markdown // Now convert HTML to Markdown
$converter = new HtmlConverter(); $text = HTML::toMarkdown($text);
$text = $converter->convert($text);
// unmask the special chars back to HTML // unmask the special chars back to HTML
$text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $text); $text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $text);

View File

@ -11,6 +11,7 @@ use DOMXPath;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\XML; use Friendica\Util\XML;
use League\HTMLToMarkdown\HtmlConverter;
class HTML class HTML
{ {
@ -672,4 +673,19 @@ class HTML
return trim($message); 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;
}
} }