Skip to content

Commit c4ad146

Browse files
authored
Merge pull request #28 from jacksleight/v3
2 parents 4327ea9 + d2558d6 commit c4ad146

40 files changed

+1241
-623
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ composer.lock
33
vendor
44
node_modules
55
.phpunit.result.cache
6-
vite.hot
6+
vite.hot
7+
.phpunit.cache/test-results

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## 3.0.0 (2024-09-03)
4+
5+
This release introduces some significant changes to the terminology and variables used in mutators, however there are only two very small breaking changes, please read [upgrade guide](https://jacksleight.dev/docs/bard-mutator/upgrade-2-0-to-3-0) for more information.
6+
7+
- [new] Mutators are now known as plugins
8+
- [new] Class based plugins for easier organisation and reuse
9+
- [new] Scoped plugins that can be enabled per-field
10+
- [new] New helpers and metadata for advanced data manipulation
11+
- Easier setup with no custom editor class binding
12+
- Better indexing of parent and sibling items
13+
- More flexible type name handling
14+
- Statamic 5.0+ is now required
15+
316
## 2.3.1 (2024-05-07)
417

518
- Statamic 5 support

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Mutator::html('link', function ($value) {
3030
use Illuminate\Support\Str;
3131
use JackSleight\StatamicBardMutator\Facades\Mutator;
3232

33-
Mutator::html('heading', function ($value, $data) {
34-
if ($data->attrs->level === 2) {
35-
$value[1]['id'] = Str::slug(collect($data->content)->implode('text', ''));
33+
Mutator::html('heading', function ($value, $item) {
34+
if ($item->attrs->level === 2) {
35+
$value[1]['id'] = Str::slug(collect($item->content)->implode('text', ''));
3636
}
3737
return $value;
3838
});

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
}
2323
},
2424
"require": {
25-
"statamic/cms": "^3.4|^4.0|^5.0"
25+
"statamic/cms": "^5.0"
2626
},
2727
"require-dev": {
28-
"nunomaduro/collision": "^6.2",
29-
"orchestra/testbench": "^7.5",
30-
"pestphp/pest": "^1.0"
28+
"nunomaduro/collision": "^8.1",
29+
"orchestra/testbench": "^9.2",
30+
"pestphp/pest": "^2.0"
3131
},
3232
"config": {
3333
"allow-plugins": {

docs/data-formats.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
2-
title: Data Formats
2+
title: Formats
33
order: 7
44
---
55

6-
# Data Formats
6+
# Formats
77

88
[TOC]
99

1010
---
1111

12-
## Node Data
12+
## Nodes
1313

14-
Nodes mostly map to block elements in the rendered HTML. They're used for things like paragraphs, headings and lists. Node data looks like this:
14+
Nodes mostly map to block elements in the rendered HTML. They're used for things like paragraphs, headings and lists. Nodes look like this:
1515

1616
```yaml
1717
-
@@ -35,7 +35,7 @@ Each node is an object with the following properties:
3535
3636
---
3737
38-
## Mark Data
38+
## Marks
3939
4040
Marks mostly map to inline elements in the rendered HTML. They're used for things like links, bold, and italic text. Marks are not nested in the same way they are in HTML. Instead they're applied to a node in a flat list. For example, a bold link is a text node with marks that look like this:
4141

docs/examples.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Mutator::html('link', function ($value) {
4141
```php
4242
use JackSleight\StatamicBardMutator\Facades\Mutator;
4343

44-
Mutator::html('heading', function ($value, $data) {
45-
if ($data->attrs->level === 2) {
46-
$value[1]['id'] = str_slug(collect($data->content)->implode('text', ''));
44+
Mutator::html('heading', function ($value, $item) {
45+
if ($item->attrs->level === 2) {
46+
$value[1]['id'] = str_slug(collect($item->content)->implode('text', ''));
4747
}
4848
return $value;
4949
});
@@ -68,8 +68,8 @@ Mutator::html('table', function ($value) {
6868
```php
6969
use JackSleight\StatamicBardMutator\Facades\Mutator;
7070

71-
Mutator::html('listItem', function ($value, $meta) {
72-
if ($meta['parent']->type === 'bulletList') {
71+
Mutator::html('listItem', function ($value, $info) {
72+
if ($info->parent->type === 'bulletList') {
7373
$value[2] = ['span', [], 0];
7474
}
7575
return $value;
@@ -108,8 +108,8 @@ Mutator::html('image', function ($value) {
108108
```php
109109
use JackSleight\StatamicBardMutator\Facades\Mutator;
110110

111-
Mutator::html('paragraph', function ($value, $meta) {
112-
if (($meta['parent']->type ?? null) === 'listItem') {
111+
Mutator::html('paragraph', function ($value, $info) {
112+
if (($info->parent->type ?? null) === 'listItem') {
113113
return null;
114114
}
115115
return $value;
@@ -121,8 +121,8 @@ Mutator::html('paragraph', function ($value, $meta) {
121121
```php
122122
use JackSleight\StatamicBardMutator\Facades\Mutator;
123123

124-
Mutator::html('paragraph', function ($value, $data) {
125-
if (($data->content[0]->type ?? null) === 'image') {
124+
Mutator::html('paragraph', function ($value, $item) {
125+
if (($item->content[0]->type ?? null) === 'image') {
126126
return null;
127127
}
128128
return $value;
@@ -134,8 +134,8 @@ Mutator::html('paragraph', function ($value, $data) {
134134
```php
135135
use JackSleight\StatamicBardMutator\Facades\Mutator;
136136

137-
Mutator::html('heading', function ($value, $data) {
138-
$slug = str_slug(collect($data->content)->implode('text', ''));
137+
Mutator::html('heading', function ($value, $item) {
138+
$slug = str_slug(collect($item->content)->implode('text', ''));
139139
$value[2] = ['a', [
140140
'id' => $slug,
141141
'href' => '#'.$slug,
@@ -145,6 +145,21 @@ Mutator::html('heading', function ($value, $data) {
145145
});
146146
```
147147

148+
### Obfuscate email address link values
149+
150+
```php
151+
use JackSleight\StatamicBardMutator\Facades\Mutator;
152+
153+
Mutator::html('link', function ($value, $item) use (&$obfuscated) {
154+
if (Str::startsWith($item->attrs->href, 'mailto:')) {
155+
$obfuscated = Statamic::modify(Str::after($item->attrs->href, 'mailto:'))->obfuscateEmail();
156+
$value[1]['href'] = 'mailto:'.$obfuscated;
157+
}
158+
159+
return $value;
160+
});
161+
```
162+
148163
## Data Mutators
149164

150165
### Add permalink anchors before all heading text
@@ -153,15 +168,29 @@ Mutator::html('heading', function ($value, $data) {
153168
use JackSleight\StatamicBardMutator\Facades\Mutator;
154169
use JackSleight\StatamicBardMutator\Support\Data;
155170

156-
Mutator::data('heading', function ($data) {
157-
$slug = str_slug(collect($data->content)->implode('text', ''));
171+
Mutator::data('heading', function ($item) {
172+
$slug = str_slug(collect($item->content)->implode('text', ''));
158173
array_unshift(
159-
$data->content,
174+
$item->content,
160175
Data::html('<a id="'.$slug.'" href="#'.$slug.'">#</a>')
161176
);
162177
});
163178
```
164179

180+
### Convert blockquotes to figures with figcaptions
181+
182+
```php
183+
use JackSleight\StatamicBardMutator\Facades\Mutator;
184+
use JackSleight\StatamicBardMutator\Support\Data;
185+
186+
Mutator::data('blockquote', function ($data) {
187+
Data::morph($data, Data::html('figure', ['class' => 'quote'], [
188+
Data::clone($data, content: collect($data->content)->slice(0, -1)->values()->all()),
189+
Data::html('figcaption', [], [collect($data->content)->last()]),
190+
]));
191+
});
192+
```
193+
165194
Check out the modifier [example below](examples#using-with-the-bard-modifiers) to see how you could use these in a table of contents.
166195

167196
## Using with the Bard modifiers

docs/helpers.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ Creates a new mark object.
2525

2626
Creates a new text node object.
2727

28-
### `Data::html($html)`
28+
### `Data::html($html|$tag, $attrs = [], $content = [])`
2929

30-
Creates a new HTML node object.
30+
Creates a new HTML node object.
31+
32+
### `Data::apply($item, ...$properties)`
33+
34+
Apply new properties to an existing node/mark.
35+
36+
### `Data::clone($item, ...$properties)`
37+
38+
Clone an existing node/mark and optionally apply new properties to it.
39+
40+
### `Data::morph($item, $into)`
41+
42+
Morph an existing node/mark into a different node/mark.

docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ This Statamic addon allows you to modify the data and tags rendered by the Bard
1111

1212
This is how Tiptap (the magic behind Bard's content format and rendering) handles the rendering process, and how Bard Mutator interacts with that process:
1313

14-
1. The raw content is stored as a ProseMirror document in your entries, these documents consist of [nodes and marks](data-formats)
14+
1. The raw content is stored as a ProseMirror document in your entries, these documents consist of [nodes and marks](formats)
1515
2. Statamic's augmentation process passes this data to the Tiptap renderer
16-
3. **Bard Mutator's [data mutators](mutators#data-mutators) jump in here, allowing you to modify the raw data**
17-
4. Tiptap converts the raw node and mark data to their standard [tag values](data-formats#html-values)
18-
5. **Bard Mutator's [HTML mutators](mutators#html-mutators) jump in here, allowing you to modify the tag values**
16+
3. **Bard Mutator's [data plugins](plugins#data-plugins) jump in here, allowing you to modify the raw data**
17+
4. Tiptap converts the raw node and mark data to their standard [tag values](formats#html-values)
18+
5. **Bard Mutator's [HTML plugins](plugins#html-plugins) jump in here, allowing you to modify the tag values**
1919
6. Tiptap renders the tag values to an HTML string
2020

2121
## Compatibility

docs/installation.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,4 @@ You can search for this addon in the `Tools > Addons` section of the Statamic co
99

1010
```bash
1111
composer require jacksleight/statamic-bard-mutator
12-
```
13-
14-
## Enabling Advanced Features
15-
16-
Advanced features such as [data mutators](mutators#data-mutators) and [metadata](mutators#metadata) require deeper access to Tiptap's rendering process, which isn't avaliable by default. Bard Mutator includes an extended Tiptap Editor class that makes this access possible. To enable these features simply bind the Bard Mutator Editor class in your app service providers `register()` method:
17-
18-
```php
19-
$this->app->bind(
20-
\Tiptap\Editor::class,
21-
\JackSleight\StatamicBardMutator\Editor::class
22-
);
23-
```
24-
25-
If you don't want to use those features you don't need to do this, all other features work without it.
12+
```

docs/mutators.md

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)