@purescraps/purehtml 0.2.0 → 0.4.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/README.md +4 -72
- package/dist/examples/simple.js +135 -76
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +47 -5
- package/dist/src/index.js +138 -68
- package/dist/src/index.js.map +1 -1
- package/package.json +2 -4
- package/src/config-schema.json +1 -1
package/dist/src/index.d.ts
CHANGED
|
@@ -1,9 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PureHTML backend class
|
|
3
|
+
*/
|
|
4
|
+
declare abstract class PureHTMLBackend {
|
|
5
|
+
abstract load(html: Buffer | string): PureHTMLDocument;
|
|
6
|
+
}
|
|
7
|
+
declare abstract class PureHTMLDocument {
|
|
8
|
+
abstract root(): PureHTMLNode;
|
|
9
|
+
abstract $(selector: string): PureHTMLNode[];
|
|
10
|
+
}
|
|
11
|
+
type PureHTMLNodeAttributes = Record<string, string | null>;
|
|
12
|
+
declare abstract class PureHTMLNode {
|
|
13
|
+
abstract attr(name: string): string | null;
|
|
14
|
+
abstract attr(): PureHTMLNodeAttributes;
|
|
15
|
+
abstract find(selector: string): PureHTMLNode[];
|
|
16
|
+
abstract html(): string | null;
|
|
17
|
+
abstract is(selector: string): boolean;
|
|
18
|
+
abstract text(): string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare class PureHTMLCheerioBackend extends PureHTMLBackend {
|
|
22
|
+
load(html: string | Buffer): PureHTMLCheerioDocument;
|
|
23
|
+
}
|
|
24
|
+
declare class PureHTMLCheerioDocument extends PureHTMLDocument {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(root: cheerio.Root);
|
|
27
|
+
$(selector: string): PureHTMLCheerioNode[];
|
|
28
|
+
root(): PureHTMLCheerioNode;
|
|
29
|
+
}
|
|
30
|
+
declare class PureHTMLCheerioNode extends PureHTMLNode {
|
|
31
|
+
#private;
|
|
32
|
+
constructor($: cheerio.Root, el: cheerio.Cheerio);
|
|
33
|
+
attr(name: string): string | null;
|
|
34
|
+
attr(): PureHTMLNodeAttributes;
|
|
35
|
+
find(selector: string): PureHTMLCheerioNode[];
|
|
36
|
+
html(): string | null;
|
|
37
|
+
is(selector: string): boolean;
|
|
38
|
+
text(): string;
|
|
39
|
+
}
|
|
40
|
+
|
|
1
41
|
declare const rootProp: unique symbol;
|
|
2
42
|
type Property = typeof rootProp | string;
|
|
3
43
|
|
|
4
44
|
interface ExtractParams {
|
|
5
|
-
$:
|
|
6
|
-
$el:
|
|
45
|
+
$: PureHTMLDocument;
|
|
46
|
+
$el: PureHTMLNode;
|
|
7
47
|
url: string;
|
|
8
48
|
property: Property;
|
|
9
49
|
}
|
|
@@ -12,13 +52,15 @@ declare abstract class Config<T = unknown> {
|
|
|
12
52
|
}
|
|
13
53
|
|
|
14
54
|
declare class ConfigFactory {
|
|
15
|
-
static fromYAML(yaml: string): Config
|
|
55
|
+
static fromYAML<T>(yaml: string): Config<T>;
|
|
16
56
|
private static generate;
|
|
17
57
|
private static generateSelector;
|
|
18
58
|
private static generateTransform;
|
|
19
59
|
private static detectExpectedType;
|
|
20
60
|
}
|
|
21
61
|
|
|
22
|
-
declare
|
|
62
|
+
declare const cheerio$1: PureHTMLCheerioBackend;
|
|
63
|
+
|
|
64
|
+
declare function extract<T = unknown>(backend: PureHTMLBackend, $: PureHTMLDocument | string, config: Config<T>, url: string): T;
|
|
23
65
|
|
|
24
|
-
export { ConfigFactory, extract };
|
|
66
|
+
export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio$1 as cheerio, extract };
|
package/dist/src/index.js
CHANGED
|
@@ -6785,12 +6785,74 @@ var require_ajv = __commonJS({
|
|
|
6785
6785
|
// src/index.ts
|
|
6786
6786
|
var src_exports = {};
|
|
6787
6787
|
__export(src_exports, {
|
|
6788
|
+
Config: () => Config,
|
|
6788
6789
|
ConfigFactory: () => ConfigFactory,
|
|
6790
|
+
PureHTMLBackend: () => PureHTMLBackend,
|
|
6791
|
+
PureHTMLDocument: () => PureHTMLDocument,
|
|
6792
|
+
PureHTMLNode: () => PureHTMLNode,
|
|
6793
|
+
cheerio: () => cheerio,
|
|
6789
6794
|
extract: () => extract
|
|
6790
6795
|
});
|
|
6791
6796
|
module.exports = __toCommonJS(src_exports);
|
|
6797
|
+
|
|
6798
|
+
// src/backends/cheerio.ts
|
|
6792
6799
|
var import_cheerio = require("cheerio");
|
|
6793
6800
|
|
|
6801
|
+
// src/core/backend.ts
|
|
6802
|
+
var PureHTMLBackend = class {
|
|
6803
|
+
};
|
|
6804
|
+
var PureHTMLDocument = class {
|
|
6805
|
+
};
|
|
6806
|
+
var PureHTMLNode = class {
|
|
6807
|
+
};
|
|
6808
|
+
|
|
6809
|
+
// src/backends/cheerio.ts
|
|
6810
|
+
var PureHTMLCheerioBackend = class extends PureHTMLBackend {
|
|
6811
|
+
load(html) {
|
|
6812
|
+
return new PureHTMLCheerioDocument((0, import_cheerio.load)(html));
|
|
6813
|
+
}
|
|
6814
|
+
};
|
|
6815
|
+
var PureHTMLCheerioDocument = class extends PureHTMLDocument {
|
|
6816
|
+
#root;
|
|
6817
|
+
constructor(root) {
|
|
6818
|
+
super();
|
|
6819
|
+
this.#root = root;
|
|
6820
|
+
}
|
|
6821
|
+
$(selector) {
|
|
6822
|
+
return this.#root(selector).toArray().map((el) => new PureHTMLCheerioNode(this.#root, this.#root(el)));
|
|
6823
|
+
}
|
|
6824
|
+
root() {
|
|
6825
|
+
return new PureHTMLCheerioNode(this.#root, this.#root.root());
|
|
6826
|
+
}
|
|
6827
|
+
};
|
|
6828
|
+
var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
|
|
6829
|
+
#$;
|
|
6830
|
+
#el;
|
|
6831
|
+
constructor($, el) {
|
|
6832
|
+
super();
|
|
6833
|
+
this.#$ = $;
|
|
6834
|
+
this.#el = el;
|
|
6835
|
+
}
|
|
6836
|
+
attr(name) {
|
|
6837
|
+
if (name) {
|
|
6838
|
+
return this.#el.attr(name) ?? null;
|
|
6839
|
+
}
|
|
6840
|
+
return this.#el.attr();
|
|
6841
|
+
}
|
|
6842
|
+
find(selector) {
|
|
6843
|
+
return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
|
|
6844
|
+
}
|
|
6845
|
+
html() {
|
|
6846
|
+
return this.#el.html();
|
|
6847
|
+
}
|
|
6848
|
+
is(selector) {
|
|
6849
|
+
return this.#el.is(selector);
|
|
6850
|
+
}
|
|
6851
|
+
text() {
|
|
6852
|
+
return this.#el.text();
|
|
6853
|
+
}
|
|
6854
|
+
};
|
|
6855
|
+
|
|
6794
6856
|
// src/core/property.ts
|
|
6795
6857
|
var rootProp = Symbol("rootProp");
|
|
6796
6858
|
|
|
@@ -6808,17 +6870,24 @@ var Config = class {
|
|
|
6808
6870
|
// src/config/types/with-selector.ts
|
|
6809
6871
|
var ConfigWithSelector = class extends Config {
|
|
6810
6872
|
selector = null;
|
|
6811
|
-
|
|
6873
|
+
getAllMatches($, { alreadyMatched, includeRoot }) {
|
|
6812
6874
|
if (alreadyMatched) {
|
|
6813
6875
|
return $;
|
|
6814
6876
|
}
|
|
6815
6877
|
if (this.selector === null) {
|
|
6816
6878
|
return $;
|
|
6817
6879
|
}
|
|
6818
|
-
if ($.is(this.selector)) {
|
|
6880
|
+
if (includeRoot && $.is(this.selector)) {
|
|
6819
6881
|
return $;
|
|
6820
6882
|
}
|
|
6821
|
-
return $.find(this.selector);
|
|
6883
|
+
return $.find(this.selector) ?? null;
|
|
6884
|
+
}
|
|
6885
|
+
getFirstMatch($, params) {
|
|
6886
|
+
const matches = this.getAllMatches($, params);
|
|
6887
|
+
if (Array.isArray(matches)) {
|
|
6888
|
+
return matches[0] ?? null;
|
|
6889
|
+
}
|
|
6890
|
+
return matches;
|
|
6822
6891
|
}
|
|
6823
6892
|
};
|
|
6824
6893
|
|
|
@@ -6830,11 +6899,11 @@ var PrimitiveValueConfig = class _PrimitiveValueConfig extends ConfigWithSelecto
|
|
|
6830
6899
|
}
|
|
6831
6900
|
extract(params) {
|
|
6832
6901
|
let val = null;
|
|
6833
|
-
const $el = this.
|
|
6834
|
-
params
|
|
6835
|
-
|
|
6836
|
-
);
|
|
6837
|
-
if ($el
|
|
6902
|
+
const $el = this.getFirstMatch(params.$el, {
|
|
6903
|
+
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
6904
|
+
includeRoot: false
|
|
6905
|
+
});
|
|
6906
|
+
if ($el) {
|
|
6838
6907
|
val = $el.text();
|
|
6839
6908
|
}
|
|
6840
6909
|
if (!this.transform) {
|
|
@@ -6881,16 +6950,17 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
|
|
|
6881
6950
|
super();
|
|
6882
6951
|
}
|
|
6883
6952
|
extract(params) {
|
|
6884
|
-
const $el = this.
|
|
6885
|
-
params
|
|
6886
|
-
|
|
6887
|
-
);
|
|
6953
|
+
const $el = this.getAllMatches(params.$el, {
|
|
6954
|
+
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
6955
|
+
includeRoot: false
|
|
6956
|
+
});
|
|
6957
|
+
const matches = Array.isArray($el) ? $el : [$el];
|
|
6888
6958
|
const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
|
|
6889
6959
|
const extractOpts = { ...params };
|
|
6890
|
-
return
|
|
6960
|
+
return matches.map((el, i) => {
|
|
6891
6961
|
return conf.extract({
|
|
6892
6962
|
...extractOpts,
|
|
6893
|
-
$el:
|
|
6963
|
+
$el: el,
|
|
6894
6964
|
property: this.makeProperty(params.property, i)
|
|
6895
6965
|
});
|
|
6896
6966
|
});
|
|
@@ -6928,17 +6998,19 @@ var AttributeTransformer = class _AttributeTransformer extends Transformer {
|
|
|
6928
6998
|
}
|
|
6929
6999
|
transform({ $el }) {
|
|
6930
7000
|
const { args } = this;
|
|
6931
|
-
|
|
6932
|
-
|
|
6933
|
-
|
|
6934
|
-
|
|
6935
|
-
|
|
6936
|
-
default:
|
|
6937
|
-
return args.reduce((acc, arg) => {
|
|
6938
|
-
acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
|
|
6939
|
-
return acc;
|
|
6940
|
-
}, {});
|
|
7001
|
+
if (!$el) {
|
|
7002
|
+
return null;
|
|
7003
|
+
}
|
|
7004
|
+
if (args.length === 0) {
|
|
7005
|
+
return $el.attr();
|
|
6941
7006
|
}
|
|
7007
|
+
if (args.length === 1) {
|
|
7008
|
+
return _AttributeTransformer.getAttrValue($el, args[0]);
|
|
7009
|
+
}
|
|
7010
|
+
return args.reduce((acc, arg) => {
|
|
7011
|
+
acc[arg] = _AttributeTransformer.getAttrValue($el, arg);
|
|
7012
|
+
return acc;
|
|
7013
|
+
}, {});
|
|
6942
7014
|
}
|
|
6943
7015
|
static getAttrValue($el, attr) {
|
|
6944
7016
|
const val = $el.attr(attr);
|
|
@@ -6961,7 +7033,7 @@ var Exists = class extends Transformer {
|
|
|
6961
7033
|
return BOOLEAN;
|
|
6962
7034
|
}
|
|
6963
7035
|
transform({ $el }) {
|
|
6964
|
-
return
|
|
7036
|
+
return !!$el;
|
|
6965
7037
|
}
|
|
6966
7038
|
};
|
|
6967
7039
|
|
|
@@ -6977,7 +7049,7 @@ var HTMLTransformer = class extends Transformer {
|
|
|
6977
7049
|
return STRING;
|
|
6978
7050
|
}
|
|
6979
7051
|
transform({ $el }) {
|
|
6980
|
-
return $el
|
|
7052
|
+
return $el?.html();
|
|
6981
7053
|
}
|
|
6982
7054
|
};
|
|
6983
7055
|
|
|
@@ -7011,22 +7083,6 @@ var Length = class extends Transformer {
|
|
|
7011
7083
|
}
|
|
7012
7084
|
};
|
|
7013
7085
|
|
|
7014
|
-
// src/transformers/match-count.ts
|
|
7015
|
-
var MatchCount = class extends Transformer {
|
|
7016
|
-
static getName() {
|
|
7017
|
-
return "match-count";
|
|
7018
|
-
}
|
|
7019
|
-
inputType() {
|
|
7020
|
-
return STRING;
|
|
7021
|
-
}
|
|
7022
|
-
outputType() {
|
|
7023
|
-
return NUMBER;
|
|
7024
|
-
}
|
|
7025
|
-
transform({ $el }) {
|
|
7026
|
-
return $el.length;
|
|
7027
|
-
}
|
|
7028
|
-
};
|
|
7029
|
-
|
|
7030
7086
|
// src/transformers/number.ts
|
|
7031
7087
|
var NumberTransformer = class extends Transformer {
|
|
7032
7088
|
static getName() {
|
|
@@ -7136,13 +7192,6 @@ var UrlQueryParamTransformer = class _UrlQueryParamTransformer extends Transform
|
|
|
7136
7192
|
return acc;
|
|
7137
7193
|
}, {});
|
|
7138
7194
|
}
|
|
7139
|
-
static getAttrValue($el, attr) {
|
|
7140
|
-
const val = $el.attr(attr);
|
|
7141
|
-
if (val === void 0) {
|
|
7142
|
-
return null;
|
|
7143
|
-
}
|
|
7144
|
-
return val;
|
|
7145
|
-
}
|
|
7146
7195
|
};
|
|
7147
7196
|
|
|
7148
7197
|
// src/transformers/transformers.ts
|
|
@@ -7152,7 +7201,6 @@ var Transformers = class {
|
|
|
7152
7201
|
Exists,
|
|
7153
7202
|
HTMLTransformer,
|
|
7154
7203
|
Length,
|
|
7155
|
-
MatchCount,
|
|
7156
7204
|
NumberTransformer,
|
|
7157
7205
|
ResolveTransformer,
|
|
7158
7206
|
Trim,
|
|
@@ -7202,16 +7250,27 @@ var TransformerFactory = class _TransformerFactory {
|
|
|
7202
7250
|
};
|
|
7203
7251
|
|
|
7204
7252
|
// src/config/types/constant.ts
|
|
7205
|
-
var ConstantConfig = class _ConstantConfig extends
|
|
7206
|
-
constructor(val) {
|
|
7253
|
+
var ConstantConfig = class _ConstantConfig extends ConfigWithSelector {
|
|
7254
|
+
constructor(val, selector) {
|
|
7207
7255
|
super();
|
|
7208
7256
|
this.val = val;
|
|
7257
|
+
this.selector = selector;
|
|
7209
7258
|
}
|
|
7210
|
-
extract() {
|
|
7259
|
+
extract(params) {
|
|
7260
|
+
if (this.selector) {
|
|
7261
|
+
const matches = this.getAllMatches(params.$el, {
|
|
7262
|
+
alreadyMatched: false,
|
|
7263
|
+
includeRoot: true
|
|
7264
|
+
});
|
|
7265
|
+
if (Array.isArray(matches) && matches.length === 0) {
|
|
7266
|
+
return null;
|
|
7267
|
+
}
|
|
7268
|
+
return this.val;
|
|
7269
|
+
}
|
|
7211
7270
|
return this.val;
|
|
7212
7271
|
}
|
|
7213
|
-
static generate(val) {
|
|
7214
|
-
return new _ConstantConfig(val);
|
|
7272
|
+
static generate(val, selector) {
|
|
7273
|
+
return new _ConstantConfig(val, selector);
|
|
7215
7274
|
}
|
|
7216
7275
|
};
|
|
7217
7276
|
|
|
@@ -7231,11 +7290,11 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
|
|
|
7231
7290
|
}
|
|
7232
7291
|
extract(params) {
|
|
7233
7292
|
const $parent = params.$el;
|
|
7234
|
-
const $el = this.
|
|
7235
|
-
|
|
7236
|
-
|
|
7237
|
-
);
|
|
7238
|
-
if (
|
|
7293
|
+
const $el = this.getFirstMatch($parent, {
|
|
7294
|
+
alreadyMatched: params.elementAlreadyMatched ?? false,
|
|
7295
|
+
includeRoot: false
|
|
7296
|
+
});
|
|
7297
|
+
if (!$el) {
|
|
7239
7298
|
return null;
|
|
7240
7299
|
}
|
|
7241
7300
|
const props = this.properties;
|
|
@@ -7268,8 +7327,11 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7268
7327
|
const $parent = params.$el;
|
|
7269
7328
|
for (const config of this.configs) {
|
|
7270
7329
|
if (config instanceof ConfigWithSelector) {
|
|
7271
|
-
const $el = config.
|
|
7272
|
-
|
|
7330
|
+
const $el = config.getFirstMatch($parent, {
|
|
7331
|
+
alreadyMatched: false,
|
|
7332
|
+
includeRoot: true
|
|
7333
|
+
});
|
|
7334
|
+
if (!$el) {
|
|
7273
7335
|
continue;
|
|
7274
7336
|
}
|
|
7275
7337
|
return config.extract({
|
|
@@ -7278,7 +7340,7 @@ var UnionConfig = class _UnionConfig extends Config {
|
|
|
7278
7340
|
elementAlreadyMatched: true
|
|
7279
7341
|
});
|
|
7280
7342
|
} else {
|
|
7281
|
-
return config.extract({ ...params, $el: params.$
|
|
7343
|
+
return config.extract({ ...params, $el: params.$el });
|
|
7282
7344
|
}
|
|
7283
7345
|
}
|
|
7284
7346
|
return null;
|
|
@@ -7436,7 +7498,7 @@ var oneOf = [
|
|
|
7436
7498
|
{
|
|
7437
7499
|
$comment: "Constant",
|
|
7438
7500
|
type: "object",
|
|
7439
|
-
properties: { constant: {} },
|
|
7501
|
+
properties: { selector: { type: "string" }, constant: {} },
|
|
7440
7502
|
required: ["constant"],
|
|
7441
7503
|
additionalProperties: false
|
|
7442
7504
|
}
|
|
@@ -7490,7 +7552,7 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7490
7552
|
const transform = transformOrig ? _ConfigFactory.generateTransform(transformOrig) : void 0;
|
|
7491
7553
|
switch (expectedType) {
|
|
7492
7554
|
case "constant":
|
|
7493
|
-
return ConstantConfig.generate(constant);
|
|
7555
|
+
return ConstantConfig.generate(constant, selector);
|
|
7494
7556
|
case "object": {
|
|
7495
7557
|
let propConfigs = void 0;
|
|
7496
7558
|
if (properties) {
|
|
@@ -7508,7 +7570,9 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7508
7570
|
transform
|
|
7509
7571
|
);
|
|
7510
7572
|
case "union":
|
|
7511
|
-
return UnionConfig.generate(
|
|
7573
|
+
return UnionConfig.generate(
|
|
7574
|
+
union.map((cfg) => this.generate(cfg))
|
|
7575
|
+
);
|
|
7512
7576
|
default:
|
|
7513
7577
|
return PrimitiveValueConfig.generate(selector, transform);
|
|
7514
7578
|
}
|
|
@@ -7560,10 +7624,11 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7560
7624
|
};
|
|
7561
7625
|
|
|
7562
7626
|
// src/index.ts
|
|
7563
|
-
|
|
7627
|
+
var cheerio = new PureHTMLCheerioBackend();
|
|
7628
|
+
function extract(backend, $, config, url) {
|
|
7564
7629
|
let $root = $;
|
|
7565
7630
|
if (typeof $root === "string") {
|
|
7566
|
-
$root =
|
|
7631
|
+
$root = backend.load($root);
|
|
7567
7632
|
}
|
|
7568
7633
|
return config.extract({
|
|
7569
7634
|
$: $root,
|
|
@@ -7574,7 +7639,12 @@ function extract($, config, url) {
|
|
|
7574
7639
|
}
|
|
7575
7640
|
// Annotate the CommonJS export names for ESM import in node:
|
|
7576
7641
|
0 && (module.exports = {
|
|
7642
|
+
Config,
|
|
7577
7643
|
ConfigFactory,
|
|
7644
|
+
PureHTMLBackend,
|
|
7645
|
+
PureHTMLDocument,
|
|
7646
|
+
PureHTMLNode,
|
|
7647
|
+
cheerio,
|
|
7578
7648
|
extract
|
|
7579
7649
|
});
|
|
7580
7650
|
/*! Bundled license information:
|