Skip to content

Commit f97d959

Browse files
committed
Improve examples
1 parent d19bad9 commit f97d959

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

content/terms/reference/filters.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Can be used as follows in the declaration:
4949
}
5050
```
5151

52-
Example:
52+
#### Example
5353

5454
```js
5555
export function convertTimeAgoToDate(document) {
@@ -114,13 +114,15 @@ Can be used as follows in the declaration:
114114
}
115115
```
116116

117-
Example:
117+
#### Example 1
118118

119119
```js
120-
export function removeLinksWithText(document, text) {
120+
export function removeLinksWithText(document, textArray) {
121121
const links = document.querySelectorAll('a');
122+
const textsToRemove = Array.isArray(textArray) ? textArray : [textArray];
123+
122124
links.forEach(link => {
123-
if (link.textContent.trim() === text) {
125+
if (textsToRemove.includes(link.textContent.trim())) {
124126
link.remove();
125127
}
126128
});
@@ -135,8 +137,7 @@ export function removeLinksWithText(document, text) {
135137
"fetch": "https://example.com/privacy",
136138
"select": ".content",
137139
"filter": [
138-
{ "removeLinksWithText": "Return to previous section" }
139-
{ "removeLinksWithText": "Go to next section" }
140+
{ "removeLinksWithText": ["Return to previous section", "Go to next section"] }
140141
]
141142
}
142143
}
@@ -160,3 +161,51 @@ Result:
160161
<p>...</p>
161162
</div>
162163
```
164+
165+
#### Example 2
166+
167+
```js
168+
import fetch from 'isomorphic-fetch';
169+
170+
export async function convertImagesToBase64(document, selector, documentDeclaration) {
171+
const images = Array.from(document.querySelectorAll(`selector`));
172+
173+
return Promise.all(images.map(async ({ src }, index) => {
174+
if (src.startsWith('data:')) {
175+
return; // Already a data-URI, skip
176+
}
177+
178+
const imageUrl = new URL(src, documentDeclaration.fetch).href; // Ensure url is absolute
179+
const response = await fetch(imageUrl);
180+
const mimeType = response.headers.get('content-type');
181+
const content = await response.arrayBuffer();
182+
183+
const base64Content = btoa(String.fromCharCode(...new Uint8Array(content)));
184+
185+
images[index].src = `data:${mimeType};base64,${base64Content}`;
186+
}));
187+
188+
}
189+
```
190+
191+
```json
192+
{
193+
"name": "MyService",
194+
"terms": {
195+
"Privacy Policy": {
196+
"fetch": "https://example.com/privacy",
197+
"select": ".content",
198+
"filter": [
199+
{ "convertImagesToBase64": ".meaningful-illustration" }
200+
]
201+
}
202+
}
203+
}
204+
```
205+
206+
Result:
207+
208+
```diff
209+
- <img src="https://example.com/image.png" class="meaningful-illustration">
210+
+ <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..." class="meaningful-illustration">
211+
```

0 commit comments

Comments
 (0)