@purescraps/purehtml 0.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/.eslintrc.json +21 -0
- package/.prettierrc +3 -0
- package/README.md +75 -0
- package/config.yaml +0 -0
- package/dist/examples/simple.d.ts +2 -0
- package/dist/examples/simple.js +7586 -0
- package/dist/examples/simple.js.map +1 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +7586 -0
- package/dist/src/index.js.map +1 -0
- package/examples/simple.html +26 -0
- package/examples/simple.ts +12 -0
- package/examples/simple.yaml +20 -0
- package/jest.config.ts +9 -0
- package/package.json +36 -0
- package/sample.html +0 -0
- package/src/config/config.ts +12 -0
- package/src/config/factory.ts +142 -0
- package/src/config/index.ts +6 -0
- package/src/config/plain-config.ts +19 -0
- package/src/config/types/array.test.ts +103 -0
- package/src/config/types/array.ts +61 -0
- package/src/config/types/constant.ts +15 -0
- package/src/config/types/object.test.ts +99 -0
- package/src/config/types/object.ts +65 -0
- package/src/config/types/primitive.ts +63 -0
- package/src/config/types/union.ts +36 -0
- package/src/config/types/with-selector.test.ts +32 -0
- package/src/config/types/with-selector.ts +26 -0
- package/src/config/validator.ts +14 -0
- package/src/config-schema.json +144 -0
- package/src/config-schema.test.ts +187 -0
- package/src/core/argument-types.ts +21 -0
- package/src/core/complex-types.ts +4 -0
- package/src/core/primitive-types.ts +5 -0
- package/src/core/property.ts +3 -0
- package/src/core/transformer.ts +21 -0
- package/src/errors/invalid-parse-input-error.ts +7 -0
- package/src/index.ts +26 -0
- package/src/jest.d.ts +5 -0
- package/src/setup-jest-matchers.ts +26 -0
- package/src/transformers/attr.ts +49 -0
- package/src/transformers/exists.ts +20 -0
- package/src/transformers/factory.test.ts +17 -0
- package/src/transformers/factory.ts +54 -0
- package/src/transformers/html.ts +20 -0
- package/src/transformers/index.ts +9 -0
- package/src/transformers/length.ts +28 -0
- package/src/transformers/match-count.ts +20 -0
- package/src/transformers/number.ts +30 -0
- package/src/transformers/resolve.ts +29 -0
- package/src/transformers/transformers.ts +27 -0
- package/src/transformers/trim.ts +32 -0
- package/src/transformers/url-query-param.ts +65 -0
- package/tests/config/constant.test.ts +23 -0
- package/tests/extract.test.ts +21 -0
- package/tests/fixtures/basic.html +16 -0
- package/tests/transformers/attr.test.ts +146 -0
- package/tests/transformers/html.test.ts +25 -0
- package/tests/transformers/url-query-param.test.ts +85 -0
- package/tsconfig.json +10 -0
- package/tsup.config.ts +12 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": { "browser": true, "es2021": true },
|
|
3
|
+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
|
|
4
|
+
"overrides": [
|
|
5
|
+
{
|
|
6
|
+
"env": { "node": true },
|
|
7
|
+
"files": [".eslintrc.{js,cjs}"],
|
|
8
|
+
"parserOptions": { "sourceType": "script" }
|
|
9
|
+
}
|
|
10
|
+
],
|
|
11
|
+
"parser": "@typescript-eslint/parser",
|
|
12
|
+
"parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
|
|
13
|
+
"plugins": ["@typescript-eslint"],
|
|
14
|
+
"rules": {
|
|
15
|
+
"indent": ["error", 2],
|
|
16
|
+
"linebreak-style": ["error", "unix"],
|
|
17
|
+
"quotes": ["error", "single"],
|
|
18
|
+
"semi": ["error", "always"]
|
|
19
|
+
},
|
|
20
|
+
"ignorePatterns": ["dist", "src/jest.d.ts"]
|
|
21
|
+
}
|
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Configurable HTML to JSON converter
|
|
2
|
+
|
|
3
|
+
This tool aims to let you convert your HTMLs to structured JSONs.
|
|
4
|
+
|
|
5
|
+
## API
|
|
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`
|
package/config.yaml
ADDED
|
File without changes
|