Skip to content

Commit c64a5d2

Browse files
author
github-actions
committed
Merge pull request #223 from hydephp/196-add-easy-config-option-to-enable-html-in-markdown-config
Add easy config option to enable HTML in Markdown config hydephp/develop@e7b7ff0
1 parent 22f1116 commit c64a5d2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Services/MarkdownConverterService.php

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Hyde\Framework\Services\Markdown\CodeblockFilepathProcessor;
99
use Hyde\Framework\Services\Markdown\ShortcodeProcessor;
1010
use League\CommonMark\CommonMarkConverter;
11+
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
1112
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
1213
use Torchlight\Commonmark\V2\TorchlightExtension;
1314

@@ -85,6 +86,16 @@ protected function setupConverter(): void
8586
$this->addExtension(TorchlightExtension::class);
8687
}
8788

89+
if (config('markdown.allow_html', false)) {
90+
$this->addExtension(DisallowedRawHtmlExtension::class);
91+
92+
$this->config = array_merge([
93+
'disallowed_raw_html' => [
94+
'disallowed_tags' => [],
95+
],
96+
], $this->config);
97+
}
98+
8899
// Add any custom extensions defined in config
89100
foreach (config('markdown.extensions', []) as $extensionClassName) {
90101
$this->addExtension($extensionClassName);

tests/Feature/MarkdownConverterServiceTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,23 @@ public function test_bladedown_can_be_enabled()
7777
$service->addFeature('bladedown')->parse();
7878
$this->assertEquals("Hello World!\n", $service->parse());
7979
}
80+
81+
// test raw html tags are stripped by default
82+
public function test_raw_html_tags_are_stripped_by_default()
83+
{
84+
$markdown = '<p>foo</p><style>bar</style><script>hat</script>';
85+
$service = new MarkdownConverterService($markdown);
86+
$html = $service->parse();
87+
$this->assertEquals("<p>foo</p>&lt;style>bar&lt;/style>&lt;script>hat&lt;/script>\n", $html);
88+
}
89+
90+
// test raw html tags are not stripped when explicitly enabled
91+
public function test_raw_html_tags_are_not_stripped_when_explicitly_enabled()
92+
{
93+
config(['markdown.allow_html' =>true]);
94+
$markdown = '<p>foo</p><style>bar</style><script>hat</script>';
95+
$service = new MarkdownConverterService($markdown);
96+
$html = $service->parse();
97+
$this->assertEquals("<p>foo</p><style>bar</style><script>hat</script>\n", $html);
98+
}
8099
}

0 commit comments

Comments
 (0)