Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add support for both CommonMark 1.x and 2.x #37954

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"doctrine/inflector": "^1.4|^2.0",
"dragonmantank/cron-expression": "^3.0.2",
"egulias/email-validator": "^2.1.10",
"league/commonmark": "^1.3",
"league/commonmark": "^1.3|^2.0",
Copy link
Contributor Author

@colinodell colinodell Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically you'll want ^1.3|^2.0.0-beta3, as the type hints in the first two betas do not support $converter->getEnvironment()->addExtension() as used below.

"league/flysystem": "^1.1",
"monolog/monolog": "^2.0",
"nesbot/carbon": "^2.31",
Expand Down
11 changes: 4 additions & 7 deletions src/Illuminate/Mail/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Extension\Table\TableExtension;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

Expand Down Expand Up @@ -104,15 +103,13 @@ public function renderText($view, array $data = [])
*/
public static function parse($text)
{
$environment = Environment::createCommonMarkEnvironment();

$environment->addExtension(new TableExtension);

$converter = new CommonMarkConverter([
'allow_unsafe_links' => false,
], $environment);
]);

$converter->getEnvironment()->addExtension(new TableExtension());

return new HtmlString($converter->convertToHtml($text));
return new HtmlString((string) $converter->convertToHtml($text));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 1.x the convertToHtml() method returns a string. In 2.x it returns a Stringable object. Casting the output to a string allows us to support both.

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Mail/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"illuminate/contracts": "^8.0",
"illuminate/macroable": "^8.0",
"illuminate/support": "^8.0",
"league/commonmark": "^1.3",
"league/commonmark": "^1.3|^2.0",
"psr/log": "^1.0",
"swiftmailer/swiftmailer": "^6.0",
"tijsverkoyen/css-to-inline-styles": "^2.2.2"
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public static function markdown($string, array $options = [])
{
$converter = new GithubFlavoredMarkdownConverter($options);

return $converter->convertToHtml($string);
return (string) $converter->convertToHtml($string);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"suggest": {
"illuminate/filesystem": "Required to use the composer class (^8.0).",
"league/commonmark": "Required to use Str::markdown() and Stringable::markdown() (^1.3).",
"league/commonmark": "Required to use Str::markdown() and Stringable::markdown() (^1.3|^2.0).",
"ramsey/uuid": "Required to use Str::uuid() (^4.0).",
"symfony/process": "Required to use the composer class (^5.1.4).",
"symfony/var-dumper": "Required to use the dd function (^5.1.4).",
Expand Down