Move functions for MarkdownExtension

This commit is contained in:
Fabien Potencier
2023-12-09 18:24:26 +01:00
parent e07e9b5be3
commit 69a89e0e77
4 changed files with 75 additions and 20 deletions
+23 -20
View File
@@ -21,28 +21,31 @@ final class MarkdownExtension extends AbstractExtension
{
return [
new TwigFilter('markdown_to_html', ['Twig\\Extra\\Markdown\\MarkdownRuntime', 'convert'], ['is_safe' => ['all']]),
new TwigFilter('html_to_markdown', 'Twig\\Extra\\Markdown\\twig_html_to_markdown', ['is_safe' => ['all']]),
new TwigFilter('html_to_markdown', [self::class, 'htmlToMarkdown'], ['is_safe' => ['all']]),
];
}
}
function twig_html_to_markdown(string $body, array $options = []): string
{
static $converters;
if (!class_exists(HtmlConverter::class)) {
throw new \LogicException('You cannot use the "html_to_markdown" filter as league/html-to-markdown is not installed; try running "composer require league/html-to-markdown".');
/**
* @internal
*/
public static function htmlToMarkdown(string $body, array $options = []): string
{
static $converters;
if (!class_exists(HtmlConverter::class)) {
throw new \LogicException('You cannot use the "html_to_markdown" filter as league/html-to-markdown is not installed; try running "composer require league/html-to-markdown".');
}
$options += [
'hard_break' => true,
'strip_tags' => true,
'remove_nodes' => 'head style',
];
if (!isset($converters[$key = serialize($options)])) {
$converters[$key] = new HtmlConverter($options);
}
return $converters[$key]->convert($body);
}
$options += [
'hard_break' => true,
'strip_tags' => true,
'remove_nodes' => 'head style',
];
if (!isset($converters[$key = serialize($options)])) {
$converters[$key] = new HtmlConverter($options);
}
return $converters[$key]->convert($body);
}
@@ -0,0 +1,23 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Markdown;
/**
* @internal
* @deprecated since Twig 3.9.0
*/
function html_to_markdown(string $body, array $options = []): string
{
trigger_deprecation('twig/intl-extra', '3.9.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
return MarkdownExtension::htmlToMarkdown($body, $options);
}
@@ -0,0 +1,27 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Markdown\Tests;
use PHPUnit\Framework\TestCase;
use Twig\Extra\Markdown\MarkdownExtension;
use function Twig\Extra\Markdown\html_to_markdown;
/**
* @group legacy
*/
class LegacyFunctionsTest extends TestCase
{
public function testHtmlToMarkdown()
{
$this->assertSame(MarkdownExtension::htmlToMarkdown('<p>foo</p>'), html_to_markdown('<p>foo</p>'));
}
}
+2
View File
@@ -16,6 +16,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.5|^3",
"twig/twig": "^3.0"
},
"require-dev": {
@@ -26,6 +27,7 @@
"michelf/php-markdown": "^1.8|^2.0"
},
"autoload": {
"files": [ "Resources/functions.php" ],
"psr-4" : { "Twig\\Extra\\Markdown\\" : "" },
"exclude-from-classmap": [
"/Tests/"