├── .gitignore ├── .gitattributes ├── .editorconfig ├── composer.json ├── index.php ├── LICENSE.md ├── README.md ├── composer.lock └── src └── Chopper.php /.gitignore: -------------------------------------------------------------------------------- 1 | # OS files 2 | .DS_Store 3 | 4 | # npm modules 5 | /node_modules 6 | 7 | # Composer files 8 | /vendor -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Note: You need to uncomment the lines you want to use; the other lines can be deleted 2 | 3 | # Git 4 | # .gitattributes export-ignore 5 | # .gitignore export-ignore 6 | 7 | # Tests 8 | # /.coveralls.yml export-ignore 9 | # /.travis.yml export-ignore 10 | # /phpunit.xml.dist export-ignore 11 | # /tests/ export-ignore 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.php] 13 | indent_size = 4 14 | 15 | [*.md,*.txt] 16 | trim_trailing_whitespace = false 17 | insert_final_newline = false 18 | 19 | [composer.json] 20 | indent_size = 4 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hashandsalt/kirby-chopper", 3 | "description": "Kirby 3 Chopper plugin for truncating text", 4 | "type": "kirby-plugin", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "James Steel", 9 | "email": "hello@hashandsalt.com" 10 | } 11 | ], 12 | "require": { 13 | "getkirby/composer-installer": "^1.1" 14 | }, 15 | "config": { 16 | "optimize-autoloader": true 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 'src/Chopper.php' 5 | ], __DIR__); 6 | 7 | use HashAndSalt\Chopper\Chopper; 8 | 9 | Kirby::plugin('hashandsalt/chopper', [ 10 | 11 | 12 | 'options' => [ 13 | 'ellipsis' => '…', 14 | 'exact' => true, 15 | 'html' => true, 16 | 'trimWidth' => false, 17 | 'keep' => '
',
18 | ],
19 |
20 | 'fieldMethods' => [
21 | 'chopper' => function ($field, $length = 250, $options = []) {
22 |
23 | $options += [
24 | 'ellipsis' => option('hashandsalt.chopper.ellipsis'), 'exact' => option('hashandsalt.chopper.exact'), 'html' => option('hashandsalt.chopper.html'), 'trimWidth' => option('hashandsalt.chopper.trimWidth'),
25 | ];
26 |
27 | $chopper = $field->kt();
28 | $field = Chopper::truncate($chopper, $length, $options);
29 |
30 | $field = strip_tags($field, option('hashandsalt.chopper.keep'));
31 |
32 | return $field;
33 | }
34 | ]
35 |
36 | ]);
37 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Hash&Salt
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Kirby Chopper Plugin for Kirby 3
2 |
3 | Kirby's built in [Excerpt](https://getkirby.com/docs/reference/templates/field-methods/excerpt) takes an all or nothing approach to stripping html tags, and only works on characters. This plugin creates excerpts from fields via KirbyText but keeps any HTML tags, with the ability to define the tags you wish to keep. Also works on whole words, as well as characters.
4 |
5 |
6 | ## Commerical Usage
7 |
8 | This plugin is free but if you use it in a commercial project please consider to
9 | - [make a donation 🍻](https://paypal.me/hashandsalt?locale.x=en_GB) or
10 | - [buy a Kirby license using this affiliate link](https://a.paddle.com/v2/click/1129/36141?link=1170)
11 |
12 |
13 | ## Installation
14 |
15 | ### Manual
16 |
17 | To use this plugin, place all the files in `site/plugins/chopper/`.
18 |
19 | ### Composer
20 |
21 | ```
22 | composer require hashandsalt/kirby-chopper
23 | ```
24 |
25 | ## Usage
26 |
27 | Defaults to limiting to 250 charchters, maintaining whole words, so if you just want 20 words and an ellipsis on the end:
28 |
29 | ```
30 | = $page->yourtextfield()->chopper() ?>
31 | ```
32 |
33 | To set number of charachters to trim to
34 |
35 | ```
36 | = $page->yourtextfield()->chopper(100) ?>
37 | ```
38 |
39 | To trim to 50 charachters, maintaining whole words, append an arrow on the on the end:
40 |
41 | ```
42 | = $page->yourtextfield()->chopper(50, ['ellipsis', '→']) ?>
43 | ```
44 |
45 | To change the default list of kept tags, add this line to your `config.php` and amend accordingly:
46 |
47 | ```
48 | 'hashandsalt.chopper.keep' => '