mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
Move functions for MarkdownExtension
This commit is contained in:
@@ -21,28 +21,31 @@ final class MarkdownExtension extends AbstractExtension
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFilter('markdown_to_html', ['Twig\\Extra\\Markdown\\MarkdownRuntime', 'convert'], ['is_safe' => ['all']]),
|
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
|
/**
|
||||||
{
|
* @internal
|
||||||
static $converters;
|
*/
|
||||||
|
public static function htmlToMarkdown(string $body, array $options = []): string
|
||||||
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".');
|
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>'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.2.5",
|
"php": ">=7.2.5",
|
||||||
|
"symfony/deprecation-contracts": "^2.5|^3",
|
||||||
"twig/twig": "^3.0"
|
"twig/twig": "^3.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
@@ -26,6 +27,7 @@
|
|||||||
"michelf/php-markdown": "^1.8|^2.0"
|
"michelf/php-markdown": "^1.8|^2.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
"files": [ "Resources/functions.php" ],
|
||||||
"psr-4" : { "Twig\\Extra\\Markdown\\" : "" },
|
"psr-4" : { "Twig\\Extra\\Markdown\\" : "" },
|
||||||
"exclude-from-classmap": [
|
"exclude-from-classmap": [
|
||||||
"/Tests/"
|
"/Tests/"
|
||||||
|
|||||||
Reference in New Issue
Block a user