Skip to content

Commit

Permalink
Move functions for MarkdownExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 9, 2023
1 parent e07e9b5 commit 69a89e0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
43 changes: 23 additions & 20 deletions extra/markdown-extra/MarkdownExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,31 @@ public function getFilters()
{
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".');
}

$options += [
'hard_break' => true,
'strip_tags' => true,
'remove_nodes' => 'head style',
];

if (!isset($converters[$key = serialize($options)])) {
$converters[$key] = new HtmlConverter($options);
/**
* @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);
}

return $converters[$key]->convert($body);
}
23 changes: 23 additions & 0 deletions extra/markdown-extra/Resources/functions.php
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 27 additions & 0 deletions extra/markdown-extra/Tests/LegacyFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions extra/markdown-extra/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.5|^3",
"twig/twig": "^3.0"
},
"require-dev": {
Expand All @@ -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/"
Expand Down

0 comments on commit 69a89e0

Please sign in to comment.