Skip to content

Commit 75acc97

Browse files
committed
Improve filter doc
1 parent f97d959 commit 75acc97

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

content/terms/explanation/filters.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ weight: 3
77

88
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.
99

10-
## Why filters are needed
10+
## When filters are needed
1111

12-
Web pages often contain dynamically generated content or content that cannot be targeted with CSS selectors that creates noise in the recorded version, for example:
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.
1313

14-
- Tracking parameters in URLs, for example `utm_source`, `utm_medium`, …
15-
- Content that are date based and can change between visits, for example "Updated X days ago" can be converted to a "Last updated on YYYY-MM-DD".
16-
- Dynamic elements with changing classes or IDs
14+
Use filters when:
1715

18-
Without filters, this dynamic content creates changes that are not meaningful to the terms.
16+
- **CSS selectors are insufficient**: 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**: When elements change on each page load, such as:
18+
- Tracking parameters in URLs (e.g., `utm_source`, `utm_medium`)
19+
- Dynamic elements with changing classes or IDs
20+
- **Complex tasks are needed**: When content transformation is needed such as:
21+
- Converting images to base64 to store them in the terms version.
22+
- Converting date-based content to a more stable format (e.g., "Updated X days ago" to "Last updated on YYYY-MM-DD")
1923

2024
## How filters work
2125

@@ -25,15 +29,16 @@ Filters are JavaScript functions that receive a JSDOM document instance and can
2529

2630
When designing filters, follow these core principles:
2731

28-
- **Be specific**: Target only the noise you want to remove. Avoid broad selectors that might accidentally remove important content.
29-
- **Be safe**: Ensure your filter doesn't accidentally remove important content. Always check that the generated version still contains the whole terms content.
30-
- **Be idempotent**: Your filter should produce the same result even if run multiple times on its own output. This ensures consistency and prevents unexpected behavior.
31-
- **Be efficient**: Use efficient DOM queries and avoid unnecessary operations. Process only the elements you need to modify.
32+
- **Be specific**: Target only the noise you want to remove. Avoid broad selectors that might accidentally remove important content.
3233

33-
## When to use filters
34+
> 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.
3435
35-
Use filters when:
36+
- **Be idempotent**: Filters should produce the same result even if run multiple times on their own output. This ensures consistency and prevents unexpected behavior.
3637

37-
- **CSS selectors are insufficient**: 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.
38-
- **Meaningful content is dynamic**: When elements change on each page load, for example "Updated X days ago" can be converted to a "Last updated on YYYY-MM-DD".
39-
- **Patterns are complex**: When simple removal isn't possible, for example removing all the tracking parameters in URLs.
38+
> 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.
39+
40+
- **Be efficient**: Use efficient DOM queries and avoid unnecessary operations. Process only the elements you need to modify.
41+
42+
> 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.
43+
44+
- **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.

content/terms/reference/filters.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ Filters are JavaScript functions that take the document DOM as parameter and are
1111

1212
The generic function signature for a filter is:
1313

14+
- For simple filters:
15+
16+
```js
17+
export [async] function filterName(document, [documentDeclaration])
18+
```
19+
20+
- For filters with parameters:
21+
1422
```js
15-
export [async] function filterName(document, [parameters])
23+
export [async] function filterName(document, parameters, [documentDeclaration])
1624
```
1725

18-
Each filter is exposed as a named function export that takes a `document` parameter and behaves like the `document` object in a browser DOM.
19-
> The `document` parameter is actually a [JSDOM](https://github.com/jsdom/jsdom) document instance.
26+
Each filter is exposed as a named function export that takes a `document` parameter and behaves like the `document` object in a browser DOM. The `document` parameter is actually a [JSDOM](https://github.com/jsdom/jsdom) document instance.
2027

2128
These functions can be `async`, but they will still run sequentially.
2229

@@ -53,7 +60,7 @@ Can be used as follows in the declaration:
5360

5461
```js
5562
export function convertTimeAgoToDate(document) {
56-
const timeElements = document.querySelectorAll('time');
63+
const timeElements = document.querySelectorAll('.metadata time');
5764

5865
timeElements.forEach(timeElement => {
5966
const dateTimeValue = timeElement.getAttribute('datetime');
@@ -81,8 +88,8 @@ export function convertTimeAgoToDate(document) {
8188
Result:
8289

8390
```diff
84-
- <p class="meta-data">Last update: <time datetime="2025-06-23T11:16:36Z" title="06/23/2025, 13:16" data-datetime="relative">2 months ago</time></p>
85-
+ <p class="meta-data">Last update: 2025-06-23T11:16:36Z</p>
91+
- <p class="metadata">Last update: <time datetime="2025-06-23T11:16:36Z" title="06/23/2025, 13:16" data-datetime="relative">2 months ago</time></p>
92+
+ <p class="metadata">Last update: 2025-06-23T11:16:36Z</p>
8693
```
8794

8895
### Filter with parameters

0 commit comments

Comments
 (0)