Skip to content

Commit e7b7ff0

Browse files
authored
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
2 parents 3c901ec + 0586908 commit e7b7ff0

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

config/markdown.php

+14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@
5050
//
5151
],
5252

53+
/*
54+
|--------------------------------------------------------------------------
55+
| Allow all HTML tags
56+
|--------------------------------------------------------------------------
57+
|
58+
| HydePHP uses the GitHub Flavored Markdown extension to convert Markdown.
59+
| This, by default strips out some HTML tags. If you want to allow all
60+
| arbitrary HTML tags, and understand the risks involved, you can
61+
| use this config setting to enable all HTML tags.
62+
|
63+
*/
64+
65+
'allow_html' => false,
66+
5367
/*
5468
|--------------------------------------------------------------------------
5569
| Blade-supported Markdown

docs/advanced-markdown.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ category: "Digging Deeper"
88

99
## Introduction
1010

11-
Since HydePHP makes heavy use of Markdown there are some extra features and helpers
12-
created just for Hyde to make using Markdown even easier!
11+
Since HydePHP makes heavy use of Markdown there are some extra features and helpers created just for Hyde to make using Markdown even easier!
12+
13+
## Raw HTML Tags
14+
15+
HydePHP uses the GitHub Flavored Markdown extension to convert Markdown. This, by default strips out some HTML tags. If you want to allow all arbitrary HTML tags, and understand the risks involved, enable all HTML tags by setting the following option to true in your `config/markdown.php` file.
16+
17+
```php
18+
// filepath: config/markdown.php
19+
// torchlight! {"lineNumbers": false}
20+
'allow_html' => true,
21+
```
22+
23+
This, will behind the scenes add the bundled `DisallowedRawHtml` extension, and configure it so that no HTML tags are stripped out.
1324

1425
## Blade Support
1526

packages/framework/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);

packages/framework/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)