Test with CommonMark too

This commit is contained in:
Ion Bazan
2024-11-03 19:11:32 +08:00
parent bf43aa2882
commit 4df1b977e3
4 changed files with 263 additions and 8 deletions

View File

@@ -18,6 +18,7 @@
"bjeavons/zxcvbn-php": "^1.3.1",
"doctrine/dbal": "^3.9.3",
"gabrielelana/byte-units": "^0.5.0",
"graham-campbell/markdown": "^15.2",
"guzzlehttp/guzzle": "^7.9.2",
"hdvinnie/laravel-joypixel-emojis": "^v3.0.0",
"hdvinnie/laravel-security-headers": "^v3.0.0",

82
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "8968ba4e56e6b77cdb2c5fa7fb05a61a",
"content-hash": "2b7b1571638ee3814dcd8de9c741fd01",
"packages": [
{
"name": "assada/laravel-achievements",
@@ -1199,6 +1199,86 @@
},
"time": "2018-01-11T10:40:03+00:00"
},
{
"name": "graham-campbell/markdown",
"version": "v15.2.0",
"source": {
"type": "git",
"url": "https://github.com/GrahamCampbell/Laravel-Markdown.git",
"reference": "d594fc197b9068de5e234a890be361807a1ab34f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/GrahamCampbell/Laravel-Markdown/zipball/d594fc197b9068de5e234a890be361807a1ab34f",
"reference": "d594fc197b9068de5e234a890be361807a1ab34f",
"shasum": ""
},
"require": {
"illuminate/contracts": "^8.75 || ^9.0 || ^10.0 || ^11.0",
"illuminate/filesystem": "^8.75 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^8.75 || ^9.0 || ^10.0 || ^11.0",
"illuminate/view": "^8.75 || ^9.0 || ^10.0 || ^11.0",
"league/commonmark": "^2.4.2",
"php": "^7.4.15 || ^8.0.2"
},
"require-dev": {
"graham-campbell/analyzer": "^4.1",
"graham-campbell/testbench": "^6.1",
"mockery/mockery": "^1.6.6",
"phpunit/phpunit": "^9.6.17 || ^10.5.13"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"GrahamCampbell\\Markdown\\MarkdownServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"GrahamCampbell\\Markdown\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
}
],
"description": "Markdown Is A CommonMark Wrapper For Laravel",
"keywords": [
"Graham Campbell",
"GrahamCampbell",
"Laravel Markdown",
"Laravel-Markdown",
"common mark",
"commonmark",
"framework",
"laravel",
"markdown"
],
"support": {
"issues": "https://github.com/GrahamCampbell/Laravel-Markdown/issues",
"source": "https://github.com/GrahamCampbell/Laravel-Markdown/tree/v15.2.0"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/graham-campbell/markdown",
"type": "tidelift"
}
],
"time": "2024-03-17T23:07:39+00:00"
},
{
"name": "graham-campbell/result-type",
"version": "v1.1.3",

159
config/markdown.php Normal file
View File

@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);
/*
* This file is part of Laravel Markdown.
*
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
/*
|--------------------------------------------------------------------------
| Enable View Integration
|--------------------------------------------------------------------------
|
| This option specifies if the view integration is enabled so you can write
| markdown views and have them rendered as html. The following extensions
| are currently supported: ".md", ".md.php", and ".md.blade.php". You may
| disable this integration if it is conflicting with another package.
|
| Default: true
|
*/
'views' => true,
/*
|--------------------------------------------------------------------------
| CommonMark Extensions
|--------------------------------------------------------------------------
|
| This option specifies what extensions will be automatically enabled.
| Simply provide your extension class names here.
|
| Default: [
| League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension::class,
| League\CommonMark\Extension\Table\TableExtension::class,
| ]
|
*/
'extensions' => [
League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension::class,
League\CommonMark\Extension\Autolink\AutolinkExtension::class,
League\CommonMark\Extension\Table\TableExtension::class,
League\CommonMark\Extension\Strikethrough\StrikethroughExtension::class,
League\CommonMark\Extension\Attributes\AttributesExtension::class,
League\CommonMark\Extension\DescriptionList\DescriptionListExtension::class,
League\CommonMark\Extension\Footnote\FootnoteExtension::class,
],
/*
|--------------------------------------------------------------------------
| Renderer Configuration
|--------------------------------------------------------------------------
|
| This option specifies an array of options for rendering HTML.
|
| Default: [
| 'block_separator' => "\n",
| 'inner_separator' => "\n",
| 'soft_break' => "\n",
| ]
|
*/
'renderer' => [
'block_separator' => "\n",
'inner_separator' => "\n",
'soft_break' => "\n",
],
/*
|--------------------------------------------------------------------------
| Commonmark Configuration
|--------------------------------------------------------------------------
|
| This option specifies an array of options for commonmark.
|
| Default: [
| 'enable_em' => true,
| 'enable_strong' => true,
| 'use_asterisk' => true,
| 'use_underscore' => true,
| 'unordered_list_markers' => ['-', '+', '*'],
| ]
|
*/
'commonmark' => [
'enable_em' => true,
'enable_strong' => true,
'use_asterisk' => true,
'use_underscore' => true,
'unordered_list_markers' => ['-', '+', '*'],
],
/*
|--------------------------------------------------------------------------
| HTML Input
|--------------------------------------------------------------------------
|
| This option specifies how to handle untrusted HTML input.
|
| Default: 'strip'
|
*/
'html_input' => 'allow',
/*
|--------------------------------------------------------------------------
| Allow Unsafe Links
|--------------------------------------------------------------------------
|
| This option specifies whether to allow risky image URLs and links.
|
| Default: true
|
*/
'allow_unsafe_links' => true,
/*
|--------------------------------------------------------------------------
| Maximum Nesting Level
|--------------------------------------------------------------------------
|
| This option specifies the maximum permitted block nesting level.
|
| Default: PHP_INT_MAX
|
*/
'max_nesting_level' => PHP_INT_MAX,
/*
|--------------------------------------------------------------------------
| Slug Normalizer
|--------------------------------------------------------------------------
|
| This option specifies an array of options for slug normalization.
|
| Default: [
| 'max_length' => 255,
| 'unique' => 'document',
| ]
|
*/
'slug_normalizer' => [
'max_length' => 255,
'unique' => 'document',
],
];

View File

@@ -15,27 +15,25 @@ declare(strict_types=1);
*/
use App\Helpers\MarkdownExtra;
use League\CommonMark\MarkdownConverter;
describe('markdown support', tests: function (): void {
it(
'Generates HTML from Markdown',
function (
string $service,
string $result,
string $markdown,
bool $minify = true,
bool $strict = false,
bool $safeMode = false
): void {
$service = (new MarkdownExtra())
->setSafeMode($safeMode)
->setStrictMode($strict);
$html = $service->parse($markdown);
$html = convertToMarkdown($service, $markdown, $strict, $safeMode);
$html = $minify ? str_replace(["\r\n", "\r", "\n"], '', $html) : $html;
$this->assertEquals($result, $html);
$this->assertEquals($result, rtrim($html));
}
)->with(function (): iterable {
)->with(['default', 'commonmark'])->with(function (): iterable {
yield from basicMarkdown();
yield from codeBlocks();
@@ -55,6 +53,23 @@ describe('markdown support', tests: function (): void {
});
});
function convertToMarkdown(string $converter, string $input, bool $strict = false, bool $safeMode = false): string
{
if ($converter === 'commonmark') {
/** @var MarkdownConverter $service */
$service = app(MarkdownConverter::class);
$service->getEnvironment()->mergeConfig([
'html_input' => $safeMode ? 'escape' : 'allow',
]);
return $service->convert($input)->getContent();
}
return (new MarkdownExtra())
->setSafeMode($safeMode)
->setStrictMode($strict)->parse($input);
}
function basicMarkdown(): iterable
{
yield 'heading' => ['<h1>Heading</h1>', '# Heading'];