Skip to content

Commit 77dde98

Browse files
committed
Initial commit
0 parents  commit 77dde98

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 kenshō digital
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Kirby HTMLDoc
2+
3+
Minifies HTML5 output for [Kirby][1] templates.
4+
5+
## General
6+
7+
Integrates [Hexydec’s][2] excellent [HTMLDoc library][3] into Kirby projects to minify HTML5 output.
8+
9+
## Installation
10+
11+
```shell
12+
composer require kenshodigital/kirby-htmldoc ^1.0
13+
```
14+
15+
## Usage
16+
17+
Basically works out of the box without any additional configuration.
18+
19+
### Configuration
20+
21+
However, applicable content types as well as the library’s [default configuration][4] can be easily modified in your site’s [`config.php`][5].
22+
23+
```php
24+
<?php
25+
26+
return [
27+
'kensho.htmldoc' => [
28+
'contentTypes' = [
29+
'htm',
30+
'html',
31+
],
32+
'options' => [
33+
'quotes' => false,
34+
'urls' => [
35+
'relative' => false,
36+
'parent' => false,
37+
],
38+
...
39+
],
40+
],
41+
];
42+
```
43+
44+
#### Defaults
45+
46+
Applicable content types are set to `htm` and `html` by default. For the library, only the setting for quotes deviates from the library’s default and is set to `false` in this plugin.
47+
48+
[1]: https://getkirby.com
49+
[2]: https://github.com/hexydec
50+
[3]: https://github.com/hexydec/htmldoc
51+
[4]: https://github.com/hexydec/htmldoc/blob/master/docs/api/minify.md#options
52+
[5]: https://getkirby.com/docs/guide/configuration#the-config-php

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "kenshodigital/kirby-htmldoc",
3+
"description": "Minifies HTML5 output for Kirby templates.",
4+
"type": "kirby-plugin",
5+
"version": "1.0.0",
6+
"homepage": "https://github.com/kenshodigital/kirby-htmldoc",
7+
"license": "MIT",
8+
"authors":
9+
[
10+
{
11+
"name": "Jens Scherbl",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"require":
16+
{
17+
"php": "^8.2",
18+
"getkirby/cms": "^4.0",
19+
"getkirby/composer-installer": "^1.2",
20+
"hexydec/htmldoc": "^1.8"
21+
},
22+
"autoload":
23+
{
24+
"psr-4":
25+
{
26+
"Kensho\\HTMLDoc\\": "src"
27+
}
28+
}
29+
}

index.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
use hexydec\html;
4+
use Kensho\HTMLDoc\HTMLDoc;
5+
use Kirby\Cms\App;
6+
7+
App::plugin('kensho/htmldoc', [
8+
'options' => [
9+
'contentTypes' => [
10+
'htm',
11+
'html',
12+
],
13+
'options' => [
14+
'quotes' => true,
15+
],
16+
],
17+
'hooks' => [
18+
'page.render:after' => function (string $contentType, array $data, string $html): string {
19+
$kirby = App::instance();
20+
$lib = new html\htmldoc;
21+
$HTMLDoc = new HTMLDoc($kirby, $lib);
22+
23+
if ($HTMLDoc->isApplicable($contentType)) {
24+
return $HTMLDoc->getMinified($html);
25+
}
26+
return $html;
27+
},
28+
],
29+
]);

src/HTMLDoc.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Kensho\HTMLDoc;
4+
5+
use hexydec\html;
6+
use Kirby\Cms\App;
7+
8+
readonly class HTMLDoc
9+
{
10+
private const OPTION_CONTENT_TYPES = 'kensho.htmldoc.contentTypes';
11+
private const OPTION_OPTIONS = 'kensho.htmldoc.options';
12+
13+
public function __construct(
14+
private App $kirby,
15+
private html\htmldoc $lib,
16+
) {}
17+
18+
public function isApplicable(string $contentType): bool
19+
{
20+
$contentTypes = $this->getContentTypes();
21+
22+
return \in_array($contentType, $contentTypes);
23+
}
24+
25+
private function getContentTypes(): array
26+
{
27+
return $this->kirby->option(self::OPTION_CONTENT_TYPES, []);
28+
}
29+
30+
public function getMinified(string $html): string
31+
{
32+
if ($this->lib->load($html)) {
33+
$options = $this->getOptions();
34+
$this->lib->minify($options);
35+
return $this->lib->html();
36+
}
37+
return $html;
38+
}
39+
40+
private function getOptions(): array
41+
{
42+
return $this->kirby->option(self::OPTION_OPTIONS, []);
43+
}
44+
}

0 commit comments

Comments
 (0)