@purescraps/purehtml 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -1,75 +1,7 @@
1
- # Configurable HTML to JSON converter
1
+ # PureHTML: Parsing specification for extracting JSON from HTML
2
2
 
3
- This tool aims to let you convert your HTMLs to structured JSONs.
3
+ This is the TypeScript/JavaScript implementation of the PureHTML specification.
4
4
 
5
- ## API
5
+ You need to install [NodeJS](https://nodejs.org) first, then follow the steps explained in the documentation.
6
6
 
7
- Using this module is as simple as:
8
-
9
- ```ts
10
- import { ConfigFactory, extract } from '@purescraps/purehtml';
11
-
12
- const yaml = '...';
13
- const html = '...';
14
- const config = ConfigFactory.fromYAML(yaml);
15
- const result = extract(html, config);
16
- ```
17
-
18
- ### `extract($: cheerio.Root | string, config: Config): any`
19
-
20
- You may pass either `cheerio.Root` or `string` (html contents) to `extract` function.
21
- If you wish, you can use the same `cheerio.Root` for multiple configs without need to
22
- call `load(html)` again and again.
23
-
24
- ## Configuration
25
-
26
- - `selector` allows to filter the current HTML element with a CSS selector query. **optional**.
27
- - `type` can be `string`, `object` and `array`. **optional**.
28
- if you set `properties`, then your `type` will be `object`.
29
- if you set `items`, then your `type` will be `array`
30
- - `type` might become `number`, `boolean`, `object` and `array` **implicitly** by the `transform` parameter.
31
- - `transform` can be either:
32
- - name of one of the core transformers. see `Core Transformers` section below.
33
- - [not available for YAML] an instance of a class that extends `Transformer` class.
34
- - array of above
35
- - `properties: { [key: string]: Config }` implicitly sets `type` to `object`.
36
- - `items: Config` defines the configuration that will be used to extract data for every match of the selector.
37
- - `union: ConfigWithSelector[]` extracts data for the first matching configuration only. Extracts `null` if
38
- none of the given configurations match. Can't be used with any other properties. `selector` IS required in union items.
39
- - `constant: any` always extracts the provided value. Can't be used with any other properties.
40
-
41
- ## Selectors
42
-
43
- [cheerio](https://npmjs.com/package/cheerio) is used for matching elements so the format you provided MUST be valid for cheerio. Except the **special selectors**.
44
-
45
- ### Special Selectors
46
-
47
- - **$self**
48
- Passes the same element down to the extraction process.
49
-
50
- ## Core Transformers
51
-
52
- 1. **length**:
53
- Input type: `string | array`
54
- Output type: `number`
55
-
56
- 2. **trim**:
57
- Input type: `string`
58
- Output type: `string`
59
-
60
- 3. **exists**:
61
- Input type: `$`
62
- Output type: `number`
63
-
64
- 4. **match-count**:
65
- Input type: `$`
66
- Output type: `string`
67
- Returns matched element count of your selector.
68
-
69
- 5. **exists**:
70
- Input type: `$`
71
- Output type: `boolean`
72
-
73
- 6. **html**:
74
- Input type: `$`
75
- Output type: `string`
7
+ See [https://purescraps.github.io/purehtml/](https://purescraps.github.io/purehtml/) for documentation and playground.
@@ -7505,7 +7505,9 @@ var ConfigFactory = class _ConfigFactory {
7505
7505
  transform
7506
7506
  );
7507
7507
  case "union":
7508
- return UnionConfig.generate(union.map((cfg) => this.generate(cfg)));
7508
+ return UnionConfig.generate(
7509
+ union.map((cfg) => this.generate(cfg))
7510
+ );
7509
7511
  default:
7510
7512
  return PrimitiveValueConfig.generate(selector, transform);
7511
7513
  }