Skip to content

Commit f825bf8

Browse files
committed
Improve filters documentation
1 parent 4bd20bc commit f825bf8

File tree

4 files changed

+54
-53
lines changed

4 files changed

+54
-53
lines changed

content/terms/explanation/filters.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,34 @@ weight: 3
55

66
# Filters
77

8-
Filters solve [noise]({{< relref "/terms/guideline/declaring/#usual-noise" >}}) issues in terms versions that cannot be addressed with direct selection or removal of content using CSS selectors or range selectors.
8+
Filters enable solving [noise]({{< relref "/terms/guideline/declaring/#usual-noise" >}}) issues in versions that cannot be addressed with direct selection or removal of content using selectors.
99

1010
## When filters are needed
1111

12-
Filters are necessary when standard CSS selectors and range selectors cannot adequately address noise in terms versions. They provide a solution for complex content manipulation that goes beyond simple selection and removal.
13-
1412
Use filters when:
1513

16-
- **CSS selectors are insufficient**, for example when noise appears within content that can't be targeted with selectors or [range selectors]({{< relref "terms/explanation/range-selectors" >}}) with the [`select`]({{< relref "terms/reference/declaration/#ref-select" >}}) and [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) properties.
17-
- **Content is dynamically generated**, for example when elements change on each page load with tracking parameters in URLs (like `utm_source`, `utm_medium`) or dynamic elements with changing classes or IDs.
18-
- **Complex tasks are needed**, for example when content transformation is required such as converting images to base64 to store them in the terms version or converting date-based content to a more stable format (like "Updated X days ago" to "Last updated on YYYY-MM-DD").
14+
- **Content selectors are insufficient**, for example when noise appears within content that can't be targeted with CSS selectors or [range selectors]({{< relref "terms/explanation/range-selectors" >}}) with the [`select`]({{< relref "terms/reference/declaration/#ref-select" >}}) and [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) properties.
15+
- **Content is dynamically generated**, for example when elements change on each page load with changing classes or IDs that cannot be targeted with [attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
16+
- **Complex tasks are needed**, for example when content transformation is required such as converting images to base64 to store them in the terms version or converting date-based content to a stable format (like Updated X days ago to Last updated on YYYY-MM-DD).
1917

2018
## How filters work
2119

22-
Filters are JavaScript functions that receive a JSDOM document instance and can manipulate the DOM structure directly. They modify the document structure and content in-place and they run sequentially in the order specified in the declaration.
20+
Filters are JavaScript functions that can manipulate the DOM structure directly. They modify the document structure and content in-place.
2321

2422
## Filter design principles
2523

26-
When designing filters, follow these core principles:
24+
Filters should follow these core principles:
25+
26+
- **Specific**: target only the noise to remove. Avoid broad selectors that might accidentally remove important content.
2727

28-
- **Be specific**: target only the noise you want to remove. Avoid broad selectors that might accidentally remove important content.
28+
> For example, if a filter converts relative dates to absolute dates, make sure to scope the targeted dates. This might translate to selecting with `.metadata time`, not `time`, which might also affect important effective dates within the terms content.
2929
30-
> For example, if your filter converts relative dates to absolute dates, use `.metadata time` not `time` which might also affect important effective dates within the terms content.
30+
- **Idempotent**: filters should produce the same result even if run multiple times on their own output. This ensures consistency.
3131

32-
- **Be idempotent**: filters should produce the same result even if run multiple times on their own output. This ensures consistency and prevents unexpected behavior.
32+
> For example, if a filter adds section numbers like "1." to headings, it should check if the numbers already exist, to prevent "1. Privacy Policy" from becoming "1. 1. Privacy Policy" on repeated runs.
3333
34-
> For example, if your filter adds section numbers like "1." to headings, check if numbers already exist to prevent "1. Privacy Policy" from becoming "1. 1. Privacy Policy" on repeated runs.
34+
- **Efficient**: DOM queries should be optimised and filters should avoid unnecessary operations, processing only the elements needed.
3535

36-
- **Be efficient**: use efficient DOM queries and avoid unnecessary operations. Process only the elements you need to modify.
37-
38-
> For example, if your filter updates timestamp elements with a specific class, use `document.querySelector('.timestamp')` instead of `document.querySelectorAll('*')` followed by filtering for timestamp elements.
36+
> For example, if a filter updates timestamp elements with a specific class, using `document.querySelectorAll('.timestamp')` is more efficient than `document.querySelectorAll('*')` followed by filtering for timestamp elements.
3937
40-
- **Be safe**: filters should not accidentally remove important content. The generated version should always be checked after adding a filter to ensure it still contains the whole terms content.
38+
- **Safe**: filters must not accidentally remove important content. The generated version should always be checked after adding a filter to ensure it still contains the whole terms content.

content/terms/how-to/apply-filters.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,47 @@ weight: 7
55

66
# How to apply filters
77

8-
This guide explains how to apply filters to existing declarations to remove meaningless content that changes on each page load or that cannot be removed with CSS selectors to avoid noise in the terms changes history.
8+
This guide explains how to add filters to existing declarations to remove meaningless content that cannot be removed with CSS selectors, to prevent noise in the versions.
99

1010
## Prerequisites
1111

12-
- An existing terms declaration file
13-
- Identified the noise you want to remove and ensure it cannot be removed with CSS selectors with the [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) property.
12+
- An existing terms declaration file.
13+
- Having already identified the noise to remove and having double-checked it cannot be removed with CSS selectors with the [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) property.
1414

1515
## Step 1: Check for built-in filters
1616

17-
Built-in filters are pre-defined functions that handle common noise patterns. They're the easiest way to clean up content without writing custom code.
17+
Built-in filters are pre-defined functions that handle common noise patterns. They are the easiest way to clean up content.
1818

1919
Review the available [built-in filters]({{< relref "/terms/reference/built-in-filters" >}}) to find if one matches your needs.
2020

21-
If you find a suitable built-in filter, proceed to [Step 2](#step-2-declare-the-filter), otherwise you will need to create a custom filter.
21+
If you find a suitable built-in filter, proceed to [Step 3](#step-3-declare-the-filter), otherwise you will need to create a custom filter.
2222

23-
### Create a custom filter (optional)
23+
## Step 2: Create a custom filter _(optional)_
2424

25-
If no built-in filter matches your needs, you'll need to create a custom filter. This requires JavaScript knowledge and familiarity with DOM manipulation.
25+
If no built-in filter matches your needs, you will need to create a custom filter. This requires JavaScript knowledge and familiarity with DOM manipulation.
2626

27-
#### Create the filter file
27+
### Create the filter file
2828

29-
Create a JavaScript file with the same name as your service declaration but with `.filters.js` extension. For example, if your declaration is `declarations/MyService.json`, create `declarations/MyService.filters.js`.
29+
Create a JavaScript file in the same folder and with the same name as your service declaration, but with `.filters.js` extension.
3030

31-
#### Write the filter function
31+
> For example, if your declaration is `declarations/MyService.json`, create `declarations/MyService.filters.js`.
3232
33-
Define your filter function following this signature:
33+
### Write the filter function
34+
35+
Define your filter function with the following signature:
3436

3537
```js
3638
export function myCustomFilter(document, [parameters]) {
3739
// Your filter logic here
3840
}
3941
```
4042

41-
**Parameters:**
43+
#### Parameters
4244

4345
- `document`: JSDOM document instance representing the web page
44-
- `parameters`: Values passed from the declaration (optional)
46+
- `parameters`: values passed from the declaration _(optional)_
4547

46-
**Example: Remove session IDs from text content**
48+
#### Example: Remove session IDs from text content
4749

4850
For example, let's say you want to remove session IDs from text content:
4951

@@ -61,7 +63,7 @@ You can implement this filter as follows:
6163
```js
6264
export function removeSessionIds(document) {
6365
// Find all paragraphs that might contain session IDs
64-
const paragraphs = document.querySelectorAll('p.session-id');
66+
const paragraphs = document.querySelectorAll('.session-id');
6567

6668
paragraphs.forEach(paragraph => {
6769
let text = paragraph.textContent;
@@ -84,20 +86,20 @@ Result after applying the filter:
8486
+ <p class="session-id">Last updated on 2023-12-07</p>
8587
```
8688

87-
## Step 2: Declare the filter
89+
## Step 3: Declare the filter
8890

89-
Open your service declaration file (e.g., `declarations/MyService.json`) and locate the `filter` property of the specific terms you want to apply the filter to. If it doesn't exist, add it as an array.
91+
Open your service declaration file (e.g. `declarations/MyService.json`) and locate the `filter` property of the specific terms you want to apply the filter to. If it doesn't exist, add it as an array.
9092

9193
### Filter without parameters
9294

93-
For filters that don't require parameters, add the filter name as a string:
95+
For filters that dont require parameters, add the filter name as a string:
9496

9597
```json
9698
{
9799
"name": "MyService",
98100
"terms": {
99101
"Privacy Policy": {
100-
"fetch": "https://my.service.com/en/privacy-policy",
102+
"fetch": "https://my.service.example/en/privacy-policy",
101103
"select": ".textcontent",
102104
"filter": [
103105
"removeSessionIds"
@@ -107,16 +109,16 @@ For filters that don't require parameters, add the filter name as a string:
107109
}
108110
```
109111

110-
### Parameterized filter
112+
### Filter with parameters
111113

112-
For filters that require parameters, use an object format, for example with the built-in filter `removeQueryParams` to remove query parameters from URLs:
114+
For filters that take parameters, use an object format, for example with the built-in filter `removeQueryParams` to remove query parameters from URLs:
113115

114116
```json
115117
{
116118
"name": "MyService",
117119
"terms": {
118120
"Privacy Policy": {
119-
"fetch": "https://my.service.com/en/privacy-policy",
121+
"fetch": "https://my.service.example/en/privacy-policy",
120122
"select": ".textcontent",
121123
"filter": [
122124
{
@@ -137,7 +139,7 @@ You can combine multiple filters in the same declaration:
137139
"name": "MyService",
138140
"terms": {
139141
"Privacy Policy": {
140-
"fetch": "https://my.service.com/en/privacy-policy",
142+
"fetch": "https://my.service.example/en/privacy-policy",
141143
"select": ".textcontent",
142144
"filter": [
143145
{
@@ -150,7 +152,7 @@ You can combine multiple filters in the same declaration:
150152
}
151153
```
152154

153-
## Step 3: Test the filter
155+
## Step 4: Test the filter
154156

155157
After adding the filter, test your declaration to ensure it works correctly:
156158

content/terms/reference/built-in-filters.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ title: "Built-in filters"
44

55
# Built-in filters
66

7-
This reference documentation details all available built-in filters that can be used to avoid noise in the terms content.
8-
9-
## Filters
7+
This reference details all available built-in [filters]({{< relref "terms/explanation/filters" >}}) that can be applied to avoid noise in versions.
108

119
{{< refItem
1210
name="removeQueryParams"
13-
description="Removes specified query parameters from URLs in links and images within the terms content"
11+
description="Removes specified query parameters from URLs in links and images."
1412
>}}
1513
1614
```json
@@ -21,8 +19,6 @@ This reference documentation details all available built-in filters that can be
2119
]
2220
```
2321

24-
Result:
25-
2622
```diff
2723
- <p>Read the <a href="https://example.com/example-page?utm_source=OGB&utm_medium=website&lang=en">list of our affiliates</a>.</p>
2824
+ <p>Read the <a href="https://example.com/example-page?lang=en">list of our affiliates</a>.</p>

content/terms/reference/filters.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ Filters are JavaScript functions that take the document DOM as parameter and are
88

99
- **in-place**: they modify the document structure and content directly;
1010
- **idempotent**: they return the same document structure and content even if run repeatedly on their own result.
11+
- **ordered**: they are run sequentially in the order specified in the declaration.
12+
13+
Learn more about the concept and constraints on the [filters explanation]({{< relref "terms/explanation/filters" >}}).
14+
15+
## Signature
1116

1217
The generic function signature for a filter is:
1318

14-
- For simple filters:
19+
- For filters that take no parameter:
1520

1621
```js
1722
export [async] function filterName(document, [documentDeclaration])
1823
```
1924

20-
- For filters with parameters:
25+
- For filters that take parameters:
2126

2227
```js
2328
export [async] function filterName(document, parameters, [documentDeclaration])
@@ -29,7 +34,7 @@ These functions can be `async`, but they will still run sequentially.
2934

3035
## Usage
3136

32-
### Simple filter
37+
### Filters that take no parameter
3338

3439
```js
3540
// <service name>.filters.js
@@ -75,7 +80,7 @@ export function convertTimeAgoToDate(document) {
7580
"name": "MyService",
7681
"terms": {
7782
"Privacy Policy": {
78-
"fetch": "https://example.com/privacy",
83+
"fetch": "https://my.service.example/privacy",
7984
"select": ".content",
8085
"filter": [
8186
"convertTimeAgoToDate"
@@ -141,7 +146,7 @@ export function removeLinksWithText(document, textArray) {
141146
"name": "MyService",
142147
"terms": {
143148
"Privacy Policy": {
144-
"fetch": "https://example.com/privacy",
149+
"fetch": "https://my.service.example/privacy",
145150
"select": ".content",
146151
"filter": [
147152
{ "removeLinksWithText": ["Return to previous section", "Go to next section"] }
@@ -200,7 +205,7 @@ export async function convertImagesToBase64(document, selector, documentDeclarat
200205
"name": "MyService",
201206
"terms": {
202207
"Privacy Policy": {
203-
"fetch": "https://example.com/privacy",
208+
"fetch": "https://my.service.example/privacy",
204209
"select": ".content",
205210
"filter": [
206211
{ "convertImagesToBase64": ".meaningful-illustration" }
@@ -213,6 +218,6 @@ export async function convertImagesToBase64(document, selector, documentDeclarat
213218
Result:
214219

215220
```diff
216-
- <img src="https://example.com/image.png" class="meaningful-illustration">
221+
- <img src="https://my.service.example/image.png" class="meaningful-illustration">
217222
+ <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..." class="meaningful-illustration">
218223
```

0 commit comments

Comments
 (0)