@adguard/agtree 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -8,7 +8,33 @@ adheres to [Semantic Versioning][semver].
8
8
  [keepachangelog]: https://keepachangelog.com/en/1.0.0/
9
9
  [semver]: https://semver.org/spec/v2.0.0.html
10
10
 
11
- ## [Unreleased]
11
+ ## [1.1.0] - 2023-08-10
12
+
13
+ ### Added
14
+
15
+ - Compatibility tables for modifiers
16
+ - Validator for modifiers
17
+ - Basic rule converter
18
+ - New utils (regex, quotes)
19
+
20
+ ### Changed
21
+
22
+ - Updated dependencies
23
+ - Improved library build
24
+ - Improved CSSTree utils
25
+ - Export CSSTree utils
26
+ - Store raw data while parsing
27
+ - General code improvements
28
+
29
+ ### Fixed
30
+
31
+ - Package metadata
32
+ - Type import/export
33
+ - Modifier list parsing
34
+ - Scriptlet parsing
35
+ - Metadata parsing
36
+
37
+ ## [1.0.1] - 2023-05-24
12
38
 
13
39
  ### Added
14
40
 
package/README.md CHANGED
@@ -1,76 +1,72 @@
1
+ <!-- markdownlint-disable -->
2
+
1
3
  &nbsp;
2
4
 
3
5
  <p align="center">
4
- <picture>
5
- <source
6
- media="(prefers-color-scheme: dark)"
7
- srcset="
8
- https://cdn.adtidy.org/website/github.com/AGTree/agtree_darkmode.svg
9
- "
10
- />
11
- <img
12
- alt="AGTree"
13
- src="https://cdn.adtidy.org/website/github.com/AGTree/agtree_lightmode.svg"
14
- width="350px"
15
- />
16
- </picture>
6
+ <picture>
7
+ <source
8
+ media="(prefers-color-scheme: dark)"
9
+ srcset="https://cdn.adtidy.org/website/github.com/AGTree/agtree_darkmode.svg"
10
+ />
11
+ <img
12
+ alt="AGTree"
13
+ src="https://cdn.adtidy.org/website/github.com/AGTree/agtree_lightmode.svg"
14
+ width="350px"
15
+ />
16
+ </picture>
17
17
  </p>
18
18
  <h3 align="center">Universal adblock filter list parser</h3>
19
19
  <p align="center">Supported syntaxes:</p>
20
20
  <p align="center">
21
- <a href="https://adguard.com"
22
- ><img
23
- src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg"
24
- width="14px"
25
- />
26
- AdGuard</a
27
- >
28
- |
29
- <a href="https://github.com/gorhill/uBlock"
30
- ><img
31
- src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg"
32
- width="14px"
33
- />
34
- uBlock Origin</a
35
- >
36
- |
37
- <a href="https://getadblock.com"
38
- ><img
39
- src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg"
40
- width="14px"
41
- />
42
- AdBlock</a
43
- >
44
- |
45
- <a href="https://adblockplus.org"
46
- ><img
47
- src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg"
48
- width="14px"
49
- />
50
- Adblock Plus</a
51
- >
21
+ <a href="https://adguard.com">
22
+ <img
23
+ src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg"
24
+ width="14px"
25
+ />
26
+ AdGuard
27
+ </a>
28
+ |
29
+ <a href="https://github.com/gorhill/uBlock">
30
+ <img
31
+ src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg"
32
+ width="14px"
33
+ />
34
+ uBlock Origin
35
+ </a>
36
+ |
37
+ <a href="https://getadblock.com">
38
+ <img
39
+ src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg"
40
+ width="14px"
41
+ />
42
+ AdBlock
43
+ </a>
44
+ |
45
+ <a href="https://adblockplus.org">
46
+ <img
47
+ src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg"
48
+ width="14px"
49
+ />
50
+ Adblock Plus
51
+ </a>
52
52
  </p>
53
53
 
54
54
  <p align="center">
55
- <a href="https://www.npmjs.com/package/@adguard/agtree"
56
- ><img src="https://img.shields.io/npm/v/@adguard/agtree" alt="NPM version"
57
- /></a>
58
- <a href="https://www.npmjs.com/package/@adguard/agtree"
59
- ><img
60
- src="https://img.shields.io/npm/dm/@adguard/agtree"
61
- alt="NPM Downloads"
62
- /></a>
63
- <a href="https://github.com/AdguardTeam/AGTree/blob/master/LICENSE"
64
- ><img src="https://img.shields.io/npm/l/@adguard/agtree" alt="License"
65
- /></a>
55
+ <a href="https://www.npmjs.com/package/@adguard/agtree">
56
+ <img src="https://img.shields.io/npm/v/@adguard/agtree" alt="NPM version" />
57
+ </a>
58
+ <a href="https://www.npmjs.com/package/@adguard/agtree">
59
+ <img src="https://img.shields.io/npm/dm/@adguard/agtree" alt="NPM Downloads" />
60
+ </a>
61
+ <a href="https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/agtree/LICENSE">
62
+ <img src="https://img.shields.io/npm/l/@adguard/agtree" alt="License" />
63
+ </a>
66
64
  </p>
65
+ <!-- markdownlint-restore -->
67
66
 
68
67
  Table of Contents:
69
68
 
70
69
  - [Introduction](#introduction)
71
- - [Usage](#usage)
72
- - [Parsing rules](#parsing-rules)
73
- - [Parsing filter lists](#parsing-filter-lists)
74
70
  - [Development \& Contribution](#development--contribution)
75
71
  - [Ideas \& Questions](#ideas--questions)
76
72
  - [License](#license)
@@ -78,249 +74,74 @@ Table of Contents:
78
74
 
79
75
  ## Introduction
80
76
 
81
- AGTree is a universal adblock filter list parser. It supports all syntaxes
82
- currently in use: AdGuard, uBlock Origin and AdBlock / Adblock Plus.
83
-
84
- ## Usage
85
-
86
- AGTree provides a powerful, error-tolerant parser for all kinds of ad blocking
87
- rules. It fully supports AdGuard, uBlock Origin and Adblock Plus syntaxes, and
88
- provides a high-detail AST for all rules.
89
-
90
- Basically, the parser API has two main parts:
91
-
92
- - Parser: parsing rules (string &#8594; AST)
93
- - Generator: serialization of ASTs (AST &#8594; string)
94
-
95
- ### Parsing rules
96
-
97
- You can parse individual rules using the `RuleParser` class. It has a `parse`
98
- method that takes a rule string and returns an AST. For example, this code:
99
-
100
- ```typescript
101
- import { RuleParser } from "@adguard/agtree";
102
-
103
- // RuleParser automatically determines the rule type
104
- const ast = RuleParser.parse("/ads.js^$script");
105
- ```
106
-
107
- will gives you this AST:
108
-
109
- ```json
110
- {
111
- "type": "NetworkRule",
112
- "category": "Network",
113
- "syntax": "Common",
114
- "exception": false,
115
- "pattern": {
116
- "type": "Value",
117
- "value": "/ads.js^"
118
- },
119
- "modifiers": {
120
- "type": "ModifierList",
121
- "children": [
122
- {
123
- "type": "Modifier",
124
- "modifier": {
125
- "type": "Value",
126
- "value": "script"
127
- },
128
- "exception": false
129
- }
130
- ]
131
- }
132
- }
133
- ```
134
-
135
- <details>
136
-
137
- <summary>Show full AST (with locations)</summary>
138
-
139
- ```json
140
- {
141
- "type": "NetworkRule",
142
- "loc": {
143
- "start": {
144
- "offset": 0,
145
- "line": 1,
146
- "column": 1
147
- },
148
- "end": {
149
- "offset": 15,
150
- "line": 1,
151
- "column": 16
152
- }
153
- },
154
- "raws": {
155
- "text": "/ads.js^$script"
156
- },
157
- "category": "Network",
158
- "syntax": "Common",
159
- "exception": false,
160
- "pattern": {
161
- "type": "Value",
162
- "loc": {
163
- "start": {
164
- "offset": 0,
165
- "line": 1,
166
- "column": 1
167
- },
168
- "end": {
169
- "offset": 8,
170
- "line": 1,
171
- "column": 9
172
- }
173
- },
174
- "value": "/ads.js^"
175
- },
176
- "modifiers": {
177
- "type": "ModifierList",
178
- "loc": {
179
- "start": {
180
- "offset": 9,
181
- "line": 1,
182
- "column": 10
183
- },
184
- "end": {
185
- "offset": 15,
186
- "line": 1,
187
- "column": 16
188
- }
189
- },
190
- "children": [
191
- {
192
- "type": "Modifier",
193
- "loc": {
194
- "start": {
195
- "offset": 9,
196
- "line": 1,
197
- "column": 10
198
- },
199
- "end": {
200
- "offset": 15,
201
- "line": 1,
202
- "column": 16
203
- }
204
- },
205
- "modifier": {
206
- "type": "Value",
207
- "loc": {
208
- "start": {
209
- "offset": 9,
210
- "line": 1,
211
- "column": 10
212
- },
213
- "end": {
214
- "offset": 15,
215
- "line": 1,
216
- "column": 16
217
- }
218
- },
219
- "value": "script"
220
- },
221
- "exception": false
222
- }
223
- ]
224
- }
225
- }
226
- ```
77
+ AGTree is a universal tool for working with adblock filter lists. It contains
78
+ the following modules:
227
79
 
228
- </details>
80
+ - [Adblock rule converter][converter-url]
81
+ - [Adblock rule parser][parser-url]
82
+ - [Compatibility tables][compatibility-tables-url]
229
83
 
230
- As you can see, this AST is very detailed and contains all the information about
231
- the rule: syntax, category, exception, modifiers, node locations, and so on.
232
- Locations are especially useful for linters, since they allow you to point to
233
- the exact place in the rule where the error occurred.
84
+ AGTree supports all syntaxes currently in use:
234
85
 
235
- And this code:
86
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard][adg-url]
87
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin][ubo-url]
88
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus][abp-url]
89
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg" width="14px"> [AdBlock][ab-url]
236
90
 
237
- ```typescript
238
- RuleParser.generate(ast);
239
- ```
240
-
241
- will generate the adblock rule from the AST (serialization):
242
-
243
- ```adblock
244
- /ads.js^$script
245
- ```
246
-
247
- Please keep in mind that the parser omits unnecessary whitespaces, so the
248
- generated rule may not match with the original rule character by character.
249
- Only the formatting can change, the rule itself remains the same. You can
250
- pass any rule to the parser, it automatically determines the type and category
251
- of the rule. If the rule is syntactically incorrect, the parser will throw an
252
- error.
253
-
254
- ### Parsing filter lists
255
-
256
- You can also parse complete filter lists using the `FilterListParser` class.
257
- It works the same way as the `RuleParser` class. Here is an example of parsing
258
- [EasyList](https://easylist.to/easylist/easylist.txt) and generating it back:
259
-
260
- ```typescript
261
- import { FilterListParser } from "@adguard/agtree";
262
- import { writeFile } from "fs/promises";
263
- // Requires installing "node-fetch" package
264
- import fetch from "node-fetch";
265
-
266
- // Download EasyList
267
- const easyList = await (
268
- await fetch("https://easylist.to/easylist/easylist.txt")
269
- ).text();
270
-
271
- // Or read it from file
272
- // const easyList = await readFile('easylist.txt', 'utf-8');
273
-
274
- // Parse EasyList to AST. By default, parser is very tolerant,
275
- // if it can't parse some rules, it will just mark them as "raw".
276
- // If you want to disable this behavior, you can pass the second
277
- // argument as "false" to the "parse" method, like this:
278
- // const ast = FilterListParser.parse(easyList, false);
279
- const ast = FilterListParser.parse(easyList);
280
-
281
- // Generate filter list from filter list AST
282
- const easyListGenerated = FilterListParser.generate(ast);
283
-
284
- // Write generated filter list to file
285
- await writeFile("easylist-generated.txt", easyListGenerated);
286
- ```
91
+ [ab-url]: https://getadblock.com
92
+ [abp-url]: https://adblockplus.org
93
+ [adg-url]: https://adguard.com
94
+ [compatibility-tables-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/compatibility-tables
95
+ [converter-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/converter
96
+ [parser-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/src/parser
97
+ [ubo-url]: https://github.com/gorhill/uBlock
287
98
 
288
99
  ## Development & Contribution
289
100
 
290
- Please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for details on how to
101
+ Please read the [CONTRIBUTING.md][contributing-url] file for details on how to
291
102
  contribute to this project.
292
103
 
104
+ [contributing-url]: https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree/CONTRIBUTING.md
105
+
293
106
  ## Ideas & Questions
294
107
 
295
- If you have any questions or ideas for new features, please open an issue or a
296
- discussion. We will be happy to discuss it with you.
108
+ If you have any questions or ideas for new features, please
109
+ [open an issue][new-issue-url] or a [discussion][discussions-url]. We will be
110
+ happy to discuss it with you.
111
+
112
+ [discussions-url]: https://github.com/AdguardTeam/tsurlfilter/discussions
113
+ [new-issue-url]: https://github.com/AdguardTeam/tsurlfilter/issues/new
297
114
 
298
115
  ## License
299
116
 
300
- AGTree is licensed under the MIT License. See the [LICENSE](LICENSE) file for
301
- details.
117
+ AGTree is licensed under the MIT License. See the [LICENSE][license-url] file
118
+ for details.
119
+
120
+ [license-url]: https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/agtree/LICENSE
302
121
 
303
122
  ## References
304
123
 
305
124
  Here are some useful links to help you write adblock rules. This list is not
306
125
  exhaustive, so if you know any other useful resources, please let us know.
307
126
 
127
+ <!--markdownlint-disable MD013-->
308
128
  - Syntax documentation:
309
- - [AdGuard: _How to create your own ad filters_][adg-filters]
310
- - [uBlock Origin: _Static filter syntax_][ubo-filters]
311
- - [Adblock Plus: _How to write filters_][abp-filters]
129
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard: *How to create your own ad filters*][adg-filters]
130
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin: *Static filter syntax*][ubo-filters]
131
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus: *How to write filters*][abp-filters]
312
132
  - Extended CSS documentation:
313
- - [MDN: _CSS selectors_][mdn-css-selectors]
314
- - [AdGuard: _Extended CSS capabilities_][adg-ext-css]
315
- - [uBlock Origin: _Procedural cosmetic filters_][ubo-procedural]
316
- - [Adblock Plus: _Extended CSS selectors_][abp-ext-css]
133
+ - [MDN: *CSS selectors*][mdn-css-selectors]
134
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard: *Extended CSS capabilities*][adg-ext-css]
135
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin: *Procedural cosmetic filters*][ubo-procedural]
136
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus: *Extended CSS selectors*][abp-ext-css]
317
137
  - Scriptlets:
318
- - [AdGuard scriptlets][adg-scriptlets]
319
- - [uBlock Origin scriptlets][ubo-scriptlets]
320
- - [Adblock Plus snippets][abp-snippets]
138
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard scriptlets][adg-scriptlets]
139
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg" width="14px"> [uBlock Origin scriptlets][ubo-scriptlets]
140
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg" width="14px"> [Adblock Plus snippets][abp-snippets]
321
141
  - Third party libraries:
322
- - [CSSTree docs][css-tree-docs]
323
- - [AdGuard's compatibility table][adg-compatibility-table]
142
+ - <img src="https://raw.githubusercontent.com/csstree/csstree/master/assets/csstree-logo-rounded.svg" width="14px"> [CSSTree docs][css-tree-docs]
143
+ - <img src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg" width="14px"> [AdGuard's compatibility table][adg-compatibility-table]
144
+ <!--markdownlint-enable MD013-->
324
145
 
325
146
  [abp-ext-css]: https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide-emulation
326
147
  [abp-filters]: https://help.eyeo.com/adblockplus/how-to-write-filters